from collections import deque

n = int(input())
t = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    t[a].append(b)
    t[b].append(a)
c = sorted(list(map(int, input().split())))

queue = deque([0])
ans = [0 for i in range(n)]
num = n - 1
while queue:
    cur_node = queue.popleft()
    if ans[cur_node] > 0:
        continue
    ans[cur_node] = c[num]
    num -= 1
    for i in t[cur_node]:
        queue.append(i)
print(sum(c) - max(c))
print(*ans) from collections import deque n = int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] t = [[] for i in range(n)] t [[] for i in range(n)] for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    t[a].append(b)
    t[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 t[a].append(b) [a].append [a] t a append b t[b].append(a) [b].append [b] t b append a c = sorted(list(map(int, input().split()))) c sorted(list(map(int, input().split()))) sorted list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split queue = deque([0]) queue deque([0]) deque [0] 0 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 ans = [0 for i in range(n)] ans [0 for i in range(n)] num = n - 1 num n - 1 n 1 while queue:
    cur_node = queue.popleft()
    if ans[cur_node] > 0:
        continue
    ans[cur_node] = c[num]
    num -= 1
    for i in t[cur_node]:
        queue.append(i) queue cur_node = queue.popleft() cur_node queue.popleft() queue.popleft queue popleft if ans[cur_node] > 0:
        continue
     ans[cur_node] > 0 ans[cur_node] ans cur_node 0 continue ans[cur_node] = c[num] ans[cur_node] ans cur_node c[num] c num num -= 1 num 1 for i in t[cur_node]:
        queue.append(i) i t[cur_node] t cur_node queue.append(i) queue.append queue append i print(sum(c) - max(c)) print sum(c) - max(c) sum(c) sum c max(c) max c print(*ans) print *ans ans t = [[] for i in range(n)] [[] for i in range(n)] t n = int(input()) int(input()) n b = map(int, input().split()) map(int, input().split()) b num = n - 1 n - 1 num cur_node = queue.popleft() queue.popleft() cur_node a, b = map(int, input().split()) map(int, input().split()) a c = sorted(list(map(int, input().split()))) sorted(list(map(int, input().split()))) c queue = deque([0]) deque([0]) queue ans = [0 for i in range(n)] [0 for i in range(n)] ans num -= 1 1 num b -= 1 1 b a -= 1 1 a
from collections import deque

n = int(input())
t = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    t[a].append(b)
    t[b].append(a)
c = sorted(list(map(int, input().split())))

queue = deque([0])
ans = [0 for i in range(n)]
num = n - 1
while queue:
    cur_node = queue.popleft()
    if ans[cur_node] > 0:
        continue
    ans[cur_node] = c[num]
    num -= 1
    for i in t[cur_node]:
        queue.append(i)
print(sum(c) - max(c))
print(*ans)