from collections import deque

n = int(input())
g = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)
c = list(map(int, input().split()))
c.sort()
m = sum(c[:-1])
ans = [-1] * n

q = deque([(0, -1)])
while q:
    v, pv = q.popleft()
    ans[v] = c.pop()
    for nv in g[v]:
        if nv == pv: continue
        q.append((nv, v))

print(m)
print(*ans) from collections import deque 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):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[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 g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g 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() c.sort c sort m = sum(c[:-1]) m sum(c[:-1]) sum c[:-1] c :-1 -1 ans = [-1] * n ans [-1] * n [-1] -1 n q = deque([(0, -1)]) q deque([(0, -1)]) deque [(0, -1)] (0, -1) 0 -1 while q:
    v, pv = q.popleft()
    ans[v] = c.pop()
    for nv in g[v]:
        if nv == pv: continue
        q.append((nv, v)) q v, pv = q.popleft() v q.popleft() q.popleft q popleft pv ans[v] = c.pop() ans[v] ans v c.pop() c.pop c pop for nv in g[v]:
        if nv == pv: continue
        q.append((nv, v)) nv g[v] g v if nv == pv: continue
         nv == pv nv pv q.append((nv, v)) q.append q append (nv, v) nv v print(m) print m print(*ans) print *ans ans ans = [-1] * n [-1] * n ans q = deque([(0, -1)]) deque([(0, -1)]) q a, b = map(int, input().split()) map(int, input().split()) a a -= 1 1 a b = map(int, input().split()) map(int, input().split()) b b -= 1 1 b v, pv = q.popleft() q.popleft() v g = [[] for _ in range(n)] [[] for _ in range(n)] g c = list(map(int, input().split())) list(map(int, input().split())) c pv = q.popleft() q.popleft() pv n = int(input()) int(input()) n m = sum(c[:-1]) sum(c[:-1]) m
from collections import deque

n = int(input())
g = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)
c = list(map(int, input().split()))
c.sort()
m = sum(c[:-1])
ans = [-1] * n

q = deque([(0, -1)])
while q:
    v, pv = q.popleft()
    ans[v] = c.pop()
    for nv in g[v]:
        if nv == pv: continue
        q.append((nv, v))

print(m)
print(*ans)
