from collections import deque

N = int(input())
tree = [[] for i in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  a -= 1; b -= 1
  tree[a].append(b); tree[b].append(a)

C = list(map(int, input().split()))
C.sort()
ans = sum(C[:-1])
dep = [-1] * N
dep[0] = C.pop()
d = deque([[0, -1]])
while d:
  node, par = d.popleft()
  children = tree[node]
  for child in children:
    if child == par:
      continue
    dep[child] = C.pop()
    d.append([child, node])

print(ans)
print(*dep) from collections import deque N = int(input()) N int(input()) int input() input [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] tree = [[] for i in range(N)] tree [[] for i in range(N)] for i in range(N-1):
  a, b = map(int, input().split())
  a -= 1; b -= 1
  tree[a].append(b); tree[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 a -= 1 a 1 b -= 1 b 1 tree[a].append(b) [a].append [a] tree a append b tree[b].append(a) [b].append [b] tree 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 ans = sum(C[:-1]) ans sum(C[:-1]) sum C[:-1] C :-1 -1 dep = [-1] * N dep [-1] * N [-1] -1 N dep[0] = C.pop() dep[0] dep 0 C.pop() C.pop C pop d = deque([[0, -1]]) d deque([[0, -1]]) deque [[0, -1]] [0, -1] 0 -1 while d:
  node, par = d.popleft()
  children = tree[node]
  for child in children:
    if child == par:
      continue
    dep[child] = C.pop()
    d.append([child, node]) d node, par = d.popleft() node d.popleft() d.popleft d popleft par children = tree[node] children tree[node] tree node for child in children:
    if child == par:
      continue
    dep[child] = C.pop()
    d.append([child, node]) child children if child == par:
      continue
     child == par child par continue dep[child] = C.pop() dep[child] dep child C.pop() C.pop C pop d.append([child, node]) d.append d append [child, node] child node print(ans) print ans print(*dep) print *dep dep children = tree[node] tree[node] children tree = [[] for i in range(N)] [[] for i in range(N)] tree a, b = map(int, input().split()) map(int, input().split()) a b -= 1 1 b C = list(map(int, input().split())) list(map(int, input().split())) C dep = [-1] * N [-1] * N dep node, par = d.popleft() d.popleft() node par = d.popleft() d.popleft() par d = deque([[0, -1]]) deque([[0, -1]]) d ans = sum(C[:-1]) sum(C[:-1]) ans N = int(input()) int(input()) N b = map(int, input().split()) map(int, input().split()) b a -= 1 1 a
from collections import deque

N = int(input())
tree = [[] for i in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  a -= 1; b -= 1
  tree[a].append(b); tree[b].append(a)

C = list(map(int, input().split()))
C.sort()
ans = sum(C[:-1])
dep = [-1] * N
dep[0] = C.pop()
d = deque([[0, -1]])
while d:
  node, par = d.popleft()
  children = tree[node]
  for child in children:
    if child == par:
      continue
    dep[child] = C.pop()
    d.append([child, node])

print(ans)
print(*dep)