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

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

d = [0] * n

q = deque([0])
d[0] = c.pop()

while q:
    v = q.popleft()
    for e in es[v]:
        if d[e]==0:
            d[e] = c.pop()
            q.append(e)
print(*d) 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) [] es = [[] for i in range(n)] es [[] for i in range(n)] for i in range(n - 1):
    a, b = map(int, input().split())
    es[a - 1].append(b - 1)
    es[b - 1].append(a - 1) 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 es[a - 1].append(b - 1) [a - 1].append [a - 1] es a - 1 a 1 append b - 1 b 1 es[b - 1].append(a - 1) [b - 1].append [b - 1] es 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() c.sort c sort print(sum(c) - c[-1]) print sum(c) - c[-1] sum(c) sum c c[-1] c -1 d = [0] * n d [0] * n [0] 0 n q = deque([0]) q deque([0]) deque [0] 0 d[0] = c.pop() d[0] d 0 c.pop() c.pop c pop while q:
    v = q.popleft()
    for e in es[v]:
        if d[e]==0:
            d[e] = c.pop()
            q.append(e) q v = q.popleft() v q.popleft() q.popleft q popleft for e in es[v]:
        if d[e]==0:
            d[e] = c.pop()
            q.append(e) e es[v] es v if d[e]==0:
            d[e] = c.pop()
            q.append(e) d[e]==0 d[e] d e 0 d[e] = c.pop() d[e] d e c.pop() c.pop c pop q.append(e) q.append q append e print(*d) print *d d c = list(map(int, input().split())) list(map(int, input().split())) c es = [[] for i in range(n)] [[] for i in range(n)] es b = map(int, input().split()) map(int, input().split()) b d = [0] * n [0] * n d q = deque([0]) deque([0]) q n = int(input()) int(input()) n v = q.popleft() q.popleft() v a, b = map(int, input().split()) map(int, input().split()) a
from collections import deque 
n = int(input())
es = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    es[a - 1].append(b - 1)
    es[b - 1].append(a - 1)

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

d = [0] * n

q = deque([0])
d[0] = c.pop()

while q:
    v = q.popleft()
    for e in es[v]:
        if d[e]==0:
            d[e] = c.pop()
            q.append(e)
print(*d)