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