from collections import deque
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

c = list(map(int, input().split()))
c.sort()
score = sum(c)-c[-1]

for i in range(n):
    if len(graph[i]) == 1:
        next_v = deque([i])
        break
# dfs
ans = [-1]*n
while next_v:
    v = next_v.popleft()
    ans[v] = c.pop()
    for i in graph[v]:
        if ans[i] != -1:
            continue
        next_v.append(i)

print(score)
print(*ans, sep=" ") 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())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a) _ 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 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a 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() c.sort c sort score = sum(c)-c[-1] score sum(c)-c[-1] sum(c) sum c c[-1] c -1 for i in range(n):
    if len(graph[i]) == 1:
        next_v = deque([i])
        break
# dfs i range(n) range n if len(graph[i]) == 1:
        next_v = deque([i])
        break
# dfs len(graph[i]) == 1 len(graph[i]) len graph[i] graph i 1 next_v = deque([i]) next_v deque([i]) deque [i] i break ans = [-1]*n ans [-1]*n [-1] -1 n while next_v:
    v = next_v.popleft()
    ans[v] = c.pop()
    for i in graph[v]:
        if ans[i] != -1:
            continue
        next_v.append(i) next_v v = next_v.popleft() v next_v.popleft() next_v.popleft next_v popleft ans[v] = c.pop() ans[v] ans v c.pop() c.pop c pop for i in graph[v]:
        if ans[i] != -1:
            continue
        next_v.append(i) i graph[v] graph v if ans[i] != -1:
            continue
         ans[i] != -1 ans[i] ans i -1 continue next_v.append(i) next_v.append next_v append i print(score) print score print(*ans, sep=" ") print *ans ans sep=" " sep " " n = int(input()) int(input()) n a -= 1 1 a graph = [[] for _ in range(n)] [[] for _ in range(n)] graph b -= 1 1 b ans = [-1]*n [-1]*n ans b = map(int, input().split()) map(int, input().split()) b score = sum(c)-c[-1] sum(c)-c[-1] score c = list(map(int, input().split())) list(map(int, input().split())) c v = next_v.popleft() next_v.popleft() v next_v = deque([i]) deque([i]) next_v a, b = map(int, input().split()) map(int, input().split()) a
from collections import deque
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

c = list(map(int, input().split()))
c.sort()
score = sum(c)-c[-1]

for i in range(n):
    if len(graph[i]) == 1:
        next_v = deque([i])
        break
# dfs
ans = [-1]*n
while next_v:
    v = next_v.popleft()
    ans[v] = c.pop()
    for i in graph[v]:
        if ans[i] != -1:
            continue
        next_v.append(i)

print(score)
print(*ans, sep=" ")
