n=int(input())
ab=[list(map(int,input().split())) for _ in range(n-1)]
c=list(map(int,input().split()))
c.sort()
print(sum(c)-c[-1])
ki=[[] for _ in range(n)]
ki_ji=[[i,0] for i in range(n)]
for a,b in ab:
  a,b=a-1,b-1
  ki[a].append(b)
  ki[b].append(a)
  ki_ji[a][1]+=1
  ki_ji[b][1]+=1
# 1番大きい値の隣には2番目に大きい値を入れたい。
# 次数最大のノードを始点にしてBFS的に値を入れていく。
from collections import deque
ki_ji.sort(key=lambda x:x[1])
seen=[0]*n
t=ki_ji[-1]
todo=deque([t[0]])
seen[t[0]]=c.pop()
while todo:
  t=todo.popleft()
  l=ki[t]
  for li in l:
    if seen[li]==0:
      todo.append(li)
      seen[li]=c.pop()
print(' '.join(map(str,seen)))
#print(ki_ji) n=int(input()) n int(input()) int input() input list(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) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ab=[list(map(int,input().split())) for _ in range(n-1)] ab [list(map(int,input().split())) for _ 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() c.sort c sort print(sum(c)-c[-1]) print sum(c)-c[-1] sum(c) sum c c[-1] c -1 [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] ki=[[] for _ in range(n)] ki [[] for _ in range(n)] [i,0] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [i,0] i 0 ki_ji=[[i,0] for i in range(n)] ki_ji [[i,0] for i in range(n)] for a,b in ab:
  a,b=a-1,b-1
  ki[a].append(b)
  ki[b].append(a)
  ki_ji[a][1]+=1
  ki_ji[b][1]+=1
# 1番大きい値の隣には2番目に大きい値を入れたい。
# 次数最大のノードを始点にしてBFS的に値を入れていく。 a b ab a,b=a-1,b-1 a a-1 a 1 b b-1 b 1 ki[a].append(b) [a].append [a] ki a append b ki[b].append(a) [b].append [b] ki b append a ki_ji[a][1]+=1 ki_ji[a][1] [a] ki_ji a 1 1 ki_ji[b][1]+=1 ki_ji[b][1] [b] ki_ji b 1 1 from collections import deque ki_ji.sort(key=lambda x:x[1]) ki_ji.sort ki_ji sort key=lambda x:x[1] key lambda x:x[1] x[1] x 1 x seen=[0]*n seen [0]*n [0] 0 n t=ki_ji[-1] t ki_ji[-1] ki_ji -1 todo=deque([t[0]]) todo deque([t[0]]) deque [t[0]] t[0] t 0 seen[t[0]]=c.pop() seen[t[0]] seen t[0] t 0 c.pop() c.pop c pop while todo:
  t=todo.popleft()
  l=ki[t]
  for li in l:
    if seen[li]==0:
      todo.append(li)
      seen[li]=c.pop() todo t=todo.popleft() t todo.popleft() todo.popleft todo popleft l=ki[t] l ki[t] ki t for li in l:
    if seen[li]==0:
      todo.append(li)
      seen[li]=c.pop() li l if seen[li]==0:
      todo.append(li)
      seen[li]=c.pop() seen[li]==0 seen[li] seen li 0 todo.append(li) todo.append todo append li seen[li]=c.pop() seen[li] seen li c.pop() c.pop c pop print(' '.join(map(str,seen))) print ' '.join(map(str,seen)) ' '.join ' ' join map(str,seen) map str seen a,b=a-1,b-1 a-1 a ki_ji=[[i,0] for i in range(n)] [[i,0] for i in range(n)] ki_ji todo=deque([t[0]]) deque([t[0]]) todo c=list(map(int,input().split())) list(map(int,input().split())) c ki=[[] for _ in range(n)] [[] for _ in range(n)] ki n=int(input()) int(input()) n b=a-1,b-1 b-1 b seen=[0]*n [0]*n seen t=todo.popleft() todo.popleft() t l=ki[t] ki[t] l ab=[list(map(int,input().split())) for _ in range(n-1)] [list(map(int,input().split())) for _ in range(n-1)] ab t=ki_ji[-1] ki_ji[-1] t
n=int(input())
ab=[list(map(int,input().split())) for _ in range(n-1)]
c=list(map(int,input().split()))
c.sort()
print(sum(c)-c[-1])
ki=[[] for _ in range(n)]
ki_ji=[[i,0] for i in range(n)]
for a,b in ab:
  a,b=a-1,b-1
  ki[a].append(b)
  ki[b].append(a)
  ki_ji[a][1]+=1
  ki_ji[b][1]+=1
# 1番大きい値の隣には2番目に大きい値を入れたい。
# 次数最大のノードを始点にしてBFS的に値を入れていく。
from collections import deque
ki_ji.sort(key=lambda x:x[1])
seen=[0]*n
t=ki_ji[-1]
todo=deque([t[0]])
seen[t[0]]=c.pop()
while todo:
  t=todo.popleft()
  l=ki[t]
  for li in l:
    if seen[li]==0:
      todo.append(li)
      seen[li]=c.pop()
print(' '.join(map(str,seen)))
#print(ki_ji)
