from collections import defaultdict, deque
d = defaultdict(list)
n = int(input())
for i in range(n-1):
    u, v = map(int, input().split())
    u-=1
    v-=1
    d[u].append(v)
    d[v].append(u)
c = list(map(int, input().split()))
c.sort(reverse = True)
vis = [0 for i in range(n)]
q = deque([])
q.append(0)
vis[0] = c[0]
ind = 1
while(q):
    f = q.popleft()
    for i in d[f]:
        if not vis[i]:
            vis[i] = c[ind]
            ind+=1
            q.append(i)
print(sum(vis) - c[0])
print(*vis) from collections import defaultdict, deque d = defaultdict(list) d defaultdict(list) defaultdict list n = int(input()) n int(input()) int input() input for i in range(n-1):
    u, v = map(int, input().split())
    u-=1
    v-=1
    d[u].append(v)
    d[v].append(u) i range(n-1) range n-1 n 1 u, v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split v u-=1 u 1 v-=1 v 1 d[u].append(v) [u].append [u] d u append v d[v].append(u) [v].append [v] d v append u 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 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 vis = [0 for i in range(n)] vis [0 for i in range(n)] q = deque([]) q deque([]) deque [] q.append(0) q.append q append 0 vis[0] = c[0] vis[0] vis 0 c[0] c 0 ind = 1 ind 1 while(q):
    f = q.popleft()
    for i in d[f]:
        if not vis[i]:
            vis[i] = c[ind]
            ind+=1
            q.append(i) q f = q.popleft() f q.popleft() q.popleft q popleft for i in d[f]:
        if not vis[i]:
            vis[i] = c[ind]
            ind+=1
            q.append(i) i d[f] d f if not vis[i]:
            vis[i] = c[ind]
            ind+=1
            q.append(i) not vis[i] vis[i] vis i vis[i] = c[ind] vis[i] vis i c[ind] c ind ind+=1 ind 1 q.append(i) q.append q append i print(sum(vis) - c[0]) print sum(vis) - c[0] sum(vis) sum vis c[0] c 0 print(*vis) print *vis vis v-=1 1 v vis = [0 for i in range(n)] [0 for i in range(n)] vis f = q.popleft() q.popleft() f u, v = map(int, input().split()) map(int, input().split()) u c = list(map(int, input().split())) list(map(int, input().split())) c d = defaultdict(list) defaultdict(list) d v = map(int, input().split()) map(int, input().split()) v u-=1 1 u n = int(input()) int(input()) n q = deque([]) deque([]) q ind+=1 1 ind ind = 1 1 ind
from collections import defaultdict, deque
d = defaultdict(list)
n = int(input())
for i in range(n-1):
    u, v = map(int, input().split())
    u-=1
    v-=1
    d[u].append(v)
    d[v].append(u)
c = list(map(int, input().split()))
c.sort(reverse = True)
vis = [0 for i in range(n)]
q = deque([])
q.append(0)
vis[0] = c[0]
ind = 1
while(q):
    f = q.popleft()
    for i in d[f]:
        if not vis[i]:
            vis[i] = c[ind]
            ind+=1
            q.append(i)
print(sum(vis) - c[0])
print(*vis)