from collections import deque
n = int(input())
path = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
C = sorted(list(map(int,input().split())),reverse = True)
M = sum(C[1:])
ans = [-1]*n
que = deque([0])
i = 0
while que:
    p = que.popleft()
    ans[p] = C[i]
    i += 1
    for nxt in path[p]:
        if ans[nxt] >= 0:continue
        que.append(nxt)
print(M)
print(' '.join(str(i) for i in 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) [] path = [[] for _ in range(n)] path [[] for _ in range(n)] for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a) 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 a -= 1 a 1 b -= 1 b 1 path[a].append(b) [a].append [a] path a append b path[b].append(a) [b].append [b] path b append a C = sorted(list(map(int,input().split())),reverse = True) C sorted(list(map(int,input().split())),reverse = True) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split reverse = True reverse True M = sum(C[1:]) M sum(C[1:]) sum C[1:] C 1: 1 ans = [-1]*n ans [-1]*n [-1] -1 n que = deque([0]) que deque([0]) deque [0] 0 i = 0 i 0 while que:
    p = que.popleft()
    ans[p] = C[i]
    i += 1
    for nxt in path[p]:
        if ans[nxt] >= 0:continue
        que.append(nxt) que p = que.popleft() p que.popleft() que.popleft que popleft ans[p] = C[i] ans[p] ans p C[i] C i i += 1 i 1 for nxt in path[p]:
        if ans[nxt] >= 0:continue
        que.append(nxt) nxt path[p] path p if ans[nxt] >= 0:continue
         ans[nxt] >= 0 ans[nxt] ans nxt 0 que.append(nxt) que.append que append nxt print(M) print M print(' '.join(str(i) for i in ans)) print ' '.join(str(i) for i in ans) ' '.join ' ' join str(i) str i b -= 1 1 b p = que.popleft() que.popleft() p que = deque([0]) deque([0]) que C = sorted(list(map(int,input().split())),reverse = True) sorted(list(map(int,input().split())),reverse = True) C i = 0 0 i n = int(input()) int(input()) n path = [[] for _ in range(n)] [[] for _ in range(n)] path i += 1 1 i a,b = map(int,input().split()) map(int,input().split()) a b = map(int,input().split()) map(int,input().split()) b ans = [-1]*n [-1]*n ans a -= 1 1 a M = sum(C[1:]) sum(C[1:]) M
from collections import deque
n = int(input())
path = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
C = sorted(list(map(int,input().split())),reverse = True)
M = sum(C[1:])
ans = [-1]*n
que = deque([0])
i = 0
while que:
    p = que.popleft()
    ans[p] = C[i]
    i += 1
    for nxt in path[p]:
        if ans[nxt] >= 0:continue
        que.append(nxt)
print(M)
print(' '.join(str(i) for i in ans))
