from collections import *
N=int(input())
AB=[list(map(int,input().split())) for i in range(N-1)]
C=list(map(int,input().split()))
C.sort(reverse=True)
data=[[] for i in range(N+1)]
num=[-1 for i in range(N+1)]
num[1]=C[0]
for a,b in AB:
    data[a].append(b)
    data[b].append(a)
sumc=sum(C[1:])

stack=[1]
stack=deque(stack)
visited=set()
visited.add(1)
idx=1
while stack:
    a=stack.popleft()
    for p in data[a]:
        if not p in visited:
            visited.add(p)
            num[p]=C[idx]
            idx+=1
            stack.append(p)
print(sumc)
print(" ".join([str(num[i]) for i in range(1,N+1)])) from collections import * N=int(input()) N int(input()) int input() input list(map(int,input().split())) 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) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split AB=[list(map(int,input().split())) for i in range(N-1)] AB [list(map(int,input().split())) for i in range(N-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 [] 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) [] data=[[] for i in range(N+1)] data [[] for i in range(N+1)] -1 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) -1 num=[-1 for i in range(N+1)] num [-1 for i in range(N+1)] num[1]=C[0] num[1] num 1 C[0] C 0 for a,b in AB:
    data[a].append(b)
    data[b].append(a) a b AB data[a].append(b) [a].append [a] data a append b data[b].append(a) [b].append [b] data b append a sumc=sum(C[1:]) sumc sum(C[1:]) sum C[1:] C 1: 1 stack=[1] stack [1] 1 stack=deque(stack) stack deque(stack) deque stack visited=set() visited set() set visited.add(1) visited.add visited add 1 idx=1 idx 1 while stack:
    a=stack.popleft()
    for p in data[a]:
        if not p in visited:
            visited.add(p)
            num[p]=C[idx]
            idx+=1
            stack.append(p) stack a=stack.popleft() a stack.popleft() stack.popleft stack popleft for p in data[a]:
        if not p in visited:
            visited.add(p)
            num[p]=C[idx]
            idx+=1
            stack.append(p) p data[a] data a if not p in visited:
            visited.add(p)
            num[p]=C[idx]
            idx+=1
            stack.append(p) not p in visited p in visited p visited visited.add(p) visited.add visited add p num[p]=C[idx] num[p] num p C[idx] C idx idx+=1 idx 1 stack.append(p) stack.append stack append p print(sumc) print sumc str(num[i]) for i in range(1,N+1) for i in range(1,N+1) i range(1,N+1) range 1 N+1 N 1 for i in range(1,N+1) str(num[i]) str num[i] num i print(" ".join([str(num[i]) for i in range(1,N+1)])) print " ".join([str(num[i]) for i in range(1,N+1)]) " ".join " " join [str(num[i]) for i in range(1,N+1)] a=stack.popleft() stack.popleft() a AB=[list(map(int,input().split())) for i in range(N-1)] [list(map(int,input().split())) for i in range(N-1)] AB visited=set() set() visited idx+=1 1 idx N=int(input()) int(input()) N num=[-1 for i in range(N+1)] [-1 for i in range(N+1)] num C=list(map(int,input().split())) list(map(int,input().split())) C data=[[] for i in range(N+1)] [[] for i in range(N+1)] data sumc=sum(C[1:]) sum(C[1:]) sumc stack=deque(stack) deque(stack) stack stack=[1] [1] stack idx=1 1 idx
from collections import *
N=int(input())
AB=[list(map(int,input().split())) for i in range(N-1)]
C=list(map(int,input().split()))
C.sort(reverse=True)
data=[[] for i in range(N+1)]
num=[-1 for i in range(N+1)]
num[1]=C[0]
for a,b in AB:
    data[a].append(b)
    data[b].append(a)
sumc=sum(C[1:])

stack=[1]
stack=deque(stack)
visited=set()
visited.add(1)
idx=1
while stack:
    a=stack.popleft()
    for p in data[a]:
        if not p in visited:
            visited.add(p)
            num[p]=C[idx]
            idx+=1
            stack.append(p)
print(sumc)
print(" ".join([str(num[i]) for i in range(1,N+1)]))
