n = int(input())
g = [[] * n for i in range(n)]
for i in range(n-1):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  g[u].append(v)
  g[v].append(u)
  
d = list(map(int,input().split()))
d.sort(reverse=True)

from collections import deque
 
que = deque()
que.append(0)
chk = [False]*n
chk[0] = True
ans = [0]*n
ans[0] = d[0]
cnt = 1
while que:
  now = que.popleft()
  if cnt == n:
    break
  for i in g[now]:
    if not chk[i]:
      chk[i] = True
      ans[i] = d[cnt]
      cnt += 1
      que.append(i)
      
print(sum(d[1:]) if n > 1 else 0)
print(*ans) n = int(input()) n int(input()) int input() input [] * n for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] * n [] n g = [[] * n for i in range(n)] g [[] * n for i in range(n)] for i in range(n-1):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  g[u].append(v)
  g[v].append(u)
   i range(n-1) range n-1 n 1 u, v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split v u -= 1 u 1 v -= 1 v 1 g[u].append(v) [u].append [u] g u append v g[v].append(u) [v].append [v] g v append u d = list(map(int,input().split())) d list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split d.sort(reverse=True) d.sort d sort reverse=True reverse True from collections import deque que = deque() que deque() deque que.append(0) que.append que append 0 chk = [False]*n chk [False]*n [False] False n chk[0] = True chk[0] chk 0 True ans = [0]*n ans [0]*n [0] 0 n ans[0] = d[0] ans[0] ans 0 d[0] d 0 cnt = 1 cnt 1 while que:
  now = que.popleft()
  if cnt == n:
    break
  for i in g[now]:
    if not chk[i]:
      chk[i] = True
      ans[i] = d[cnt]
      cnt += 1
      que.append(i)
       que now = que.popleft() now que.popleft() que.popleft que popleft if cnt == n:
    break
   cnt == n cnt n break for i in g[now]:
    if not chk[i]:
      chk[i] = True
      ans[i] = d[cnt]
      cnt += 1
      que.append(i)
       i g[now] g now if not chk[i]:
      chk[i] = True
      ans[i] = d[cnt]
      cnt += 1
      que.append(i)
       not chk[i] chk[i] chk i chk[i] = True chk[i] chk i True ans[i] = d[cnt] ans[i] ans i d[cnt] d cnt cnt += 1 cnt 1 que.append(i) que.append que append i print(sum(d[1:]) if n > 1 else 0) print sum(d[1:]) if n > 1 else 0 n > 1 n 1 sum(d[1:]) sum d[1:] d 1: 1 0 print(*ans) print *ans ans u, v = map(int, input().split()) map(int, input().split()) u ans = [0]*n [0]*n ans n = int(input()) int(input()) n chk = [False]*n [False]*n chk cnt = 1 1 cnt now = que.popleft() que.popleft() now v -= 1 1 v v = map(int, input().split()) map(int, input().split()) v d = list(map(int,input().split())) list(map(int,input().split())) d u -= 1 1 u cnt += 1 1 cnt que = deque() deque() que g = [[] * n for i in range(n)] [[] * n for i in range(n)] g
n = int(input())
g = [[] * n for i in range(n)]
for i in range(n-1):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  g[u].append(v)
  g[v].append(u)
  
d = list(map(int,input().split()))
d.sort(reverse=True)

from collections import deque
 
que = deque()
que.append(0)
chk = [False]*n
chk[0] = True
ans = [0]*n
ans[0] = d[0]
cnt = 1
while que:
  now = que.popleft()
  if cnt == n:
    break
  for i in g[now]:
    if not chk[i]:
      chk[i] = True
      ans[i] = d[cnt]
      cnt += 1
      que.append(i)
      
print(sum(d[1:]) if n > 1 else 0)
print(*ans)