import heapq

n = int(input())

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

c = list(map(lambda x: -int(x), input().split()))
heapq.heapify(c)
print(min(c) - sum(c))

ans = []
s = [-1] * n
leaf = -1
for i, a in enumerate(g):
    if len(a) == 1:
        leaf = i
        break

que = [leaf]
while que:
    t = que.pop()
    for e in g[t]:
        if s[e] == -1:
            s[e] = -heapq.heappop(c)
            que.append(e)

print(*s) import heapq heapq 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 i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1) i 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 g[a-1].append(b-1) [a-1].append [a-1] g a-1 a 1 append b-1 b 1 g[b-1].append(a-1) [b-1].append [b-1] g b-1 b 1 append a-1 a 1 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 heapq.heapify(c) heapq.heapify heapq heapify c print(min(c) - sum(c)) print min(c) - sum(c) min(c) min c sum(c) sum c ans = [] ans [] s = [-1] * n s [-1] * n [-1] -1 n leaf = -1 leaf -1 for i, a in enumerate(g):
    if len(a) == 1:
        leaf = i
        break i a enumerate(g) enumerate g if len(a) == 1:
        leaf = i
        break len(a) == 1 len(a) len a 1 leaf = i leaf i break que = [leaf] que [leaf] leaf while que:
    t = que.pop()
    for e in g[t]:
        if s[e] == -1:
            s[e] = -heapq.heappop(c)
            que.append(e) que t = que.pop() t que.pop() que.pop que pop for e in g[t]:
        if s[e] == -1:
            s[e] = -heapq.heappop(c)
            que.append(e) e g[t] g t if s[e] == -1:
            s[e] = -heapq.heappop(c)
            que.append(e) s[e] == -1 s[e] s e -1 s[e] = -heapq.heappop(c) s[e] s e -heapq.heappop(c) heapq.heappop(c) heapq.heappop heapq heappop c que.append(e) que.append que append e print(*s) print *s s que = [leaf] [leaf] que a, b = map(int, input().split()) map(int, input().split()) a s = [-1] * n [-1] * n s leaf = -1 -1 leaf g = [[] for _ in range(n)] [[] for _ in range(n)] g b = map(int, input().split()) map(int, input().split()) b n = int(input()) int(input()) n ans = [] [] ans leaf = i i leaf t = que.pop() que.pop() t c = list(map(lambda x: -int(x), input().split())) list(map(lambda x: -int(x), input().split())) c
import heapq

n = int(input())

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

c = list(map(lambda x: -int(x), input().split()))
heapq.heapify(c)
print(min(c) - sum(c))

ans = []
s = [-1] * n
leaf = -1
for i, a in enumerate(g):
    if len(a) == 1:
        leaf = i
        break

que = [leaf]
while que:
    t = que.pop()
    for e in g[t]:
        if s[e] == -1:
            s[e] = -heapq.heappop(c)
            que.append(e)

print(*s)
