import heapq
import sys
sys.setrecursionlimit(10**7)
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 = [-i for i in list(map(int,input().split()))]
heapq.heapify(c)
print(-sum(c)+min(c))

for i in range(n):
    if len(g[i]) == 1:
        start = i
        break

ans = [-1]*n
ans[start] = -heapq.heappop(c)
q = [start]

while q:
    now = q.pop(-1)
    for i in g[now]:
        if ans[i] == -1:
            ans[i] = -heapq.heappop(c)
            q.append(i)

for i in ans:
    print(i) import heapq heapq import sys sys sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 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 -i for i in list(map(int,input().split())) for i in list(map(int,input().split())) i list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for i in list(map(int,input().split())) -i i c = [-i for i in list(map(int,input().split()))] c [-i for i in list(map(int,input().split()))] heapq.heapify(c) heapq.heapify heapq heapify c print(-sum(c)+min(c)) print -sum(c)+min(c) -sum(c) sum(c) sum c min(c) min c for i in range(n):
    if len(g[i]) == 1:
        start = i
        break i range(n) range n if len(g[i]) == 1:
        start = i
        break len(g[i]) == 1 len(g[i]) len g[i] g i 1 start = i start i break ans = [-1]*n ans [-1]*n [-1] -1 n ans[start] = -heapq.heappop(c) ans[start] ans start -heapq.heappop(c) heapq.heappop(c) heapq.heappop heapq heappop c q = [start] q [start] start while q:
    now = q.pop(-1)
    for i in g[now]:
        if ans[i] == -1:
            ans[i] = -heapq.heappop(c)
            q.append(i) q now = q.pop(-1) now q.pop(-1) q.pop q pop -1 for i in g[now]:
        if ans[i] == -1:
            ans[i] = -heapq.heappop(c)
            q.append(i) i g[now] g now if ans[i] == -1:
            ans[i] = -heapq.heappop(c)
            q.append(i) ans[i] == -1 ans[i] ans i -1 ans[i] = -heapq.heappop(c) ans[i] ans i -heapq.heappop(c) heapq.heappop(c) heapq.heappop heapq heappop c q.append(i) q.append q append i for i in ans:
    print(i) i ans print(i) print i now = q.pop(-1) q.pop(-1) now b = map(int,input().split()) map(int,input().split()) b start = i i start n = int(input()) int(input()) n c = [-i for i in list(map(int,input().split()))] [-i for i in list(map(int,input().split()))] c a,b = map(int,input().split()) map(int,input().split()) a g = [[] for _ in range(n)] [[] for _ in range(n)] g ans = [-1]*n [-1]*n ans q = [start] [start] q
import heapq
import sys
sys.setrecursionlimit(10**7)
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 = [-i for i in list(map(int,input().split()))]
heapq.heapify(c)
print(-sum(c)+min(c))

for i in range(n):
    if len(g[i]) == 1:
        start = i
        break

ans = [-1]*n
ans[start] = -heapq.heappop(c)
q = [start]

while q:
    now = q.pop(-1)
    for i in g[now]:
        if ans[i] == -1:
            ans[i] = -heapq.heappop(c)
            q.append(i)

for i in ans:
    print(i)