from collections import deque

n = int(input())
ab = [list(map(int, input().split())) for _ in range(n-1)]
c = sorted(list(map(int, input().split())))

l = [[] for _ in range(n)]
for a, b in ab:
    a, b = a-1, b-1
    l[a].append(b)
    l[b].append(a)
t = [-1]*n
q = deque([0])
w = [0]*n
w[0] = c.pop()
ans = 0
while q:
    v = q.pop()
    for i in l[v]:
        if i == t[v]:
            continue
        t[i] = v
        q.appendleft(i)
        w[i] = c.pop()
        ans += min(w[v], w[i])
print(ans)
print(*w) from collections import deque n = int(input()) n int(input()) int input() input list(map(int, input().split())) for _ in range(n-1) for _ in range(n-1) _ range(n-1) range n-1 n 1 for _ in range(n-1) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ab = [list(map(int, input().split())) for _ in range(n-1)] ab [list(map(int, input().split())) for _ in range(n-1)] 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 [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] l = [[] for _ in range(n)] l [[] for _ in range(n)] for a, b in ab:
    a, b = a-1, b-1
    l[a].append(b)
    l[b].append(a) a b ab a, b = a-1, b-1 a a-1 a 1 b b-1 b 1 l[a].append(b) [a].append [a] l a append b l[b].append(a) [b].append [b] l b append a t = [-1]*n t [-1]*n [-1] -1 n q = deque([0]) q deque([0]) deque [0] 0 w = [0]*n w [0]*n [0] 0 n w[0] = c.pop() w[0] w 0 c.pop() c.pop c pop ans = 0 ans 0 while q:
    v = q.pop()
    for i in l[v]:
        if i == t[v]:
            continue
        t[i] = v
        q.appendleft(i)
        w[i] = c.pop()
        ans += min(w[v], w[i]) q v = q.pop() v q.pop() q.pop q pop for i in l[v]:
        if i == t[v]:
            continue
        t[i] = v
        q.appendleft(i)
        w[i] = c.pop()
        ans += min(w[v], w[i]) i l[v] l v if i == t[v]:
            continue
         i == t[v] i t[v] t v continue t[i] = v t[i] t i v q.appendleft(i) q.appendleft q appendleft i w[i] = c.pop() w[i] w i c.pop() c.pop c pop ans += min(w[v], w[i]) ans min(w[v], w[i]) min w[v] w v w[i] w i print(ans) print ans print(*w) print *w w b = a-1, b-1 b-1 b v = q.pop() q.pop() v w = [0]*n [0]*n w ab = [list(map(int, input().split())) for _ in range(n-1)] [list(map(int, input().split())) for _ in range(n-1)] ab q = deque([0]) deque([0]) q c = sorted(list(map(int, input().split()))) sorted(list(map(int, input().split()))) c n = int(input()) int(input()) n l = [[] for _ in range(n)] [[] for _ in range(n)] l a, b = a-1, b-1 a-1 a t = [-1]*n [-1]*n t ans += min(w[v], w[i]) min(w[v], w[i]) ans ans = 0 0 ans
from collections import deque

n = int(input())
ab = [list(map(int, input().split())) for _ in range(n-1)]
c = sorted(list(map(int, input().split())))

l = [[] for _ in range(n)]
for a, b in ab:
    a, b = a-1, b-1
    l[a].append(b)
    l[b].append(a)
t = [-1]*n
q = deque([0])
w = [0]*n
w[0] = c.pop()
ans = 0
while q:
    v = q.pop()
    for i in l[v]:
        if i == t[v]:
            continue
        t[i] = v
        q.appendleft(i)
        w[i] = c.pop()
        ans += min(w[v], w[i])
print(ans)
print(*w)