from collections import deque
n=int(input())
ans=[0 for i in range(n)]
l=[[] for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a].append(b)
  l[b].append(a)
l1=sorted(list(map(int,input().split())))
l1=l1[::-1]
sum=0
for i in range(1,n):
  sum+=l1[i]
q=deque(l1)
q1=deque([[1]])
while q:
  l2=q1.popleft()
  for i in l2:
    if ans[i-1]!=0:
      pass
    else:
      n2=q.popleft()
      ans[i-1]=n2
      q1.append(l[i])
print(sum)
answ=""
for i in range(n):
  answ+=str(ans[i])+" "
print(answ) from collections import deque n=int(input()) n int(input()) int input() input 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 ans=[0 for i in range(n)] ans [0 for i in range(n)] [] 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) [] l=[[] for i in range(n+1)] l [[] for i in range(n+1)] for i in range(n-1):
  a,b=map(int,input().split())
  l[a].append(b)
  l[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 l[a].append(b) [a].append [a] l a append b l[b].append(a) [b].append [b] l b append a l1=sorted(list(map(int,input().split()))) l1 sorted(list(map(int,input().split()))) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split l1=l1[::-1] l1 l1[::-1] l1 ::-1 -1 sum=0 sum 0 for i in range(1,n):
  sum+=l1[i] i range(1,n) range 1 n sum+=l1[i] sum l1[i] l1 i q=deque(l1) q deque(l1) deque l1 q1=deque([[1]]) q1 deque([[1]]) deque [[1]] [1] 1 while q:
  l2=q1.popleft()
  for i in l2:
    if ans[i-1]!=0:
      pass
    else:
      n2=q.popleft()
      ans[i-1]=n2
      q1.append(l[i]) q l2=q1.popleft() l2 q1.popleft() q1.popleft q1 popleft for i in l2:
    if ans[i-1]!=0:
      pass
    else:
      n2=q.popleft()
      ans[i-1]=n2
      q1.append(l[i]) i l2 if ans[i-1]!=0:
      pass
    else:
      n2=q.popleft()
      ans[i-1]=n2
      q1.append(l[i]) ans[i-1]!=0 ans[i-1] ans i-1 i 1 0 n2=q.popleft() n2 q.popleft() q.popleft q popleft ans[i-1]=n2 ans[i-1] ans i-1 i 1 n2 q1.append(l[i]) q1.append q1 append l[i] l i print(sum) print sum answ="" answ "" for i in range(n):
  answ+=str(ans[i])+" " i range(n) range n answ+=str(ans[i])+" " answ str(ans[i])+" " str(ans[i]) str ans[i] ans i " " print(answ) print answ b=map(int,input().split()) map(int,input().split()) b n=int(input()) int(input()) n l1=l1[::-1] l1[::-1] l1 l=[[] for i in range(n+1)] [[] for i in range(n+1)] l sum+=l1[i] l1[i] sum answ+=str(ans[i])+" " str(ans[i])+" " answ q=deque(l1) deque(l1) q answ="" "" answ l2=q1.popleft() q1.popleft() l2 l1=sorted(list(map(int,input().split()))) sorted(list(map(int,input().split()))) l1 a,b=map(int,input().split()) map(int,input().split()) a sum=0 0 sum q1=deque([[1]]) deque([[1]]) q1 ans=[0 for i in range(n)] [0 for i in range(n)] ans n2=q.popleft() q.popleft() n2
from collections import deque
n=int(input())
ans=[0 for i in range(n)]
l=[[] for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a].append(b)
  l[b].append(a)
l1=sorted(list(map(int,input().split())))
l1=l1[::-1]
sum=0
for i in range(1,n):
  sum+=l1[i]
q=deque(l1)
q1=deque([[1]])
while q:
  l2=q1.popleft()
  for i in l2:
    if ans[i-1]!=0:
      pass
    else:
      n2=q.popleft()
      ans[i-1]=n2
      q1.append(l[i])
print(sum)
answ=""
for i in range(n):
  answ+=str(ans[i])+" "
print(answ)