from collections import deque
N = int(input())
ad_ls = [[] for _ in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    ad_ls[a].append(b)
    ad_ls[b].append(a)
c_ls = list(map(int, input().split()))
c_ls.sort(reverse=True)
c_q = deque(c_ls)

# d_ls[i] := 0idxでi番目の頂点に書く数字
d_ls = [0] * N

q = deque()
q.append(0)
d_ls[0] = c_q.popleft()
while q:
    now = q.popleft()
    for nex in ad_ls[now]:
        if d_ls[nex] == 0:
            d_ls[nex] = c_q.popleft()
            q.append(nex)

print(sum(c_ls) - max(c_ls))
print(*d_ls) from collections import deque N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] ad_ls = [[] for _ in range(N)] ad_ls [[] for _ in range(N)] for i in range(N-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    ad_ls[a].append(b)
    ad_ls[b].append(a) 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 a,b = a-1,b-1 a a-1 a 1 b b-1 b 1 ad_ls[a].append(b) [a].append [a] ad_ls a append b ad_ls[b].append(a) [b].append [b] ad_ls b append a c_ls = list(map(int, input().split())) c_ls list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c_ls.sort(reverse=True) c_ls.sort c_ls sort reverse=True reverse True c_q = deque(c_ls) c_q deque(c_ls) deque c_ls d_ls = [0] * N d_ls [0] * N [0] 0 N q = deque() q deque() deque q.append(0) q.append q append 0 d_ls[0] = c_q.popleft() d_ls[0] d_ls 0 c_q.popleft() c_q.popleft c_q popleft while q:
    now = q.popleft()
    for nex in ad_ls[now]:
        if d_ls[nex] == 0:
            d_ls[nex] = c_q.popleft()
            q.append(nex) q now = q.popleft() now q.popleft() q.popleft q popleft for nex in ad_ls[now]:
        if d_ls[nex] == 0:
            d_ls[nex] = c_q.popleft()
            q.append(nex) nex ad_ls[now] ad_ls now if d_ls[nex] == 0:
            d_ls[nex] = c_q.popleft()
            q.append(nex) d_ls[nex] == 0 d_ls[nex] d_ls nex 0 d_ls[nex] = c_q.popleft() d_ls[nex] d_ls nex c_q.popleft() c_q.popleft c_q popleft q.append(nex) q.append q append nex print(sum(c_ls) - max(c_ls)) print sum(c_ls) - max(c_ls) sum(c_ls) sum c_ls max(c_ls) max c_ls print(*d_ls) print *d_ls d_ls a,b = a-1,b-1 a-1 a N = int(input()) int(input()) N d_ls = [0] * N [0] * N d_ls ad_ls = [[] for _ in range(N)] [[] for _ in range(N)] ad_ls a,b = map(int,input().split()) map(int,input().split()) a c_q = deque(c_ls) deque(c_ls) c_q b = map(int,input().split()) map(int,input().split()) b b = a-1,b-1 b-1 b c_ls = list(map(int, input().split())) list(map(int, input().split())) c_ls q = deque() deque() q now = q.popleft() q.popleft() now
from collections import deque
N = int(input())
ad_ls = [[] for _ in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    ad_ls[a].append(b)
    ad_ls[b].append(a)
c_ls = list(map(int, input().split()))
c_ls.sort(reverse=True)
c_q = deque(c_ls)

# d_ls[i] := 0idxでi番目の頂点に書く数字
d_ls = [0] * N

q = deque()
q.append(0)
d_ls[0] = c_q.popleft()
while q:
    now = q.popleft()
    for nex in ad_ls[now]:
        if d_ls[nex] == 0:
            d_ls[nex] = c_q.popleft()
            q.append(nex)

print(sum(c_ls) - max(c_ls))
print(*d_ls)