from collections import deque

n = int(input())
tree = {i:[] for i in range(1, n+1)}
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)
c = list(map(int, input().split()))

c = sorted(c, reverse = True)
d = [-1]*(n+1)
que = deque([1])
ic = 0
while len(que) > 0:
    x = que.pop()
    d[x] = c[ic]
    ic += 1
    for y in tree[x]:
        if d[y] < 0:
            que.append(y)
print(sum(c[1:]))
print(*d[1:]) from collections import deque n = int(input()) n int(input()) int input() input i:[] for i in range(1, n+1) for i in range(1, n+1) i range(1, n+1) range 1 n+1 n 1 for i in range(1, n+1) i [] tree = {i:[] for i in range(1, n+1)} tree {i:[] for i in range(1, n+1)} for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[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 tree[a].append(b) [a].append [a] tree a append b tree[b].append(a) [b].append [b] tree 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 = sorted(c, reverse = True) c sorted(c, reverse = True) sorted c reverse = True reverse True d = [-1]*(n+1) d [-1]*(n+1) [-1] -1 n+1 n 1 que = deque([1]) que deque([1]) deque [1] 1 ic = 0 ic 0 while len(que) > 0:
    x = que.pop()
    d[x] = c[ic]
    ic += 1
    for y in tree[x]:
        if d[y] < 0:
            que.append(y) len(que) > 0 len(que) len que 0 x = que.pop() x que.pop() que.pop que pop d[x] = c[ic] d[x] d x c[ic] c ic ic += 1 ic 1 for y in tree[x]:
        if d[y] < 0:
            que.append(y) y tree[x] tree x if d[y] < 0:
            que.append(y) d[y] < 0 d[y] d y 0 que.append(y) que.append que append y print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(*d[1:]) print *d[1:] d[1:] d 1: 1 tree = {i:[] for i in range(1, n+1)} {i:[] for i in range(1, n+1)} tree n = int(input()) int(input()) n c = list(map(int, input().split())) list(map(int, input().split())) c c = sorted(c, reverse = True) sorted(c, reverse = True) c x = que.pop() que.pop() x a, b = map(int, input().split()) map(int, input().split()) a b = map(int, input().split()) map(int, input().split()) b que = deque([1]) deque([1]) que ic = 0 0 ic d = [-1]*(n+1) [-1]*(n+1) d ic += 1 1 ic
from collections import deque

n = int(input())
tree = {i:[] for i in range(1, n+1)}
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)
c = list(map(int, input().split()))

c = sorted(c, reverse = True)
d = [-1]*(n+1)
que = deque([1])
ic = 0
while len(que) > 0:
    x = que.pop()
    d[x] = c[ic]
    ic += 1
    for y in tree[x]:
        if d[y] < 0:
            que.append(y)
print(sum(c[1:]))
print(*d[1:])
