import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N = int(input())
e = dd(list)
if N == 1:
  print(0)
  print(1)
  exit(0)
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)
res = [0] * (N + 1)
x = list(map(int, input().split()))
x.sort()
print(sum(x[: -1]))
p = 0
dimp = 0
for i in range(N + 1):
  if len(e[i]) > dimp:
    p = i
    dimp = len(e[i])
Q = dq([])
Q.append(p)
vis = set()
while len(Q):
  q = Q.popleft()
  y = x.pop()
  vis.add(q)
  res[q] = y
  for n in e[q]:
    if n in vis: continue
    Q.append(n)

print(" ".join(list(map(str, res[1: ])))) import sys sys from collections import defaultdict as dd from collections import deque as dq input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N = int(input()) N int(input()) int input() input e = dd(list) e dd(list) dd list if N == 1:
  print(0)
  print(1)
  exit(0) N == 1 N 1 print(0) print 0 print(1) print 1 exit(0) exit 0 for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u) _ 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 e[u].append(v) [u].append [u] e u append v e[v].append(u) [v].append [v] e v append u res = [0] * (N + 1) res [0] * (N + 1) [0] 0 N + 1 N 1 x = list(map(int, input().split())) x list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split x.sort() x.sort x sort print(sum(x[: -1])) print sum(x[: -1]) sum x[: -1] x : -1 -1 p = 0 p 0 dimp = 0 dimp 0 for i in range(N + 1):
  if len(e[i]) > dimp:
    p = i
    dimp = len(e[i]) i range(N + 1) range N + 1 N 1 if len(e[i]) > dimp:
    p = i
    dimp = len(e[i]) len(e[i]) > dimp len(e[i]) len e[i] e i dimp p = i p i dimp = len(e[i]) dimp len(e[i]) len e[i] e i Q = dq([]) Q dq([]) dq [] Q.append(p) Q.append Q append p vis = set() vis set() set while len(Q):
  q = Q.popleft()
  y = x.pop()
  vis.add(q)
  res[q] = y
  for n in e[q]:
    if n in vis: continue
    Q.append(n) len(Q) len Q q = Q.popleft() q Q.popleft() Q.popleft Q popleft y = x.pop() y x.pop() x.pop x pop vis.add(q) vis.add vis add q res[q] = y res[q] res q y for n in e[q]:
    if n in vis: continue
    Q.append(n) n e[q] e q if n in vis: continue
     n in vis n vis Q.append(n) Q.append Q append n print(" ".join(list(map(str, res[1: ])))) print " ".join(list(map(str, res[1: ]))) " ".join " " join list(map(str, res[1: ])) list map(str, res[1: ]) map str res[1: ] res 1: 1 q = Q.popleft() Q.popleft() q v = map(int, input().split()) map(int, input().split()) v e = dd(list) dd(list) e input = sys.stdin.readline sys.stdin.readline input u, v = map(int, input().split()) map(int, input().split()) u Q = dq([]) dq([]) Q vis = set() set() vis y = x.pop() x.pop() y dimp = 0 0 dimp res = [0] * (N + 1) [0] * (N + 1) res p = i i p p = 0 0 p dimp = len(e[i]) len(e[i]) dimp N = int(input()) int(input()) N x = list(map(int, input().split())) list(map(int, input().split())) x
import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N = int(input())
e = dd(list)
if N == 1:
  print(0)
  print(1)
  exit(0)
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)
res = [0] * (N + 1)
x = list(map(int, input().split()))
x.sort()
print(sum(x[: -1]))
p = 0
dimp = 0
for i in range(N + 1):
  if len(e[i]) > dimp:
    p = i
    dimp = len(e[i])
Q = dq([])
Q.append(p)
vis = set()
while len(Q):
  q = Q.popleft()
  y = x.pop()
  vis.add(q)
  res[q] = y
  for n in e[q]:
    if n in vis: continue
    Q.append(n)

print(" ".join(list(map(str, res[1: ]))))