from collections import deque
N = int(input())
G = [[] for i in range(N+1)]
for i in range(N-1):
  a,b = map(int,input().split())
  G[a].append(b)
  G[b].append(a)
c = list(map(int,input().split()))
c.sort()
M = sum(c)-c[-1]

score = [0 for i in range(N+1)]

seen = [False for i in range(N+1)]
seen[1] = True
score[1] = c.pop()
d = deque([1])
while len(d)>0:
  v = d.pop()
  for i in G[v]:
    if seen[i] == False:
      seen[i] = True
      score[i] = c.pop()
      d.append(i)
###
print(M)
print(*score[1:]) from collections import deque N = int(input()) N int(input()) int input() input [] 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) [] G = [[] for i in range(N+1)] G [[] for i in range(N+1)] for i in range(N-1):
  a,b = map(int,input().split())
  G[a].append(b)
  G[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 G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G 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 M = sum(c)-c[-1] M sum(c)-c[-1] sum(c) sum c c[-1] c -1 0 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) 0 score = [0 for i in range(N+1)] score [0 for i in range(N+1)] False 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) False seen = [False for i in range(N+1)] seen [False for i in range(N+1)] seen[1] = True seen[1] seen 1 True score[1] = c.pop() score[1] score 1 c.pop() c.pop c pop d = deque([1]) d deque([1]) deque [1] 1 while len(d)>0:
  v = d.pop()
  for i in G[v]:
    if seen[i] == False:
      seen[i] = True
      score[i] = c.pop()
      d.append(i)
### len(d)>0 len(d) len d 0 v = d.pop() v d.pop() d.pop d pop for i in G[v]:
    if seen[i] == False:
      seen[i] = True
      score[i] = c.pop()
      d.append(i)
### i G[v] G v if seen[i] == False:
      seen[i] = True
      score[i] = c.pop()
      d.append(i)
### seen[i] == False seen[i] seen i False seen[i] = True seen[i] seen i True score[i] = c.pop() score[i] score i c.pop() c.pop c pop d.append(i) d.append d append i print(M) print M print(*score[1:]) print *score[1:] score[1:] score 1: 1 G = [[] for i in range(N+1)] [[] for i in range(N+1)] G N = int(input()) int(input()) N a,b = map(int,input().split()) map(int,input().split()) a M = sum(c)-c[-1] sum(c)-c[-1] M c = list(map(int,input().split())) list(map(int,input().split())) c b = map(int,input().split()) map(int,input().split()) b v = d.pop() d.pop() v score = [0 for i in range(N+1)] [0 for i in range(N+1)] score d = deque([1]) deque([1]) d seen = [False for i in range(N+1)] [False for i in range(N+1)] seen
from collections import deque
N = int(input())
G = [[] for i in range(N+1)]
for i in range(N-1):
  a,b = map(int,input().split())
  G[a].append(b)
  G[b].append(a)
c = list(map(int,input().split()))
c.sort()
M = sum(c)-c[-1]

score = [0 for i in range(N+1)]

seen = [False for i in range(N+1)]
seen[1] = True
score[1] = c.pop()
d = deque([1])
while len(d)>0:
  v = d.pop()
  for i in G[v]:
    if seen[i] == False:
      seen[i] = True
      score[i] = c.pop()
      d.append(i)
###
print(M)
print(*score[1:])