import collections
n=int(input())
graph=[]
for i in range(n+1):
  graph.append([])
for i in range(n-1):
  a,b=map(int,input().split())
  graph[a].append(b)
  graph[b].append(a)
c=list(map(int,input().split()))
c.sort()
c.reverse()
ans=['0']*n
pointer=0
queue=collections.deque([[1,1]])
while queue:
  a=queue.popleft()
  ans[a[0]-1]=str(c[pointer])
  for i in graph[a[0]]:
    if i==a[1]:
      continue
    queue.append([i,a[0]])
  pointer+=1
print(sum(c)-c[0])
print(' '.join(ans)) import collections collections n=int(input()) n int(input()) int input() input graph=[] graph [] for i in range(n+1):
  graph.append([]) i range(n+1) range n+1 n 1 graph.append([]) graph.append graph append [] for i in range(n-1):
  a,b=map(int,input().split())
  graph[a].append(b)
  graph[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 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a 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 c.reverse() c.reverse c reverse ans=['0']*n ans ['0']*n ['0'] '0' n pointer=0 pointer 0 queue=collections.deque([[1,1]]) queue collections.deque([[1,1]]) collections.deque collections deque [[1,1]] [1,1] 1 1 while queue:
  a=queue.popleft()
  ans[a[0]-1]=str(c[pointer])
  for i in graph[a[0]]:
    if i==a[1]:
      continue
    queue.append([i,a[0]])
  pointer+=1 queue a=queue.popleft() a queue.popleft() queue.popleft queue popleft ans[a[0]-1]=str(c[pointer]) ans[a[0]-1] ans a[0]-1 a[0] a 0 1 str(c[pointer]) str c[pointer] c pointer for i in graph[a[0]]:
    if i==a[1]:
      continue
    queue.append([i,a[0]])
   i graph[a[0]] graph a[0] a 0 if i==a[1]:
      continue
     i==a[1] i a[1] a 1 continue queue.append([i,a[0]]) queue.append queue append [i,a[0]] i a[0] a 0 pointer+=1 pointer 1 print(sum(c)-c[0]) print sum(c)-c[0] sum(c) sum c c[0] c 0 print(' '.join(ans)) print ' '.join(ans) ' '.join ' ' join ans a=queue.popleft() queue.popleft() a c=list(map(int,input().split())) list(map(int,input().split())) c a,b=map(int,input().split()) map(int,input().split()) a n=int(input()) int(input()) n graph=[] [] graph queue=collections.deque([[1,1]]) collections.deque([[1,1]]) queue pointer+=1 1 pointer ans=['0']*n ['0']*n ans pointer=0 0 pointer b=map(int,input().split()) map(int,input().split()) b
import collections
n=int(input())
graph=[]
for i in range(n+1):
  graph.append([])
for i in range(n-1):
  a,b=map(int,input().split())
  graph[a].append(b)
  graph[b].append(a)
c=list(map(int,input().split()))
c.sort()
c.reverse()
ans=['0']*n
pointer=0
queue=collections.deque([[1,1]])
while queue:
  a=queue.popleft()
  ans[a[0]-1]=str(c[pointer])
  for i in graph[a[0]]:
    if i==a[1]:
      continue
    queue.append([i,a[0]])
  pointer+=1
print(sum(c)-c[0])
print(' '.join(ans))