# スタート地点からDFSで大きい順に入れていけば、最も大きい数以外は得点を得られる



N = int(eval(input()))

E = [[] for i in range(N)]

for i in range(N-1):

  a,b = list(map(int,input().split()))

  E[a-1].append(b-1)

  E[b-1].append(a-1)



# 得点を大きい順に並べる

C = sorted(list(map(int,input().split())))[::-1]



ans = [-1 for i in range(N)] # -1は未訪問

point = sum(C[1:])



from collections import deque

q = deque([])

q.append(0)

ind = 0

while q:

  v = q.popleft()

  if ans[v] != -1:

    # 訪問済み

    continue

  ans[v] = C[ind]

  ind += 1

  if ind == len(C):

    break

  children = E[v]

  for child in children:

    q.append(child)

  

print(point)  

print((*ans))
import sys

readline = sys.stdin.readline



N = int(readline())

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



for i in range(N - 1):

  a,b = list(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))
# スタート地点からDFSで大きい順に入れていけば、最も大きい数以外は得点を得られる
N = int(eval(input()))
E = [[] for i in range(N)]
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    E[a - 1].append(b - 1)
    E[b - 1].append(a - 1)
# 得点を大きい順に並べる
C = sorted(list(map(int, input().split())))[::-1]
ans = [-1 for i in range(N)]  # -1は未訪問
point = sum(C[1:])
from collections import deque

q = deque([])
q.append(0)
ind = 0
while q:
    v = q.popleft()
    if ans[v] != -1:
        # 訪問済み
        continue
    ans[v] = C[ind]
    ind += 1
    if ind == len(C):
        break
    children = E[v]
    for child in children:
        q.append(child)
print(point)
print((*ans))

import sys

readline = sys.stdin.readline
N = int(readline())
G = [[] for i in range(N)]
for i in range(N - 1):
    a, b = list(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))

-# スタート地点からDFSで大きい順に入れていけば、最も大きい数以外は得点を得られる -N = int(eval(input())) -E = [[] for i in range(N)] +import sys + +readline = sys.stdin.readline +N = int(readline()) +G = [[] for i in range(N)] -    a, b = list(map(int, input().split())) -    E[a - 1].append(b - 1) -    E[b - 1].append(a - 1) -# 得点を大きい順に並べる -C = sorted(list(map(int, input().split())))[::-1] -ans = [-1 for i in range(N)]  # -1は未訪問 -point = sum(C[1:]) +    a, b = list(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) -q.append(0) +q.append([start, -1]) +ans = [None] * N -    v = q.popleft() -    if ans[v] != -1: -        # 訪問済み -        continue +    v, parent = q.popleft() -    if ind == len(C): -        break -    children = E[v] -    for child in children: -        q.append(child) -print(point) +    for child in G[v]: +        if child == parent: +            continue +        q.append([child, v]) +print((sum(C[1:])))