from collections import deque

N = int(input())

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

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

stack=deque([0])
check = [-1]*N
check[0] = 0

while len(stack)>0:
    v = stack.popleft()
    for i in root[v]:
        if check[i] == -1:
            check[i]=check[v]+1
            stack.append(i)

keyCheck = [(i,check[i]) for i in range(N)]
keyCheck.sort(key=lambda t:t[1])
#print(keyCheck)
ans = [-1]*N
for i in range(N):
    ans[keyCheck[i][0]] = c[i]

print(sum(c[1:]))
print(*ans) from collections import deque 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) [] root = [[] for i in range(N)] root [[] for i in range(N)] for _ in range(N-1):
    a, b = (int(x) for x in input().split())
    root[b-1].append(a-1)
    root[a-1].append(b-1) _ range(N-1) range N-1 N 1 int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x a, b = (int(x) for x in input().split()) a (int(x) for x in input().split()) b root[b-1].append(a-1) [b-1].append [b-1] root b-1 b 1 append a-1 a 1 root[a-1].append(b-1) [a-1].append [a-1] root a-1 a 1 append b-1 b 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 stack=deque([0]) stack deque([0]) deque [0] 0 check = [-1]*N check [-1]*N [-1] -1 N check[0] = 0 check[0] check 0 0 while len(stack)>0:
    v = stack.popleft()
    for i in root[v]:
        if check[i] == -1:
            check[i]=check[v]+1
            stack.append(i) len(stack)>0 len(stack) len stack 0 v = stack.popleft() v stack.popleft() stack.popleft stack popleft for i in root[v]:
        if check[i] == -1:
            check[i]=check[v]+1
            stack.append(i) i root[v] root v if check[i] == -1:
            check[i]=check[v]+1
            stack.append(i) check[i] == -1 check[i] check i -1 check[i]=check[v]+1 check[i] check i check[v]+1 check[v] check v 1 stack.append(i) stack.append stack append i (i,check[i]) for i in range(N) for i in range(N) i range(N) range N for i in range(N) (i,check[i]) i check[i] check i keyCheck = [(i,check[i]) for i in range(N)] keyCheck [(i,check[i]) for i in range(N)] keyCheck.sort(key=lambda t:t[1]) keyCheck.sort keyCheck 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[keyCheck[i][0]] = c[i] i range(N) range N ans[keyCheck[i][0]] = c[i] ans[keyCheck[i][0]] ans keyCheck[i][0] [i] keyCheck 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 ans = [-1]*N [-1]*N ans N = int(input()) int(input()) N root = [[] for i in range(N)] [[] for i in range(N)] root c = list(map(int,input().split())) list(map(int,input().split())) c v = stack.popleft() stack.popleft() v a, b = (int(x) for x in input().split()) (int(x) for x in input().split()) a check = [-1]*N [-1]*N check keyCheck = [(i,check[i]) for i in range(N)] [(i,check[i]) for i in range(N)] keyCheck stack=deque([0]) deque([0]) stack b = (int(x) for x in input().split()) (int(x) for x in input().split()) b
from collections import deque

N = int(input())

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

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

stack=deque([0])
check = [-1]*N
check[0] = 0

while len(stack)>0:
    v = stack.popleft()
    for i in root[v]:
        if check[i] == -1:
            check[i]=check[v]+1
            stack.append(i)

keyCheck = [(i,check[i]) for i in range(N)]
keyCheck.sort(key=lambda t:t[1])
#print(keyCheck)
ans = [-1]*N
for i in range(N):
    ans[keyCheck[i][0]] = c[i]

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