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

d = list(map(int, input().split()))
d.sort(reverse=True)

que = deque()
que.append(0)
seen = [False for _ in range(n)]
seen[0] = True
memo = [0]
while que:
    cur = que.popleft()
    for to in edges[cur]:
        if seen[to]:
            continue
        seen[to] = True
        memo.append(to)
        que.append(to)


print(sum(d) - d[0])

ans = [0 for _ in range(n)]
for i in range(n):
    ans[memo[i]] = d[i]

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) [] edges = [[] for _ in range(n)] edges [[] for _ in range(n)] for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[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 edges[a-1].append(b-1) [a-1].append [a-1] edges a-1 a 1 append b-1 b 1 edges[b-1].append(a-1) [b-1].append [b-1] edges b-1 b 1 append a-1 a 1 d = list(map(int, input().split())) d list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split d.sort(reverse=True) d.sort d sort reverse=True reverse True que = deque() que deque() deque que.append(0) que.append que append 0 False for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) False seen = [False for _ in range(n)] seen [False for _ in range(n)] seen[0] = True seen[0] seen 0 True memo = [0] memo [0] 0 while que:
    cur = que.popleft()
    for to in edges[cur]:
        if seen[to]:
            continue
        seen[to] = True
        memo.append(to)
        que.append(to) que cur = que.popleft() cur que.popleft() que.popleft que popleft for to in edges[cur]:
        if seen[to]:
            continue
        seen[to] = True
        memo.append(to)
        que.append(to) to edges[cur] edges cur if seen[to]:
            continue
         seen[to] seen to continue seen[to] = True seen[to] seen to True memo.append(to) memo.append memo append to que.append(to) que.append que append to print(sum(d) - d[0]) print sum(d) - d[0] sum(d) sum d d[0] d 0 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 ans = [0 for _ in range(n)] ans [0 for _ in range(n)] for i in range(n):
    ans[memo[i]] = d[i] i range(n) range n ans[memo[i]] = d[i] ans[memo[i]] ans memo[i] memo i d[i] d i print(*ans) print *ans ans d = list(map(int, input().split())) list(map(int, input().split())) d b = map(int, input().split()) map(int, input().split()) b que = deque() deque() que seen = [False for _ in range(n)] [False for _ in range(n)] seen memo = [0] [0] memo cur = que.popleft() que.popleft() cur ans = [0 for _ in range(n)] [0 for _ in range(n)] ans edges = [[] for _ in range(n)] [[] for _ in range(n)] edges n = int(input()) int(input()) n a, b = map(int, input().split()) map(int, input().split()) a
from collections import deque
n = int(input())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)

d = list(map(int, input().split()))
d.sort(reverse=True)

que = deque()
que.append(0)
seen = [False for _ in range(n)]
seen[0] = True
memo = [0]
while que:
    cur = que.popleft()
    for to in edges[cur]:
        if seen[to]:
            continue
        seen[to] = True
        memo.append(to)
        que.append(to)


print(sum(d) - d[0])

ans = [0 for _ in range(n)]
for i in range(n):
    ans[memo[i]] = d[i]

print(*ans)