import sys
input = sys.stdin.readline
from collections import *

def bfs():
    q = deque([0])
    dist = [-1]*N
    dist[0] = 0
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist

N = int(input())
G = [[] for _ in range(N)]

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

c = list(map(int, input().split()))
c.sort(reverse=True)
dist = bfs()
idist = [(i, dist[i]) for i in range(N)]
idist.sort(key=lambda t: t[1])
ans = [-1]*N

for i in range(N):
    ans[idist[i][0]] = c[i]

print(sum(c[1:]))
print(*ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline from collections import * def bfs():
    q = deque([0])
    dist = [-1]*N
    dist[0] = 0
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist bfs q = deque([0]) q deque([0]) deque [0] 0 dist = [-1]*N dist [-1]*N [-1] -1 N dist[0] = 0 dist[0] dist 0 0 while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
     q v = q.popleft() v q.popleft() q.popleft q popleft for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
     nv G[v] G v if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
     dist[nv]==-1 dist[nv] dist nv -1 dist[nv] = dist[v]+1 dist[nv] dist nv dist[v]+1 dist[v] dist v 1 q.append(nv) q.append q append nv return dist dist 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) [] G = [[] for _ in range(N)] G [[] for _ in range(N)] for _ in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1) _ 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 G[a-1].append(b-1) [a-1].append [a-1] G a-1 a 1 append b-1 b 1 G[b-1].append(a-1) [b-1].append [b-1] G 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 dist = bfs() dist bfs() bfs (i, dist[i]) for i in range(N) for i in range(N) i range(N) range N for i in range(N) (i, dist[i]) i dist[i] dist i idist = [(i, dist[i]) for i in range(N)] idist [(i, dist[i]) for i in range(N)] idist.sort(key=lambda t: t[1]) idist.sort idist sort key=lambda t: t[1] key lambda t: t[1] t[1] t 1 t ans = [-1]*N ans [-1]*N [-1] -1 N for i in range(N):
    ans[idist[i][0]] = c[i] i range(N) range N ans[idist[i][0]] = c[i] ans[idist[i][0]] ans idist[i][0] [i] idist i 0 c[i] c i print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(*ans) print *ans ans b = map(int, input().split()) map(int, input().split()) b ans = [-1]*N [-1]*N ans idist = [(i, dist[i]) for i in range(N)] [(i, dist[i]) for i in range(N)] idist input = sys.stdin.readline sys.stdin.readline input c = list(map(int, input().split())) list(map(int, input().split())) c G = [[] for _ in range(N)] [[] for _ in range(N)] G a, b = map(int, input().split()) map(int, input().split()) a def bfs():
    q = deque([0])
    dist = [-1]*N
    dist[0] = 0
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist def bfs():
    q = deque([0])
    dist = [-1]*N
    dist[0] = 0
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist bfs N = int(input()) int(input()) N dist = bfs() bfs() dist
import sys
input = sys.stdin.readline
from collections import *

def bfs():
    q = deque([0])
    dist = [-1]*N
    dist[0] = 0
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist

N = int(input())
G = [[] for _ in range(N)]

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

c = list(map(int, input().split()))
c.sort(reverse=True)
dist = bfs()
idist = [(i, dist[i]) for i in range(N)]
idist.sort(key=lambda t: t[1])
ans = [-1]*N

for i in range(N):
    ans[idist[i][0]] = c[i]

print(sum(c[1:]))
print(*ans)