from collections import deque
n = int(input())
ab = [list(map(int,input().split())) for i in range(n-1)]
c = list(map(int,input().split()))
c.sort(reverse=True)
graph = [[] for i in range(n+1)]
parent = [0 for i in range(n+1)]
root = 1
order = []
que = deque([root])
for a,b in ab:
  graph[a].append(b)
  graph[b].append(a)
while que:
  x = que.popleft()
  order.append(x)
  for y in graph[x]:
    if parent[x] == y:
      continue
    parent[y] = x
    que.append(y)
ans = []
for i in range(n):
  ans.append((order[i],c[i]))
ans.sort()
print(sum(c[1:]))
for i in ans:
  print(i[1],end=" ") from collections import deque n = int(input()) n int(input()) int input() input list(map(int,input().split())) 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) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ab = [list(map(int,input().split())) for i in range(n-1)] ab [list(map(int,input().split())) for i in range(n-1)] 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 [] 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) [] graph = [[] for i in range(n+1)] graph [[] for i in range(n+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 parent = [0 for i in range(n+1)] parent [0 for i in range(n+1)] root = 1 root 1 order = [] order [] que = deque([root]) que deque([root]) deque [root] root for a,b in ab:
  graph[a].append(b)
  graph[b].append(a) a b ab graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a while que:
  x = que.popleft()
  order.append(x)
  for y in graph[x]:
    if parent[x] == y:
      continue
    parent[y] = x
    que.append(y) que x = que.popleft() x que.popleft() que.popleft que popleft order.append(x) order.append order append x for y in graph[x]:
    if parent[x] == y:
      continue
    parent[y] = x
    que.append(y) y graph[x] graph x if parent[x] == y:
      continue
     parent[x] == y parent[x] parent x y continue parent[y] = x parent[y] parent y x que.append(y) que.append que append y ans = [] ans [] for i in range(n):
  ans.append((order[i],c[i])) i range(n) range n ans.append((order[i],c[i])) ans.append ans append (order[i],c[i]) order[i] order i c[i] c i ans.sort() ans.sort ans sort print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 for i in ans:
  print(i[1],end=" ") i ans print(i[1],end=" ") print i[1] i 1 end=" " end " " ab = [list(map(int,input().split())) for i in range(n-1)] [list(map(int,input().split())) for i in range(n-1)] ab ans = [] [] ans order = [] [] order n = int(input()) int(input()) n graph = [[] for i in range(n+1)] [[] for i in range(n+1)] graph c = list(map(int,input().split())) list(map(int,input().split())) c parent = [0 for i in range(n+1)] [0 for i in range(n+1)] parent root = 1 1 root que = deque([root]) deque([root]) que x = que.popleft() que.popleft() x
from collections import deque
n = int(input())
ab = [list(map(int,input().split())) for i in range(n-1)]
c = list(map(int,input().split()))
c.sort(reverse=True)
graph = [[] for i in range(n+1)]
parent = [0 for i in range(n+1)]
root = 1
order = []
que = deque([root])
for a,b in ab:
  graph[a].append(b)
  graph[b].append(a)
while que:
  x = que.popleft()
  order.append(x)
  for y in graph[x]:
    if parent[x] == y:
      continue
    parent[y] = x
    que.append(y)
ans = []
for i in range(n):
  ans.append((order[i],c[i]))
ans.sort()
print(sum(c[1:]))
for i in ans:
  print(i[1],end=" ")