from collections import deque

N = int(input())
tree = [set() for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    tree[a - 1].add(b - 1)
    tree[b - 1].add(a - 1)
C = sorted(list(map(int, input().split())))

ans = [0] * N

print(sum(C[:-1]))

ans[0] = C.pop()
n = min(tree[0])
ans[n] = C.pop()

d = deque([0, n])
visited = set(d)
while d:
    x = d.pop()
    for new_x in tree[x]:
        if new_x not in visited:
            ans[new_x] = C.pop()
            d.append(new_x)
            visited.add(new_x)

print(*ans) from collections import deque N = int(input()) N int(input()) int input() input set() for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) set() set tree = [set() for _ in range(N)] tree [set() for _ in range(N)] for _ in range(N - 1):
    a, b = map(int, input().split())
    tree[a - 1].add(b - 1)
    tree[b - 1].add(a - 1) _ 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 tree[a - 1].add(b - 1) [a - 1].add [a - 1] tree a - 1 a 1 add b - 1 b 1 tree[b - 1].add(a - 1) [b - 1].add [b - 1] tree b - 1 b 1 add a - 1 a 1 C = sorted(list(map(int, input().split()))) C sorted(list(map(int, input().split()))) sorted list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = [0] * N ans [0] * N [0] 0 N print(sum(C[:-1])) print sum(C[:-1]) sum C[:-1] C :-1 -1 ans[0] = C.pop() ans[0] ans 0 C.pop() C.pop C pop n = min(tree[0]) n min(tree[0]) min tree[0] tree 0 ans[n] = C.pop() ans[n] ans n C.pop() C.pop C pop d = deque([0, n]) d deque([0, n]) deque [0, n] 0 n visited = set(d) visited set(d) set d while d:
    x = d.pop()
    for new_x in tree[x]:
        if new_x not in visited:
            ans[new_x] = C.pop()
            d.append(new_x)
            visited.add(new_x) d x = d.pop() x d.pop() d.pop d pop for new_x in tree[x]:
        if new_x not in visited:
            ans[new_x] = C.pop()
            d.append(new_x)
            visited.add(new_x) new_x tree[x] tree x if new_x not in visited:
            ans[new_x] = C.pop()
            d.append(new_x)
            visited.add(new_x) new_x not in visited new_x visited ans[new_x] = C.pop() ans[new_x] ans new_x C.pop() C.pop C pop d.append(new_x) d.append d append new_x visited.add(new_x) visited.add visited add new_x print(*ans) print *ans ans ans = [0] * N [0] * N ans n = min(tree[0]) min(tree[0]) n b = map(int, input().split()) map(int, input().split()) b C = sorted(list(map(int, input().split()))) sorted(list(map(int, input().split()))) C a, b = map(int, input().split()) map(int, input().split()) a tree = [set() for _ in range(N)] [set() for _ in range(N)] tree d = deque([0, n]) deque([0, n]) d x = d.pop() d.pop() x visited = set(d) set(d) visited N = int(input()) int(input()) N
from collections import deque

N = int(input())
tree = [set() for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    tree[a - 1].add(b - 1)
    tree[b - 1].add(a - 1)
C = sorted(list(map(int, input().split())))

ans = [0] * N

print(sum(C[:-1]))

ans[0] = C.pop()
n = min(tree[0])
ans[n] = C.pop()

d = deque([0, n])
visited = set(d)
while d:
    x = d.pop()
    for new_x in tree[x]:
        if new_x not in visited:
            ans[new_x] = C.pop()
            d.append(new_x)
            visited.add(new_x)

print(*ans)
