from collections import deque
N=int(input())
ab=list(list(map(int,input().split()))for _ in range(N-1))
c=list(map(int,input().split()))
c.sort(reverse=True)
graph=[[] for _ in range(N+1)]
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
queue=deque([1])
ans=[[] for _ in range(N+1)]
sumans=-c[0]
c=deque(c)
while len(queue)>0:
    x=queue.popleft()
    y=c.popleft()
    ans[x].append(y)
    sumans+=y
    for i in graph[x]:
        if ans[i]==[]:
            queue.append(i)
print(sumans)
for j in ans[1:]:
    print(*j,end=' ') from collections import deque N=int(input()) N int(input()) int input() input ab=list(list(map(int,input().split()))for _ in range(N-1)) ab list(list(map(int,input().split()))for _ in range(N-1)) list list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 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 _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [] graph=[[] for _ in range(N+1)] graph [[] for _ in range(N+1)] for a,b in ab:
    graph[a].append(b)
    graph[b].append(a) a b ab graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a queue=deque([1]) queue deque([1]) deque [1] 1 [] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [] ans=[[] for _ in range(N+1)] ans [[] for _ in range(N+1)] sumans=-c[0] sumans -c[0] c[0] c 0 c=deque(c) c deque(c) deque c while len(queue)>0:
    x=queue.popleft()
    y=c.popleft()
    ans[x].append(y)
    sumans+=y
    for i in graph[x]:
        if ans[i]==[]:
            queue.append(i) len(queue)>0 len(queue) len queue 0 x=queue.popleft() x queue.popleft() queue.popleft queue popleft y=c.popleft() y c.popleft() c.popleft c popleft ans[x].append(y) [x].append [x] ans x append y sumans+=y sumans y for i in graph[x]:
        if ans[i]==[]:
            queue.append(i) i graph[x] graph x if ans[i]==[]:
            queue.append(i) ans[i]==[] ans[i] ans i [] queue.append(i) queue.append queue append i print(sumans) print sumans for j in ans[1:]:
    print(*j,end=' ') j ans[1:] ans 1: 1 print(*j,end=' ') print *j j end=' ' end ' ' x=queue.popleft() queue.popleft() x queue=deque([1]) deque([1]) queue sumans+=y y sumans c=list(map(int,input().split())) list(map(int,input().split())) c ab=list(list(map(int,input().split()))for _ in range(N-1)) list(list(map(int,input().split()))for _ in range(N-1)) ab c=deque(c) deque(c) c N=int(input()) int(input()) N graph=[[] for _ in range(N+1)] [[] for _ in range(N+1)] graph ans=[[] for _ in range(N+1)] [[] for _ in range(N+1)] ans sumans=-c[0] -c[0] sumans y=c.popleft() c.popleft() y
from collections import deque
N=int(input())
ab=list(list(map(int,input().split()))for _ in range(N-1))
c=list(map(int,input().split()))
c.sort(reverse=True)
graph=[[] for _ in range(N+1)]
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
queue=deque([1])
ans=[[] for _ in range(N+1)]
sumans=-c[0]
c=deque(c)
while len(queue)>0:
    x=queue.popleft()
    y=c.popleft()
    ans[x].append(y)
    sumans+=y
    for i in graph[x]:
        if ans[i]==[]:
            queue.append(i)
print(sumans)
for j in ans[1:]:
    print(*j,end=' ')
