from collections import deque

n = int(input())
ab = [list(map(int, input().split())) for i in range(n-1)]
c = sorted(list(map(int, input().split())), reverse = True)
tree = [list() for i in range(n)]
already = [1] * n 
ans = [0] * n
k = 0
queue = deque([0])

for index,(a,b) in enumerate(ab):
  a -= 1
  b -= 1
  tree[a].append((index,b))
  tree[b].append((index,a))
while queue:
  now = queue.popleft()
  already[now] = 0
  ans[now] = c[k]
  k += 1
  for index,i in tree[now]:
    if already[i]:
      queue.append(i)
         
print(sum(c[1:]))
print(*ans) 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 = sorted(list(map(int, input().split())), reverse = True) c sorted(list(map(int, input().split())), reverse = True) sorted list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split reverse = True reverse True list() for i in range(n) for i in range(n) i range(n) range n for i in range(n) list() list tree = [list() for i in range(n)] tree [list() for i in range(n)] already = [1] * n already [1] * n [1] 1 n ans = [0] * n ans [0] * n [0] 0 n k = 0 k 0 queue = deque([0]) queue deque([0]) deque [0] 0 for index,(a,b) in enumerate(ab):
  a -= 1
  b -= 1
  tree[a].append((index,b))
  tree[b].append((index,a)) index (a,b) a b enumerate(ab) enumerate ab a -= 1 a 1 b -= 1 b 1 tree[a].append((index,b)) [a].append [a] tree a append (index,b) index b tree[b].append((index,a)) [b].append [b] tree b append (index,a) index a while queue:
  now = queue.popleft()
  already[now] = 0
  ans[now] = c[k]
  k += 1
  for index,i in tree[now]:
    if already[i]:
      queue.append(i)
          queue now = queue.popleft() now queue.popleft() queue.popleft queue popleft already[now] = 0 already[now] already now 0 ans[now] = c[k] ans[now] ans now c[k] c k k += 1 k 1 for index,i in tree[now]:
    if already[i]:
      queue.append(i)
          index i tree[now] tree now if already[i]:
      queue.append(i)
          already[i] already i queue.append(i) queue.append queue append i print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(*ans) print *ans ans now = queue.popleft() queue.popleft() now n = int(input()) int(input()) n a -= 1 1 a queue = deque([0]) deque([0]) queue already = [1] * n [1] * n already ab = [list(map(int, input().split())) for i in range(n-1)] [list(map(int, input().split())) for i in range(n-1)] ab k = 0 0 k b -= 1 1 b ans = [0] * n [0] * n ans c = sorted(list(map(int, input().split())), reverse = True) sorted(list(map(int, input().split())), reverse = True) c k += 1 1 k tree = [list() for i in range(n)] [list() for i in range(n)] tree
from collections import deque

n = int(input())
ab = [list(map(int, input().split())) for i in range(n-1)]
c = sorted(list(map(int, input().split())), reverse = True)
tree = [list() for i in range(n)]
already = [1] * n 
ans = [0] * n
k = 0
queue = deque([0])

for index,(a,b) in enumerate(ab):
  a -= 1
  b -= 1
  tree[a].append((index,b))
  tree[b].append((index,a))
while queue:
  now = queue.popleft()
  already[now] = 0
  ans[now] = c[k]
  k += 1
  for index,i in tree[now]:
    if already[i]:
      queue.append(i)
         
print(sum(c[1:]))
print(*ans)
