from collections import deque
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
c = list(map(int, input().split()))
c.sort(reverse=True)
ans = [None]*n
ans[0] = c[0]
q = deque([0])
i = 1
while q:
    node = q.popleft()
    for c_node in graph[node]:
        if ans[c_node] is not None:
            continue
        ans[c_node] = c[i]
        i += 1
        q.append(c_node)
print(sum(c)-c[0])
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) [] graph = [[] for _ in range(n)] graph [[] for _ in range(n)] for _ in range(n-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1) _ 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 graph[a-1].append(b-1) [a-1].append [a-1] graph a-1 a 1 append b-1 b 1 graph[b-1].append(a-1) [b-1].append [b-1] graph b-1 b 1 append a-1 a 1 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(reverse=True) c.sort c sort reverse=True reverse True ans = [None]*n ans [None]*n [None] None n ans[0] = c[0] ans[0] ans 0 c[0] c 0 q = deque([0]) q deque([0]) deque [0] 0 i = 1 i 1 while q:
    node = q.popleft()
    for c_node in graph[node]:
        if ans[c_node] is not None:
            continue
        ans[c_node] = c[i]
        i += 1
        q.append(c_node) q node = q.popleft() node q.popleft() q.popleft q popleft for c_node in graph[node]:
        if ans[c_node] is not None:
            continue
        ans[c_node] = c[i]
        i += 1
        q.append(c_node) c_node graph[node] graph node if ans[c_node] is not None:
            continue
         ans[c_node] is not None ans[c_node] ans c_node None continue ans[c_node] = c[i] ans[c_node] ans c_node c[i] c i i += 1 i 1 q.append(c_node) q.append q append c_node print(sum(c)-c[0]) print sum(c)-c[0] sum(c) sum c c[0] c 0 print(*ans) print *ans ans q = deque([0]) deque([0]) q i += 1 1 i b = map(int, input().split()) map(int, input().split()) b a, b = map(int, input().split()) map(int, input().split()) a node = q.popleft() q.popleft() node n = int(input()) int(input()) n ans = [None]*n [None]*n ans i = 1 1 i graph = [[] for _ in range(n)] [[] for _ in range(n)] graph c = list(map(int, input().split())) list(map(int, input().split())) c
from collections import deque
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
c = list(map(int, input().split()))
c.sort(reverse=True)
ans = [None]*n
ans[0] = c[0]
q = deque([0])
i = 1
while q:
    node = q.popleft()
    for c_node in graph[node]:
        if ans[c_node] is not None:
            continue
        ans[c_node] = c[i]
        i += 1
        q.append(c_node)
print(sum(c)-c[0])
print(*ans)