import sys
readline = sys.stdin.readline

N = int(readline())
G = [[] for i in range(N)]

for i in range(N - 1):
  a,b = map(int,readline().split())
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
  
C = list(map(int,readline().split()))

def get_farest(x):
  dist = [-1] * N
  stack = [(x,-1,0)]
  while stack:
    v,parent,c = stack.pop()
    if dist[v] != -1:
      continue
    dist[v] = c
    for child in G[v]:
      if child == parent:
        continue
      stack.append([child, v, c + 1])
  maxv = -1
  maxi = -1
  for i in range(len(dist)):
    if dist[i] > maxv:
      maxv = dist[i]
      maxi = i
  return maxi

f = get_farest(0)
start = get_farest(f)

C = sorted(C, reverse = True)
from collections import deque
q = deque([])
q.append([start, -1])
ind = 0
ans = [None] * N
while q:
  v,parent = q.popleft()
  ans[v] = C[ind]
  ind += 1
  for child in G[v]:
    if child == parent:
      continue
    q.append([child, v])
  
print(sum(C[1:]))
print(*ans) import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline N = int(readline()) N int(readline()) int readline() readline [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] G = [[] for i in range(N)] G [[] for i in range(N)] for i in range(N - 1):
  a,b = map(int,readline().split())
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
   i range(N - 1) range N - 1 N 1 a,b = map(int,readline().split()) a map(int,readline().split()) map int readline().split() ().split () readline split b G[a - 1].append(b - 1) [a - 1].append [a - 1] G a - 1 a 1 append b - 1 b 1 G[b - 1].append(a - 1) [b - 1].append [b - 1] G b - 1 b 1 append a - 1 a 1 C = list(map(int,readline().split())) C list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split def get_farest(x):
  dist = [-1] * N
  stack = [(x,-1,0)]
  while stack:
    v,parent,c = stack.pop()
    if dist[v] != -1:
      continue
    dist[v] = c
    for child in G[v]:
      if child == parent:
        continue
      stack.append([child, v, c + 1])
  maxv = -1
  maxi = -1
  for i in range(len(dist)):
    if dist[i] > maxv:
      maxv = dist[i]
      maxi = i
  return maxi get_farest dist = [-1] * N dist [-1] * N [-1] -1 N stack = [(x,-1,0)] stack [(x,-1,0)] (x,-1,0) x -1 0 while stack:
    v,parent,c = stack.pop()
    if dist[v] != -1:
      continue
    dist[v] = c
    for child in G[v]:
      if child == parent:
        continue
      stack.append([child, v, c + 1])
   stack v,parent,c = stack.pop() v stack.pop() stack.pop stack pop parent c if dist[v] != -1:
      continue
     dist[v] != -1 dist[v] dist v -1 continue dist[v] = c dist[v] dist v c for child in G[v]:
      if child == parent:
        continue
      stack.append([child, v, c + 1])
   child G[v] G v if child == parent:
        continue
       child == parent child parent continue stack.append([child, v, c + 1]) stack.append stack append [child, v, c + 1] child v c + 1 c 1 maxv = -1 maxv -1 maxi = -1 maxi -1 for i in range(len(dist)):
    if dist[i] > maxv:
      maxv = dist[i]
      maxi = i
   i range(len(dist)) range len(dist) len dist if dist[i] > maxv:
      maxv = dist[i]
      maxi = i
   dist[i] > maxv dist[i] dist i maxv maxv = dist[i] maxv dist[i] dist i maxi = i maxi i return maxi maxi x x f = get_farest(0) f get_farest(0) get_farest 0 start = get_farest(f) start get_farest(f) get_farest f C = sorted(C, reverse = True) C sorted(C, reverse = True) sorted C reverse = True reverse True from collections import deque q = deque([]) q deque([]) deque [] q.append([start, -1]) q.append q append [start, -1] start -1 ind = 0 ind 0 ans = [None] * N ans [None] * N [None] None N while q:
  v,parent = q.popleft()
  ans[v] = C[ind]
  ind += 1
  for child in G[v]:
    if child == parent:
      continue
    q.append([child, v])
   q v,parent = q.popleft() v q.popleft() q.popleft q popleft parent ans[v] = C[ind] ans[v] ans v C[ind] C ind ind += 1 ind 1 for child in G[v]:
    if child == parent:
      continue
    q.append([child, v])
   child G[v] G v if child == parent:
      continue
     child == parent child parent continue q.append([child, v]) q.append q append [child, v] child v print(sum(C[1:])) print sum(C[1:]) sum C[1:] C 1: 1 print(*ans) print *ans ans def get_farest(x):
  dist = [-1] * N
  stack = [(x,-1,0)]
  while stack:
    v,parent,c = stack.pop()
    if dist[v] != -1:
      continue
    dist[v] = c
    for child in G[v]:
      if child == parent:
        continue
      stack.append([child, v, c + 1])
  maxv = -1
  maxi = -1
  for i in range(len(dist)):
    if dist[i] > maxv:
      maxv = dist[i]
      maxi = i
  return maxi def get_farest(x):
  dist = [-1] * N
  stack = [(x,-1,0)]
  while stack:
    v,parent,c = stack.pop()
    if dist[v] != -1:
      continue
    dist[v] = c
    for child in G[v]:
      if child == parent:
        continue
      stack.append([child, v, c + 1])
  maxv = -1
  maxi = -1
  for i in range(len(dist)):
    if dist[i] > maxv:
      maxv = dist[i]
      maxi = i
  return maxi get_farest f = get_farest(0) get_farest(0) f parent = q.popleft() q.popleft() parent b = map(int,readline().split()) map(int,readline().split()) b v,parent = q.popleft() q.popleft() v start = get_farest(f) get_farest(f) start ind = 0 0 ind a,b = map(int,readline().split()) map(int,readline().split()) a readline = sys.stdin.readline sys.stdin.readline readline ind += 1 1 ind G = [[] for i in range(N)] [[] for i in range(N)] G q = deque([]) deque([]) q ans = [None] * N [None] * N ans N = int(readline()) int(readline()) N C = sorted(C, reverse = True) sorted(C, reverse = True) C C = list(map(int,readline().split())) list(map(int,readline().split())) C
import sys
readline = sys.stdin.readline

N = int(readline())
G = [[] for i in range(N)]

for i in range(N - 1):
  a,b = map(int,readline().split())
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
  
C = list(map(int,readline().split()))

def get_farest(x):
  dist = [-1] * N
  stack = [(x,-1,0)]
  while stack:
    v,parent,c = stack.pop()
    if dist[v] != -1:
      continue
    dist[v] = c
    for child in G[v]:
      if child == parent:
        continue
      stack.append([child, v, c + 1])
  maxv = -1
  maxi = -1
  for i in range(len(dist)):
    if dist[i] > maxv:
      maxv = dist[i]
      maxi = i
  return maxi

f = get_farest(0)
start = get_farest(f)

C = sorted(C, reverse = True)
from collections import deque
q = deque([])
q.append([start, -1])
ind = 0
ans = [None] * N
while q:
  v,parent = q.popleft()
  ans[v] = C[ind]
  ind += 1
  for child in G[v]:
    if child == parent:
      continue
    q.append([child, v])
  
print(sum(C[1:]))
print(*ans)