from collections import deque
n = int(input())
ki = [[] for i in range(n)]
d = [0 for i in range(n)]
for i in range(n-1):
    a , b = map(lambda x: int(x)-1,input().split())
    ki[a].append(b)
    ki[b].append(a)
c = list(map(int,input().split()))
c.sort(reverse=True)
c = deque(c)
ans = 0
d[0] = c.popleft()
q = deque()
q.append(0)
while q:
    now = q.popleft()
    for i in ki[now]:
        if d[i] == 0:
            d[i] = c.popleft()
            q.append(i)
            ans += d[i]
print(ans)
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) [] ki = [[] for i in range(n)] ki [[] for i in range(n)] 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 d = [0 for i in range(n)] d [0 for i in range(n)] for i in range(n-1):
    a , b = map(lambda x: int(x)-1,input().split())
    ki[a].append(b)
    ki[b].append(a) i range(n-1) range n-1 n 1 a , b = map(lambda x: int(x)-1,input().split()) a map(lambda x: int(x)-1,input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split b ki[a].append(b) [a].append [a] ki a append b ki[b].append(a) [b].append [b] ki 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(reverse=True) c.sort c sort reverse=True reverse True c = deque(c) c deque(c) deque c ans = 0 ans 0 d[0] = c.popleft() d[0] d 0 c.popleft() c.popleft c popleft q = deque() q deque() deque q.append(0) q.append q append 0 while q:
    now = q.popleft()
    for i in ki[now]:
        if d[i] == 0:
            d[i] = c.popleft()
            q.append(i)
            ans += d[i] q now = q.popleft() now q.popleft() q.popleft q popleft for i in ki[now]:
        if d[i] == 0:
            d[i] = c.popleft()
            q.append(i)
            ans += d[i] i ki[now] ki now if d[i] == 0:
            d[i] = c.popleft()
            q.append(i)
            ans += d[i] d[i] == 0 d[i] d i 0 d[i] = c.popleft() d[i] d i c.popleft() c.popleft c popleft q.append(i) q.append q append i ans += d[i] ans d[i] d i print(ans) print ans print(*d) print *d d c = deque(c) deque(c) c ki = [[] for i in range(n)] [[] for i in range(n)] ki a , b = map(lambda x: int(x)-1,input().split()) map(lambda x: int(x)-1,input().split()) a q = deque() deque() q now = q.popleft() q.popleft() now b = map(lambda x: int(x)-1,input().split()) map(lambda x: int(x)-1,input().split()) b d = [0 for i in range(n)] [0 for i in range(n)] d c = list(map(int,input().split())) list(map(int,input().split())) c ans += d[i] d[i] ans ans = 0 0 ans n = int(input()) int(input()) n
from collections import deque
n = int(input())
ki = [[] for i in range(n)]
d = [0 for i in range(n)]
for i in range(n-1):
    a , b = map(lambda x: int(x)-1,input().split())
    ki[a].append(b)
    ki[b].append(a)
c = list(map(int,input().split()))
c.sort(reverse=True)
c = deque(c)
ans = 0
d[0] = c.popleft()
q = deque()
q.append(0)
while q:
    now = q.popleft()
    for i in ki[now]:
        if d[i] == 0:
            d[i] = c.popleft()
            q.append(i)
            ans += d[i]
print(ans)
print(*d)