import heapq as h

N=int(input())
hen=[[] for i in range(0,N)]
for i in range(0,N-1):
    a,b=map(int,input().split())
    hen[a-1].append(b-1)
    hen[b-1].append(a-1)

c=list(map(int,input().split()))
c.sort(reverse=True)

ans=[-1 for i in range(0,N)]

q=[]
q.append(0)
h.heapify(q)
sub=[]
h.heapify(sub)
count=0
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        ans[x]=c[count]
        count+=1
        for p in hen[x]:
            if ans[p]==-1:
                h.heappush(sub,p)
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break

print(sum(c)-c[0])
print(*ans) import heapq as h heapq N=int(input()) N int(input()) int input() input [] for i in range(0,N) for i in range(0,N) i range(0,N) range 0 N for i in range(0,N) [] hen=[[] for i in range(0,N)] hen [[] for i in range(0,N)] for i in range(0,N-1):
    a,b=map(int,input().split())
    hen[a-1].append(b-1)
    hen[b-1].append(a-1) i range(0,N-1) range 0 N-1 N 1 a,b=map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b hen[a-1].append(b-1) [a-1].append [a-1] hen a-1 a 1 append b-1 b 1 hen[b-1].append(a-1) [b-1].append [b-1] hen b-1 b 1 append a-1 a 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.sort(reverse=True) c.sort c sort reverse=True reverse True -1 for i in range(0,N) for i in range(0,N) i range(0,N) range 0 N for i in range(0,N) -1 ans=[-1 for i in range(0,N)] ans [-1 for i in range(0,N)] q=[] q [] q.append(0) q.append q append 0 h.heapify(q) h.heapify h heapify q sub=[] sub [] h.heapify(sub) h.heapify h heapify sub count=0 count 0 while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        ans[x]=c[count]
        count+=1
        for p in hen[x]:
            if ans[p]==-1:
                h.heappush(sub,p)
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break q!=[] q [] while q!=[]:
        x=h.heappop(q)
        ans[x]=c[count]
        count+=1
        for p in hen[x]:
            if ans[p]==-1:
                h.heappush(sub,p)
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break q!=[] q [] x=h.heappop(q) x h.heappop(q) h.heappop h heappop q ans[x]=c[count] ans[x] ans x c[count] c count count+=1 count 1 for p in hen[x]:
            if ans[p]==-1:
                h.heappush(sub,p)
        #何らかの操作
        #ここでsubキューに次に使うものを格納
         p hen[x] hen x if ans[p]==-1:
                h.heappush(sub,p)
        #何らかの操作
        #ここでsubキューに次に使うものを格納
         ans[p]==-1 ans[p] ans p -1 h.heappush(sub,p) h.heappush h heappush sub p if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break q==[] q [] q=sub q sub sub=[] sub [] h.heapify(sub) h.heapify h heapify sub break print(sum(c)-c[0]) print sum(c)-c[0] sum(c) sum c c[0] c 0 print(*ans) print *ans ans b=map(int,input().split()) map(int,input().split()) b c=list(map(int,input().split())) list(map(int,input().split())) c x=h.heappop(q) h.heappop(q) x a,b=map(int,input().split()) map(int,input().split()) a N=int(input()) int(input()) N ans=[-1 for i in range(0,N)] [-1 for i in range(0,N)] ans sub=[] [] sub count+=1 1 count count=0 0 count q=[] [] q sub=[] [] sub hen=[[] for i in range(0,N)] [[] for i in range(0,N)] hen q=sub sub q
import heapq as h

N=int(input())
hen=[[] for i in range(0,N)]
for i in range(0,N-1):
    a,b=map(int,input().split())
    hen[a-1].append(b-1)
    hen[b-1].append(a-1)

c=list(map(int,input().split()))
c.sort(reverse=True)

ans=[-1 for i in range(0,N)]

q=[]
q.append(0)
h.heapify(q)
sub=[]
h.heapify(sub)
count=0
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        ans[x]=c[count]
        count+=1
        for p in hen[x]:
            if ans[p]==-1:
                h.heappush(sub,p)
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break

print(sum(c)-c[0])
print(*ans)