from collections import deque
N=int(input())
AB=[tuple(map(int, input().split())) for _ in range(N-1)]
C=sorted(map(int, input().split()))
G=[[] for _ in range(N)]
for a,b in AB:
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)
deq=deque(i for i in range(N) if len(G[i])==1)
D=[len(G[i]) for i in range(N)]
X=[0]*N
for c in C:
    i=deq.popleft()
    X[i]=c
    for j in G[i]:
        D[j]-=1
        if D[j]==1:
            deq.append(j)
ans=0
for a,b in AB:
    ans+=min(X[a-1],X[b-1])
print(ans)
print(*X) from collections import deque N=int(input()) N int(input()) int input() input tuple(map(int, 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) tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split AB=[tuple(map(int, input().split())) for _ in range(N-1)] AB [tuple(map(int, input().split())) for _ in range(N-1)] C=sorted(map(int, input().split())) C sorted(map(int, input().split())) sorted map(int, input().split()) map int input().split() ().split () input split [] 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 a,b in AB:
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a) a b AB a-=1 a 1 b-=1 b 1 G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a deq=deque(i for i in range(N) if len(G[i])==1) deq deque(i for i in range(N) if len(G[i])==1) deque i len(G[i]) for i in range(N) for i in range(N) i range(N) range N for i in range(N) len(G[i]) len G[i] G i D=[len(G[i]) for i in range(N)] D [len(G[i]) for i in range(N)] X=[0]*N X [0]*N [0] 0 N for c in C:
    i=deq.popleft()
    X[i]=c
    for j in G[i]:
        D[j]-=1
        if D[j]==1:
            deq.append(j) c C i=deq.popleft() i deq.popleft() deq.popleft deq popleft X[i]=c X[i] X i c for j in G[i]:
        D[j]-=1
        if D[j]==1:
            deq.append(j) j G[i] G i D[j]-=1 D[j] D j 1 if D[j]==1:
            deq.append(j) D[j]==1 D[j] D j 1 deq.append(j) deq.append deq append j ans=0 ans 0 for a,b in AB:
    ans+=min(X[a-1],X[b-1]) a b AB ans+=min(X[a-1],X[b-1]) ans min(X[a-1],X[b-1]) min X[a-1] X a-1 a 1 X[b-1] X b-1 b 1 print(ans) print ans print(*X) print *X X ans+=min(X[a-1],X[b-1]) min(X[a-1],X[b-1]) ans G=[[] for _ in range(N)] [[] for _ in range(N)] G a-=1 1 a b-=1 1 b ans=0 0 ans AB=[tuple(map(int, input().split())) for _ in range(N-1)] [tuple(map(int, input().split())) for _ in range(N-1)] AB deq=deque(i for i in range(N) if len(G[i])==1) deque(i for i in range(N) if len(G[i])==1) deq i=deq.popleft() deq.popleft() i N=int(input()) int(input()) N C=sorted(map(int, input().split())) sorted(map(int, input().split())) C D=[len(G[i]) for i in range(N)] [len(G[i]) for i in range(N)] D X=[0]*N [0]*N X
from collections import deque
N=int(input())
AB=[tuple(map(int, input().split())) for _ in range(N-1)]
C=sorted(map(int, input().split()))
G=[[] for _ in range(N)]
for a,b in AB:
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)
deq=deque(i for i in range(N) if len(G[i])==1)
D=[len(G[i]) for i in range(N)]
X=[0]*N
for c in C:
    i=deq.popleft()
    X[i]=c
    for j in G[i]:
        D[j]-=1
        if D[j]==1:
            deq.append(j)
ans=0
for a,b in AB:
    ans+=min(X[a-1],X[b-1])
print(ans)
print(*X)