from collections import deque
N=int(input())
tree=[[] for _ in range(N)]
tree2=[]
for i in range(N-1):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
    tree2.append([a-1,b-1])
score=list(map(int,input().split()))
score.sort(reverse=True)
#print(tree,tree2,score)
v=0
d=deque()
seen=[0]*N
d.append(v)
ans=[]
l=[0]*N
s=0
while len(ans)!=N:
    v=d.popleft()
    seen[v]=1
    ans.append(v)
    for j in tree[v]:
        if seen[j]!=1:
            d.append(j)
    #print(ans,seen)
    #input()
for i in range(N):
    l[ans[i]]=score[i]
#print(l)
for i in range(N-1):
    s=s+min(l[tree2[i][0]],l[tree2[i][1]])
print(s)
print(*l,sep=" ")

 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) [] tree=[[] for _ in range(N)] tree [[] for _ in range(N)] tree2=[] tree2 [] for i in range(N-1):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
    tree2.append([a-1,b-1]) 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 tree[a-1].append(b-1) [a-1].append [a-1] tree a-1 a 1 append b-1 b 1 tree[b-1].append(a-1) [b-1].append [b-1] tree b-1 b 1 append a-1 a 1 tree2.append([a-1,b-1]) tree2.append tree2 append [a-1,b-1] a-1 a 1 b-1 b 1 score=list(map(int,input().split())) score list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split score.sort(reverse=True) score.sort score sort reverse=True reverse True v=0 v 0 d=deque() d deque() deque seen=[0]*N seen [0]*N [0] 0 N d.append(v) d.append d append v ans=[] ans [] l=[0]*N l [0]*N [0] 0 N s=0 s 0 while len(ans)!=N:
    v=d.popleft()
    seen[v]=1
    ans.append(v)
    for j in tree[v]:
        if seen[j]!=1:
            d.append(j)
    #print(ans,seen)
    #input() len(ans)!=N len(ans) len ans N v=d.popleft() v d.popleft() d.popleft d popleft seen[v]=1 seen[v] seen v 1 ans.append(v) ans.append ans append v for j in tree[v]:
        if seen[j]!=1:
            d.append(j)
    #print(ans,seen)
    #input() j tree[v] tree v if seen[j]!=1:
            d.append(j)
    #print(ans,seen)
    #input() seen[j]!=1 seen[j] seen j 1 d.append(j) d.append d append j for i in range(N):
    l[ans[i]]=score[i]
#print(l) i range(N) range N l[ans[i]]=score[i] l[ans[i]] l ans[i] ans i score[i] score i for i in range(N-1):
    s=s+min(l[tree2[i][0]],l[tree2[i][1]]) i range(N-1) range N-1 N 1 s=s+min(l[tree2[i][0]],l[tree2[i][1]]) s s+min(l[tree2[i][0]],l[tree2[i][1]]) s min(l[tree2[i][0]],l[tree2[i][1]]) min l[tree2[i][0]] l tree2[i][0] [i] tree2 i 0 l[tree2[i][1]] l tree2[i][1] [i] tree2 i 1 print(s) print s print(*l,sep=" ") print *l l sep=" " sep " " l=[0]*N [0]*N l tree2=[] [] tree2 score=list(map(int,input().split())) list(map(int,input().split())) score ans=[] [] ans s=s+min(l[tree2[i][0]],l[tree2[i][1]]) s+min(l[tree2[i][0]],l[tree2[i][1]]) s N=int(input()) int(input()) N seen=[0]*N [0]*N seen s=0 0 s a,b=map(int,input().split()) map(int,input().split()) a b=map(int,input().split()) map(int,input().split()) b d=deque() deque() d tree=[[] for _ in range(N)] [[] for _ in range(N)] tree v=0 0 v v=d.popleft() d.popleft() v
from collections import deque
N=int(input())
tree=[[] for _ in range(N)]
tree2=[]
for i in range(N-1):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
    tree2.append([a-1,b-1])
score=list(map(int,input().split()))
score.sort(reverse=True)
#print(tree,tree2,score)
v=0
d=deque()
seen=[0]*N
d.append(v)
ans=[]
l=[0]*N
s=0
while len(ans)!=N:
    v=d.popleft()
    seen[v]=1
    ans.append(v)
    for j in tree[v]:
        if seen[j]!=1:
            d.append(j)
    #print(ans,seen)
    #input()
for i in range(N):
    l[ans[i]]=score[i]
#print(l)
for i in range(N-1):
    s=s+min(l[tree2[i][0]],l[tree2[i][1]])
print(s)
print(*l,sep=" ")


