from collections import deque

N = int(input())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
C = list(map(int, input().split()))

C.sort()
C = C[::-1]

graph = [[] for _ in range(N + 1)]
check = [1] * (N + 1)
for A, B in AB:
  graph[A].append(B)
  graph[B].append(A)

answer = [0] * (N + 1)
stack = deque()
stack.append(1)
check[1] = 0
ci = 0

while stack:
  s = stack.popleft()
  answer[s] = C[ci]
  ci += 1
  for g in graph[s]:
    if check[g]:
      check[g] = 0
      stack.append(g)
      
print(sum(C) - max(C))
print(*answer[1:])   from collections import deque N = int(input()) N int(input()) int input() input list(map(int, input().split())) for _ in range(N - 1) for _ in range(N - 1) _ range(N - 1) range N - 1 N 1 for _ 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 _ in range(N - 1)] AB [list(map(int, input().split())) for _ 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() C.sort C sort C = C[::-1] C C[::-1] C ::-1 -1 [] for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [] graph = [[] for _ in range(N + 1)] graph [[] for _ in range(N + 1)] check = [1] * (N + 1) check [1] * (N + 1) [1] 1 N + 1 N 1 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 answer = [0] * (N + 1) answer [0] * (N + 1) [0] 0 N + 1 N 1 stack = deque() stack deque() deque stack.append(1) stack.append stack append 1 check[1] = 0 check[1] check 1 0 ci = 0 ci 0 while stack:
  s = stack.popleft()
  answer[s] = C[ci]
  ci += 1
  for g in graph[s]:
    if check[g]:
      check[g] = 0
      stack.append(g)
       stack s = stack.popleft() s stack.popleft() stack.popleft stack popleft answer[s] = C[ci] answer[s] answer s C[ci] C ci ci += 1 ci 1 for g in graph[s]:
    if check[g]:
      check[g] = 0
      stack.append(g)
       g graph[s] graph s if check[g]:
      check[g] = 0
      stack.append(g)
       check[g] check g check[g] = 0 check[g] check g 0 stack.append(g) stack.append stack append g print(sum(C) - max(C)) print sum(C) - max(C) sum(C) sum C max(C) max C print(*answer[1:]) print *answer[1:] answer[1:] answer 1: 1 answer = [0] * (N + 1) [0] * (N + 1) answer graph = [[] for _ in range(N + 1)] [[] for _ in range(N + 1)] graph AB = [list(map(int, input().split())) for _ in range(N - 1)] [list(map(int, input().split())) for _ in range(N - 1)] AB C = list(map(int, input().split())) list(map(int, input().split())) C C = C[::-1] C[::-1] C check = [1] * (N + 1) [1] * (N + 1) check s = stack.popleft() stack.popleft() s stack = deque() deque() stack ci += 1 1 ci N = int(input()) int(input()) N ci = 0 0 ci
from collections import deque

N = int(input())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
C = list(map(int, input().split()))

C.sort()
C = C[::-1]

graph = [[] for _ in range(N + 1)]
check = [1] * (N + 1)
for A, B in AB:
  graph[A].append(B)
  graph[B].append(A)

answer = [0] * (N + 1)
stack = deque()
stack.append(1)
check[1] = 0
ci = 0

while stack:
  s = stack.popleft()
  answer[s] = C[ci]
  ci += 1
  for g in graph[s]:
    if check[g]:
      check[g] = 0
      stack.append(g)
      
print(sum(C) - max(C))
print(*answer[1:])  