from collections import deque
N=int(input())
A=[0 for i in range(N-1)]
B=[0 for i in range(N-1)]
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a-=1;b-=1
    A[i],B[i]=a,b
    G[a].append(b)
    G[b].append(a)
j=0
for i in range(N):
    if len(G[i])>len(G[j]):
        j=i
c=sorted([int(i) for i in input().split()])
d=[-1 for i in range(N)]
q=deque([j])
while(len(q)>0):
    r=q.popleft()
    d[r]=c.pop()
    for p in G[r]:
        if d[p]==-1:
            q.appendleft(p)
M=0
for i in range(N-1):
    M+=min(d[A[i]],d[B[i]])
print(M)
print(" ".join([str(i) for i in d])) from collections import deque N=int(input()) N int(input()) int input() input 0 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) 0 A=[0 for i in range(N-1)] A [0 for i in range(N-1)] 0 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) 0 B=[0 for i in range(N-1)] B [0 for i in range(N-1)] [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] G=[[] for i in range(N)] G [[] for i in range(N)] for i in range(N-1):
    a,b=map(int,input().split())
    a-=1;b-=1
    A[i],B[i]=a,b
    G[a].append(b)
    G[b].append(a) i range(N-1) range N-1 N 1 a,b=map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b a-=1 a 1 b-=1 b 1 A[i],B[i]=a,b A[i] A i a B[i] B i b G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a j=0 j 0 for i in range(N):
    if len(G[i])>len(G[j]):
        j=i i range(N) range N if len(G[i])>len(G[j]):
        j=i len(G[i])>len(G[j]) len(G[i]) len G[i] G i len(G[j]) len G[j] G j j=i j i 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=sorted([int(i) for i in input().split()]) c sorted([int(i) for i in input().split()]) sorted [int(i) for i in input().split()] -1 for i in range(N) for i in range(N) i range(N) range N for i in range(N) -1 d=[-1 for i in range(N)] d [-1 for i in range(N)] q=deque([j]) q deque([j]) deque [j] j while(len(q)>0):
    r=q.popleft()
    d[r]=c.pop()
    for p in G[r]:
        if d[p]==-1:
            q.appendleft(p) len(q)>0 len(q) len q 0 r=q.popleft() r q.popleft() q.popleft q popleft d[r]=c.pop() d[r] d r c.pop() c.pop c pop for p in G[r]:
        if d[p]==-1:
            q.appendleft(p) p G[r] G r if d[p]==-1:
            q.appendleft(p) d[p]==-1 d[p] d p -1 q.appendleft(p) q.appendleft q appendleft p M=0 M 0 for i in range(N-1):
    M+=min(d[A[i]],d[B[i]]) i range(N-1) range N-1 N 1 M+=min(d[A[i]],d[B[i]]) M min(d[A[i]],d[B[i]]) min d[A[i]] d A[i] A i d[B[i]] d B[i] B i print(M) print M str(i) for i in d for i in d i d for i in d str(i) str i print(" ".join([str(i) for i in d])) print " ".join([str(i) for i in d]) " ".join " " join [str(i) for i in d] d=[-1 for i in range(N)] [-1 for i in range(N)] d a-=1 1 a N=int(input()) int(input()) N j=i i j j=0 0 j r=q.popleft() q.popleft() r b=map(int,input().split()) map(int,input().split()) b b-=1 1 b A=[0 for i in range(N-1)] [0 for i in range(N-1)] A c=sorted([int(i) for i in input().split()]) sorted([int(i) for i in input().split()]) c q=deque([j]) deque([j]) q G=[[] for i in range(N)] [[] for i in range(N)] G B=[0 for i in range(N-1)] [0 for i in range(N-1)] B a,b=map(int,input().split()) map(int,input().split()) a M=0 0 M M+=min(d[A[i]],d[B[i]]) min(d[A[i]],d[B[i]]) M
from collections import deque
N=int(input())
A=[0 for i in range(N-1)]
B=[0 for i in range(N-1)]
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a-=1;b-=1
    A[i],B[i]=a,b
    G[a].append(b)
    G[b].append(a)
j=0
for i in range(N):
    if len(G[i])>len(G[j]):
        j=i
c=sorted([int(i) for i in input().split()])
d=[-1 for i in range(N)]
q=deque([j])
while(len(q)>0):
    r=q.popleft()
    d[r]=c.pop()
    for p in G[r]:
        if d[p]==-1:
            q.appendleft(p)
M=0
for i in range(N-1):
    M+=min(d[A[i]],d[B[i]])
print(M)
print(" ".join([str(i) for i in d]))
