import sys
input = sys.stdin.readline

N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

c = list(map(int, input().split()))
c.sort(reverse = False)

tmp = len(graph[0])
start = 0
for index, x in enumerate(graph):
    if len(x) > tmp:
        tmp = len(x)
        start = index

ans = 0
ans_lst = [-1] * N
ans_lst[start] = c.pop()
stack = [start]
while stack:
    x = stack.pop()
    for next_ in graph[x]:
        if ans_lst[next_] == -1:
            tmp = c.pop()
            ans_lst[next_] = tmp
            stack.append(next_)
            ans += tmp

print (ans)
print (*ans_lst, sep = ' ') import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] graph = [[] for _ in range(N)] graph [[] for _ in range(N)] for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a) _ range(N - 1) range N - 1 N 1 a, b = map(int, input().split()) a map(int, input().split()) map int input().split() ().split () input split b a -= 1 a 1 b -= 1 b 1 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort(reverse = False) c.sort c sort reverse = False reverse False tmp = len(graph[0]) tmp len(graph[0]) len graph[0] graph 0 start = 0 start 0 for index, x in enumerate(graph):
    if len(x) > tmp:
        tmp = len(x)
        start = index index x enumerate(graph) enumerate graph if len(x) > tmp:
        tmp = len(x)
        start = index len(x) > tmp len(x) len x tmp tmp = len(x) tmp len(x) len x start = index start index ans = 0 ans 0 ans_lst = [-1] * N ans_lst [-1] * N [-1] -1 N ans_lst[start] = c.pop() ans_lst[start] ans_lst start c.pop() c.pop c pop stack = [start] stack [start] start while stack:
    x = stack.pop()
    for next_ in graph[x]:
        if ans_lst[next_] == -1:
            tmp = c.pop()
            ans_lst[next_] = tmp
            stack.append(next_)
            ans += tmp stack x = stack.pop() x stack.pop() stack.pop stack pop for next_ in graph[x]:
        if ans_lst[next_] == -1:
            tmp = c.pop()
            ans_lst[next_] = tmp
            stack.append(next_)
            ans += tmp next_ graph[x] graph x if ans_lst[next_] == -1:
            tmp = c.pop()
            ans_lst[next_] = tmp
            stack.append(next_)
            ans += tmp ans_lst[next_] == -1 ans_lst[next_] ans_lst next_ -1 tmp = c.pop() tmp c.pop() c.pop c pop ans_lst[next_] = tmp ans_lst[next_] ans_lst next_ tmp stack.append(next_) stack.append stack append next_ ans += tmp ans tmp print (ans) print ans print (*ans_lst, sep = ' ') print *ans_lst ans_lst sep = ' ' sep ' ' input = sys.stdin.readline sys.stdin.readline input x = stack.pop() stack.pop() x stack = [start] [start] stack ans += tmp tmp ans tmp = c.pop() c.pop() tmp b = map(int, input().split()) map(int, input().split()) b a, b = map(int, input().split()) map(int, input().split()) a start = index index start start = 0 0 start ans = 0 0 ans c = list(map(int, input().split())) list(map(int, input().split())) c a -= 1 1 a b -= 1 1 b graph = [[] for _ in range(N)] [[] for _ in range(N)] graph N = int(input()) int(input()) N tmp = len(graph[0]) len(graph[0]) tmp ans_lst = [-1] * N [-1] * N ans_lst tmp = len(x) len(x) tmp
import sys
input = sys.stdin.readline

N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

c = list(map(int, input().split()))
c.sort(reverse = False)

tmp = len(graph[0])
start = 0
for index, x in enumerate(graph):
    if len(x) > tmp:
        tmp = len(x)
        start = index

ans = 0
ans_lst = [-1] * N
ans_lst[start] = c.pop()
stack = [start]
while stack:
    x = stack.pop()
    for next_ in graph[x]:
        if ans_lst[next_] == -1:
            tmp = c.pop()
            ans_lst[next_] = tmp
            stack.append(next_)
            ans += tmp

print (ans)
print (*ans_lst, sep = ' ')
