from collections import Counter
from collections import deque
n = int(input())

counter = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int,input().split())  
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  counter[a-1] += 1
  counter[b-1] += 1
  
C = sorted(list(map(int,input().split())))[::-1]


root = 0
v = 0
for i in range(n):
  if v > counter[i]:
    root = i
    v = counter[i]


def bfs(s):
  que = deque()
  que.append(s)
  nodes = [-1]*n
  k = 0
  ans = 0
  while len(que)>0:    
    q = que.pop()    
    nodes[q] = C[k]    
    k += 1
    
    for v in G[q]:
      if nodes[v]<0:
        que.append(v)
  
  print(sum(C[1:]))
  print(*nodes)                

bfs(root) from collections import Counter from collections import deque n = int(input()) n int(input()) int input() input counter = [0]*n counter [0]*n [0] 0 n [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] G = [[] for _ in range(n)] G [[] for _ in range(n)] for i in range(n-1):
  a,b = map(int,input().split())  
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  counter[a-1] += 1
  counter[b-1] += 1
   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-1].append(b-1) [a-1].append [a-1] G a-1 a 1 append b-1 b 1 G[b-1].append(a-1) [b-1].append [b-1] G b-1 b 1 append a-1 a 1 counter[a-1] += 1 counter[a-1] counter a-1 a 1 1 counter[b-1] += 1 counter[b-1] counter b-1 b 1 1 C = sorted(list(map(int,input().split())))[::-1] C sorted(list(map(int,input().split())))[::-1] (list(map(int,input().split()))) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ::-1 -1 root = 0 root 0 v = 0 v 0 for i in range(n):
  if v > counter[i]:
    root = i
    v = counter[i] i range(n) range n if v > counter[i]:
    root = i
    v = counter[i] v > counter[i] v counter[i] counter i root = i root i v = counter[i] v counter[i] counter i def bfs(s):
  que = deque()
  que.append(s)
  nodes = [-1]*n
  k = 0
  ans = 0
  while len(que)>0:    
    q = que.pop()    
    nodes[q] = C[k]    
    k += 1
    
    for v in G[q]:
      if nodes[v]<0:
        que.append(v)
  
  print(sum(C[1:]))
  print(*nodes)                 bfs que = deque() que deque() deque que.append(s) que.append que append s nodes = [-1]*n nodes [-1]*n [-1] -1 n k = 0 k 0 ans = 0 ans 0 while len(que)>0:    
    q = que.pop()    
    nodes[q] = C[k]    
    k += 1
    
    for v in G[q]:
      if nodes[v]<0:
        que.append(v)
  
   len(que)>0 len(que) len que 0 q = que.pop() q que.pop() que.pop que pop nodes[q] = C[k] nodes[q] nodes q C[k] C k k += 1 k 1 for v in G[q]:
      if nodes[v]<0:
        que.append(v)
  
   v G[q] G q if nodes[v]<0:
        que.append(v)
  
   nodes[v]<0 nodes[v] nodes v 0 que.append(v) que.append que append v print(sum(C[1:])) print sum(C[1:]) sum C[1:] C 1: 1 print(*nodes) print *nodes nodes s s bfs(root) bfs root def bfs(s):
  que = deque()
  que.append(s)
  nodes = [-1]*n
  k = 0
  ans = 0
  while len(que)>0:    
    q = que.pop()    
    nodes[q] = C[k]    
    k += 1
    
    for v in G[q]:
      if nodes[v]<0:
        que.append(v)
  
  print(sum(C[1:]))
  print(*nodes)                 def bfs(s):
  que = deque()
  que.append(s)
  nodes = [-1]*n
  k = 0
  ans = 0
  while len(que)>0:    
    q = que.pop()    
    nodes[q] = C[k]    
    k += 1
    
    for v in G[q]:
      if nodes[v]<0:
        que.append(v)
  
  print(sum(C[1:]))
  print(*nodes)                 bfs C = sorted(list(map(int,input().split())))[::-1] sorted(list(map(int,input().split())))[::-1] C b = map(int,input().split()) map(int,input().split()) b counter = [0]*n [0]*n counter G = [[] for _ in range(n)] [[] for _ in range(n)] G v = 0 0 v v = counter[i] counter[i] v n = int(input()) int(input()) n a,b = map(int,input().split()) map(int,input().split()) a root = i i root root = 0 0 root
from collections import Counter
from collections import deque
n = int(input())

counter = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int,input().split())  
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  counter[a-1] += 1
  counter[b-1] += 1
  
C = sorted(list(map(int,input().split())))[::-1]


root = 0
v = 0
for i in range(n):
  if v > counter[i]:
    root = i
    v = counter[i]


def bfs(s):
  que = deque()
  que.append(s)
  nodes = [-1]*n
  k = 0
  ans = 0
  while len(que)>0:    
    q = que.pop()    
    nodes[q] = C[k]    
    k += 1
    
    for v in G[q]:
      if nodes[v]<0:
        que.append(v)
  
  print(sum(C[1:]))
  print(*nodes)                

bfs(root)
