from heapq import heappush, heappop, heapify
from sys import setrecursionlimit
setrecursionlimit(10 ** 8)

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    ai, bi = map(lambda x: int(x) - 1, input().split())
    G[ai].append(bi)
    G[bi].append(ai)


# 大きい数字から順番に割り当てる
C = list(map(lambda x: -int(x), input().split()))
heapify(C)
score = -sum(C) + min(C)

# どこかの葉にmaxを置いて，そこから順番に置く
# => maxの値は葉のスコア計算で消えてOK
leaf = -1
for n in range(N):
    if len(G[n]) == 1:
        leaf = n
        break

# 深さ優先で全探索
queue = [leaf]
ans = [-1] * N
while queue:
    node = queue.pop()
    for neighbor in G[node]:
        if ans[neighbor] == -1:
            ans[neighbor] = -heappop(C)
            queue.append(neighbor)

print(score)
print(*ans) from heapq import heappush, heappop, heapify from sys import setrecursionlimit setrecursionlimit(10 ** 8) setrecursionlimit 10 ** 8 10 8 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) [] G = [[] for _ in range(N)] G [[] for _ in range(N)] for _ in range(N - 1):
    ai, bi = map(lambda x: int(x) - 1, input().split())
    G[ai].append(bi)
    G[bi].append(ai)


# 大きい数字から順番に割り当てる _ range(N - 1) range N - 1 N 1 ai, bi = map(lambda x: int(x) - 1, input().split()) ai map(lambda x: int(x) - 1, input().split()) map lambda x: int(x) - 1 int(x) - 1 int(x) int x 1 x input().split() ().split () input split bi G[ai].append(bi) [ai].append [ai] G ai append bi G[bi].append(ai) [bi].append [bi] G bi append ai C = list(map(lambda x: -int(x), input().split())) C list(map(lambda x: -int(x), input().split())) list map(lambda x: -int(x), input().split()) map lambda x: -int(x) -int(x) int(x) int x x input().split() ().split () input split heapify(C) heapify C score = -sum(C) + min(C) score -sum(C) + min(C) -sum(C) sum(C) sum C min(C) min C leaf = -1 leaf -1 for n in range(N):
    if len(G[n]) == 1:
        leaf = n
        break

# 深さ優先で全探索 n range(N) range N if len(G[n]) == 1:
        leaf = n
        break

# 深さ優先で全探索 len(G[n]) == 1 len(G[n]) len G[n] G n 1 leaf = n leaf n break queue = [leaf] queue [leaf] leaf ans = [-1] * N ans [-1] * N [-1] -1 N while queue:
    node = queue.pop()
    for neighbor in G[node]:
        if ans[neighbor] == -1:
            ans[neighbor] = -heappop(C)
            queue.append(neighbor) queue node = queue.pop() node queue.pop() queue.pop queue pop for neighbor in G[node]:
        if ans[neighbor] == -1:
            ans[neighbor] = -heappop(C)
            queue.append(neighbor) neighbor G[node] G node if ans[neighbor] == -1:
            ans[neighbor] = -heappop(C)
            queue.append(neighbor) ans[neighbor] == -1 ans[neighbor] ans neighbor -1 ans[neighbor] = -heappop(C) ans[neighbor] ans neighbor -heappop(C) heappop(C) heappop C queue.append(neighbor) queue.append queue append neighbor print(score) print score print(*ans) print *ans ans G = [[] for _ in range(N)] [[] for _ in range(N)] G ai, bi = map(lambda x: int(x) - 1, input().split()) map(lambda x: int(x) - 1, input().split()) ai leaf = -1 -1 leaf bi = map(lambda x: int(x) - 1, input().split()) map(lambda x: int(x) - 1, input().split()) bi N = int(input()) int(input()) N ans = [-1] * N [-1] * N ans leaf = n n leaf score = -sum(C) + min(C) -sum(C) + min(C) score node = queue.pop() queue.pop() node queue = [leaf] [leaf] queue C = list(map(lambda x: -int(x), input().split())) list(map(lambda x: -int(x), input().split())) C
from heapq import heappush, heappop, heapify
from sys import setrecursionlimit
setrecursionlimit(10 ** 8)

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    ai, bi = map(lambda x: int(x) - 1, input().split())
    G[ai].append(bi)
    G[bi].append(ai)


# 大きい数字から順番に割り当てる
C = list(map(lambda x: -int(x), input().split()))
heapify(C)
score = -sum(C) + min(C)

# どこかの葉にmaxを置いて，そこから順番に置く
# => maxの値は葉のスコア計算で消えてOK
leaf = -1
for n in range(N):
    if len(G[n]) == 1:
        leaf = n
        break

# 深さ優先で全探索
queue = [leaf]
ans = [-1] * N
while queue:
    node = queue.pop()
    for neighbor in G[node]:
        if ans[neighbor] == -1:
            ans[neighbor] = -heappop(C)
            queue.append(neighbor)

print(score)
print(*ans)