from collections import deque


n = int(input())
info = [list(map(int, input().split())) for i in range(n - 1)]
c = list(map(int, input().split()))
c = sorted(c, reverse=True)

tree = [[] for i in range(n)]
cnt = [0] * n
for i in range(n - 1):
    a, b = info[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)
    cnt[a] += 1
    cnt[b] += 1

for i in range(n):
    if len(tree[i]) == 1:
        root = i
        break
        
q = deque([root])
visited = [False] * n
visited[root] = True
ind = 0
ans = 0
res = [0] * n
while q:
    v= q.pop()
    tmp_cnt = 0
    for nxt_v in tree[v]:
        if visited[nxt_v]:
            tmp_cnt += 1
    ans += c[ind] * tmp_cnt
    res[v] = c[ind]
    ind += 1
    for nxt_v in tree[v]:
        if visited[nxt_v]:
            continue
        visited[nxt_v] = True
        q.append(nxt_v)
    
print(ans)
print(*res) from collections import deque n = int(input()) n int(input()) int input() input list(map(int, input().split())) for i in range(n - 1) for i in range(n - 1) i range(n - 1) range n - 1 n 1 for i in range(n - 1) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split info = [list(map(int, input().split())) for i in range(n - 1)] info [list(map(int, input().split())) for i in range(n - 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 = sorted(c, reverse=True) c sorted(c, reverse=True) sorted c reverse=True reverse True [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] tree = [[] for i in range(n)] tree [[] for i in range(n)] cnt = [0] * n cnt [0] * n [0] 0 n for i in range(n - 1):
    a, b = info[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)
    cnt[a] += 1
    cnt[b] += 1 i range(n - 1) range n - 1 n 1 a, b = info[i] a info[i] info i b a -= 1 a 1 b -= 1 b 1 tree[a].append(b) [a].append [a] tree a append b tree[b].append(a) [b].append [b] tree b append a cnt[a] += 1 cnt[a] cnt a 1 cnt[b] += 1 cnt[b] cnt b 1 for i in range(n):
    if len(tree[i]) == 1:
        root = i
        break
         i range(n) range n if len(tree[i]) == 1:
        root = i
        break
         len(tree[i]) == 1 len(tree[i]) len tree[i] tree i 1 root = i root i break q = deque([root]) q deque([root]) deque [root] root visited = [False] * n visited [False] * n [False] False n visited[root] = True visited[root] visited root True ind = 0 ind 0 ans = 0 ans 0 res = [0] * n res [0] * n [0] 0 n while q:
    v= q.pop()
    tmp_cnt = 0
    for nxt_v in tree[v]:
        if visited[nxt_v]:
            tmp_cnt += 1
    ans += c[ind] * tmp_cnt
    res[v] = c[ind]
    ind += 1
    for nxt_v in tree[v]:
        if visited[nxt_v]:
            continue
        visited[nxt_v] = True
        q.append(nxt_v)
     q v= q.pop() v q.pop() q.pop q pop tmp_cnt = 0 tmp_cnt 0 for nxt_v in tree[v]:
        if visited[nxt_v]:
            tmp_cnt += 1
     nxt_v tree[v] tree v if visited[nxt_v]:
            tmp_cnt += 1
     visited[nxt_v] visited nxt_v tmp_cnt += 1 tmp_cnt 1 ans += c[ind] * tmp_cnt ans c[ind] * tmp_cnt c[ind] c ind tmp_cnt res[v] = c[ind] res[v] res v c[ind] c ind ind += 1 ind 1 for nxt_v in tree[v]:
        if visited[nxt_v]:
            continue
        visited[nxt_v] = True
        q.append(nxt_v)
     nxt_v tree[v] tree v if visited[nxt_v]:
            continue
         visited[nxt_v] visited nxt_v continue visited[nxt_v] = True visited[nxt_v] visited nxt_v True q.append(nxt_v) q.append q append nxt_v print(ans) print ans print(*res) print *res res b -= 1 1 b ans += c[ind] * tmp_cnt c[ind] * tmp_cnt ans tmp_cnt = 0 0 tmp_cnt c = list(map(int, input().split())) list(map(int, input().split())) c root = i i root visited = [False] * n [False] * n visited res = [0] * n [0] * n res tmp_cnt += 1 1 tmp_cnt ind += 1 1 ind ind = 0 0 ind a, b = info[i] info[i] a n = int(input()) int(input()) n b = info[i] info[i] b cnt = [0] * n [0] * n cnt a -= 1 1 a ans = 0 0 ans info = [list(map(int, input().split())) for i in range(n - 1)] [list(map(int, input().split())) for i in range(n - 1)] info tree = [[] for i in range(n)] [[] for i in range(n)] tree q = deque([root]) deque([root]) q v= q.pop() q.pop() v c = sorted(c, reverse=True) sorted(c, reverse=True) c
from collections import deque


n = int(input())
info = [list(map(int, input().split())) for i in range(n - 1)]
c = list(map(int, input().split()))
c = sorted(c, reverse=True)

tree = [[] for i in range(n)]
cnt = [0] * n
for i in range(n - 1):
    a, b = info[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)
    cnt[a] += 1
    cnt[b] += 1

for i in range(n):
    if len(tree[i]) == 1:
        root = i
        break
        
q = deque([root])
visited = [False] * n
visited[root] = True
ind = 0
ans = 0
res = [0] * n
while q:
    v= q.pop()
    tmp_cnt = 0
    for nxt_v in tree[v]:
        if visited[nxt_v]:
            tmp_cnt += 1
    ans += c[ind] * tmp_cnt
    res[v] = c[ind]
    ind += 1
    for nxt_v in tree[v]:
        if visited[nxt_v]:
            continue
        visited[nxt_v] = True
        q.append(nxt_v)
    
print(ans)
print(*res)