#D問題
import heapq
N = int(input())
AB = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    AB[a].append(b)
    AB[b].append(a)
    
C = list(map(int,input().split()))
C.sort(reverse=True)

var = [0 for i in range(N)]
var[0] = C[0]
Q = []
heapq.heappush(Q,0)
ind = 1

for i in range(N-1):
    q = heapq.heappop(Q)
    for j in AB[q]:
        if var[j] == 0:
            var[j] = C[ind]
            ind+=1
            heapq.heappush(Q,j)
            
print(sum(C)-C[0])
for v in var:
    print(v,end=" ") import heapq heapq N = int(input()) N int(input()) int input() input [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] AB = [[] for i in range(N)] AB [[] for i in range(N)] for i in range(N-1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    AB[a].append(b)
    AB[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-=1 a 1 b-=1 b 1 AB[a].append(b) [a].append [a] AB a append b AB[b].append(a) [b].append [b] AB b append a 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 var = [0 for i in range(N)] var [0 for i in range(N)] var[0] = C[0] var[0] var 0 C[0] C 0 Q = [] Q [] heapq.heappush(Q,0) heapq.heappush heapq heappush Q 0 ind = 1 ind 1 for i in range(N-1):
    q = heapq.heappop(Q)
    for j in AB[q]:
        if var[j] == 0:
            var[j] = C[ind]
            ind+=1
            heapq.heappush(Q,j)
             i range(N-1) range N-1 N 1 q = heapq.heappop(Q) q heapq.heappop(Q) heapq.heappop heapq heappop Q for j in AB[q]:
        if var[j] == 0:
            var[j] = C[ind]
            ind+=1
            heapq.heappush(Q,j)
             j AB[q] AB q if var[j] == 0:
            var[j] = C[ind]
            ind+=1
            heapq.heappush(Q,j)
             var[j] == 0 var[j] var j 0 var[j] = C[ind] var[j] var j C[ind] C ind ind+=1 ind 1 heapq.heappush(Q,j) heapq.heappush heapq heappush Q j print(sum(C)-C[0]) print sum(C)-C[0] sum(C) sum C C[0] C 0 for v in var:
    print(v,end=" ") v var print(v,end=" ") print v end=" " end " " Q = [] [] Q N = int(input()) int(input()) N var = [0 for i in range(N)] [0 for i in range(N)] var ind+=1 1 ind AB = [[] for i in range(N)] [[] for i in range(N)] AB ind = 1 1 ind C = list(map(int,input().split())) list(map(int,input().split())) C b = map(int,input().split()) map(int,input().split()) b a,b = map(int,input().split()) map(int,input().split()) a b-=1 1 b q = heapq.heappop(Q) heapq.heappop(Q) q a-=1 1 a
#D問題
import heapq
N = int(input())
AB = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    AB[a].append(b)
    AB[b].append(a)
    
C = list(map(int,input().split()))
C.sort(reverse=True)

var = [0 for i in range(N)]
var[0] = C[0]
Q = []
heapq.heappush(Q,0)
ind = 1

for i in range(N-1):
    q = heapq.heappop(Q)
    for j in AB[q]:
        if var[j] == 0:
            var[j] = C[ind]
            ind+=1
            heapq.heappush(Q,j)
            
print(sum(C)-C[0])
for v in var:
    print(v,end=" ")
