from collections import deque

n = int(input())
V = [[] for i in range(n+1)]

a = [0]*(n-1)
for i in range(n-1):
    A,B = map(int,input().split())
    V[A].append(B)
    V[B].append(A)

c = list(map(int,input().split()))
c = sorted(c, reverse=True)
ans = sum(c[1:])


L = [0]*n
for i in range(1,n+1):
    L[i-1] = [i, len(V[i])]
    
L.sort(key= lambda val : val[1], reverse=True)

q = deque([])
d = [-1] + [0]*n
j = 0

q.append(L[0][0])
d[L[0][0]] = c[0]
j += 1

while q:
    x = q.popleft()
    for y in V[x]:
        if d[y] <= 0:
            q.append(y)
            d[y] = c[j]
            j += 1

print(ans)
for i in range(1,n+1):
    print(d[i],end="")
    print(" ",end="") from collections import deque n = int(input()) n int(input()) int input() input [] 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) [] V = [[] for i in range(n+1)] V [[] for i in range(n+1)] a = [0]*(n-1) a [0]*(n-1) [0] 0 n-1 n 1 for i in range(n-1):
    A,B = map(int,input().split())
    V[A].append(B)
    V[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 V[A].append(B) [A].append [A] V A append B V[B].append(A) [B].append [B] V 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 = sorted(c, reverse=True) c sorted(c, reverse=True) sorted c reverse=True reverse True ans = sum(c[1:]) ans sum(c[1:]) sum c[1:] c 1: 1 L = [0]*n L [0]*n [0] 0 n for i in range(1,n+1):
    L[i-1] = [i, len(V[i])]
     i range(1,n+1) range 1 n+1 n 1 L[i-1] = [i, len(V[i])] L[i-1] L i-1 i 1 [i, len(V[i])] i len(V[i]) len V[i] V i L.sort(key= lambda val : val[1], reverse=True) L.sort L sort key= lambda val : val[1] key lambda val : val[1] val[1] val 1 val reverse=True reverse True q = deque([]) q deque([]) deque [] d = [-1] + [0]*n d [-1] + [0]*n [-1] -1 [0]*n [0] 0 n j = 0 j 0 q.append(L[0][0]) q.append q append L[0][0] [0] L 0 0 d[L[0][0]] = c[0] d[L[0][0]] d L[0][0] [0] L 0 0 c[0] c 0 j += 1 j 1 while q:
    x = q.popleft()
    for y in V[x]:
        if d[y] <= 0:
            q.append(y)
            d[y] = c[j]
            j += 1 q x = q.popleft() x q.popleft() q.popleft q popleft for y in V[x]:
        if d[y] <= 0:
            q.append(y)
            d[y] = c[j]
            j += 1 y V[x] V x if d[y] <= 0:
            q.append(y)
            d[y] = c[j]
            j += 1 d[y] <= 0 d[y] d y 0 q.append(y) q.append q append y d[y] = c[j] d[y] d y c[j] c j j += 1 j 1 print(ans) print ans for i in range(1,n+1):
    print(d[i],end="")
    print(" ",end="") i range(1,n+1) range 1 n+1 n 1 print(d[i],end="") print d[i] d i end="" end "" print(" ",end="") print " " end="" end "" n = int(input()) int(input()) n c = list(map(int,input().split())) list(map(int,input().split())) c d = [-1] + [0]*n [-1] + [0]*n d a = [0]*(n-1) [0]*(n-1) a L = [0]*n [0]*n L q = deque([]) deque([]) q V = [[] for i in range(n+1)] [[] for i in range(n+1)] V j += 1 1 j c = sorted(c, reverse=True) sorted(c, reverse=True) c j = 0 0 j A,B = map(int,input().split()) map(int,input().split()) A B = map(int,input().split()) map(int,input().split()) B x = q.popleft() q.popleft() x ans = sum(c[1:]) sum(c[1:]) ans j += 1 1 j
from collections import deque

n = int(input())
V = [[] for i in range(n+1)]

a = [0]*(n-1)
for i in range(n-1):
    A,B = map(int,input().split())
    V[A].append(B)
    V[B].append(A)

c = list(map(int,input().split()))
c = sorted(c, reverse=True)
ans = sum(c[1:])


L = [0]*n
for i in range(1,n+1):
    L[i-1] = [i, len(V[i])]
    
L.sort(key= lambda val : val[1], reverse=True)

q = deque([])
d = [-1] + [0]*n
j = 0

q.append(L[0][0])
d[L[0][0]] = c[0]
j += 1

while q:
    x = q.popleft()
    for y in V[x]:
        if d[y] <= 0:
            q.append(y)
            d[y] = c[j]
            j += 1

print(ans)
for i in range(1,n+1):
    print(d[i],end="")
    print(" ",end="")