from collections import deque
n=int(input())
g=[set() for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  g[a].add(b)
  g[b].add(a)
c=list(map(int,input().split()))
c.sort(reverse=True)
print(sum(c)-c[0])
q=deque()
nums=[0]*(n+1)
q.appendleft(1)
while q:
  d=q.popleft()
  nums[d]=c.pop(0)
  for node in g[d]:
    if nums[node]==0:
      q.append(node)
print(*nums[1:]) from collections import deque n=int(input()) n int(input()) int input() input set() 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) set() set g=[set() for i in range(n+1)] g [set() for i in range(n+1)] for i in range(n-1):
  a,b=map(int,input().split())
  g[a].add(b)
  g[b].add(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 g[a].add(b) [a].add [a] g a add b g[b].add(a) [b].add [b] g b add 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(reverse=True) c.sort c sort reverse=True reverse True print(sum(c)-c[0]) print sum(c)-c[0] sum(c) sum c c[0] c 0 q=deque() q deque() deque nums=[0]*(n+1) nums [0]*(n+1) [0] 0 n+1 n 1 q.appendleft(1) q.appendleft q appendleft 1 while q:
  d=q.popleft()
  nums[d]=c.pop(0)
  for node in g[d]:
    if nums[node]==0:
      q.append(node) q d=q.popleft() d q.popleft() q.popleft q popleft nums[d]=c.pop(0) nums[d] nums d c.pop(0) c.pop c pop 0 for node in g[d]:
    if nums[node]==0:
      q.append(node) node g[d] g d if nums[node]==0:
      q.append(node) nums[node]==0 nums[node] nums node 0 q.append(node) q.append q append node print(*nums[1:]) print *nums[1:] nums[1:] nums 1: 1 n=int(input()) int(input()) n q=deque() deque() q c=list(map(int,input().split())) list(map(int,input().split())) c d=q.popleft() q.popleft() d g=[set() for i in range(n+1)] [set() for i in range(n+1)] g nums=[0]*(n+1) [0]*(n+1) nums a,b=map(int,input().split()) map(int,input().split()) a b=map(int,input().split()) map(int,input().split()) b
from collections import deque
n=int(input())
g=[set() for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  g[a].add(b)
  g[b].add(a)
c=list(map(int,input().split()))
c.sort(reverse=True)
print(sum(c)-c[0])
q=deque()
nums=[0]*(n+1)
q.appendleft(1)
while q:
  d=q.popleft()
  nums[d]=c.pop(0)
  for node in g[d]:
    if nums[node]==0:
      q.append(node)
print(*nums[1:])