from collections import deque

N = int(input())
ab = [[int(i)-1 for i in input().split()] for _ in range(N-1)]
c = [int(i) for i in input().split()]
                
c.sort()
d = [0] * N

vector = [[] for _ in range(N)]
for a, b in ab :
    vector[a].append(b)
    vector[b].append(a)

ret = sum(c[:-1]) 
queue = deque([0])
visited = [False] * N
visited[0] = True

while queue :
    q = queue.popleft()
    for v in vector[q] :
        if not visited[v] :
            queue.append(v)
            visited[v] = True
    d[q] = c.pop()
    
print(ret)
print(' '.join(map(str, d))) from collections import deque N = int(input()) N int(input()) int input() input [int(i)-1 for i in input().split()] for _ in range(N-1) for _ in range(N-1) _ range(N-1) range N-1 N 1 for _ in range(N-1) [int(i)-1 for i in input().split()] ab = [[int(i)-1 for i in input().split()] for _ in range(N-1)] ab [[int(i)-1 for i in input().split()] for _ in range(N-1)] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i c = [int(i) for i in input().split()] c [int(i) for i in input().split()] c.sort() c.sort c sort d = [0] * N d [0] * N [0] 0 N [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] vector = [[] for _ in range(N)] vector [[] for _ in range(N)] for a, b in ab :
    vector[a].append(b)
    vector[b].append(a) a b ab vector[a].append(b) [a].append [a] vector a append b vector[b].append(a) [b].append [b] vector b append a ret = sum(c[:-1]) ret sum(c[:-1]) sum c[:-1] c :-1 -1 queue = deque([0]) queue deque([0]) deque [0] 0 visited = [False] * N visited [False] * N [False] False N visited[0] = True visited[0] visited 0 True while queue :
    q = queue.popleft()
    for v in vector[q] :
        if not visited[v] :
            queue.append(v)
            visited[v] = True
    d[q] = c.pop()
     queue q = queue.popleft() q queue.popleft() queue.popleft queue popleft for v in vector[q] :
        if not visited[v] :
            queue.append(v)
            visited[v] = True
     v vector[q] vector q if not visited[v] :
            queue.append(v)
            visited[v] = True
     not visited[v] visited[v] visited v queue.append(v) queue.append queue append v visited[v] = True visited[v] visited v True d[q] = c.pop() d[q] d q c.pop() c.pop c pop print(ret) print ret print(' '.join(map(str, d))) print ' '.join(map(str, d)) ' '.join ' ' join map(str, d) map str d vector = [[] for _ in range(N)] [[] for _ in range(N)] vector N = int(input()) int(input()) N d = [0] * N [0] * N d ab = [[int(i)-1 for i in input().split()] for _ in range(N-1)] [[int(i)-1 for i in input().split()] for _ in range(N-1)] ab c = [int(i) for i in input().split()] [int(i) for i in input().split()] c queue = deque([0]) deque([0]) queue visited = [False] * N [False] * N visited q = queue.popleft() queue.popleft() q ret = sum(c[:-1]) sum(c[:-1]) ret
from collections import deque

N = int(input())
ab = [[int(i)-1 for i in input().split()] for _ in range(N-1)]
c = [int(i) for i in input().split()]
                
c.sort()
d = [0] * N

vector = [[] for _ in range(N)]
for a, b in ab :
    vector[a].append(b)
    vector[b].append(a)

ret = sum(c[:-1]) 
queue = deque([0])
visited = [False] * N
visited[0] = True

while queue :
    q = queue.popleft()
    for v in vector[q] :
        if not visited[v] :
            queue.append(v)
            visited[v] = True
    d[q] = c.pop()
    
print(ret)
print(' '.join(map(str, d)))