from queue import Queue

N = int(input())
node_array = [[] for i in range(N)]

for _ in range(N - 1):
    i, j = map(int, input().split())
    node_array[i - 1].append(j - 1)
    node_array[j - 1].append(i - 1)

c_array = sorted(map(int, input().split()), reverse=True)
ans = sum(c_array[1:])
ans_array = [-1] * N

edge = Queue()

edge.put(0)
ans_array[0] = 0

for i in range(N):
    item = edge.get()
    ans_array[item] = str(c_array[i])
    for j in node_array[item]:
        if ans_array[j] == -1:
            edge.put(j)
            ans_array[j] == 0

print(ans)
print(" ".join(ans_array)) from queue import Queue N = int(input()) N int(input()) int input() input [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] node_array = [[] for i in range(N)] node_array [[] for i in range(N)] for _ in range(N - 1):
    i, j = map(int, input().split())
    node_array[i - 1].append(j - 1)
    node_array[j - 1].append(i - 1) _ range(N - 1) range N - 1 N 1 i, j = map(int, input().split()) i map(int, input().split()) map int input().split() ().split () input split j node_array[i - 1].append(j - 1) [i - 1].append [i - 1] node_array i - 1 i 1 append j - 1 j 1 node_array[j - 1].append(i - 1) [j - 1].append [j - 1] node_array j - 1 j 1 append i - 1 i 1 c_array = sorted(map(int, input().split()), reverse=True) c_array sorted(map(int, input().split()), reverse=True) sorted map(int, input().split()) map int input().split() ().split () input split reverse=True reverse True ans = sum(c_array[1:]) ans sum(c_array[1:]) sum c_array[1:] c_array 1: 1 ans_array = [-1] * N ans_array [-1] * N [-1] -1 N edge = Queue() edge Queue() Queue edge.put(0) edge.put edge put 0 ans_array[0] = 0 ans_array[0] ans_array 0 0 for i in range(N):
    item = edge.get()
    ans_array[item] = str(c_array[i])
    for j in node_array[item]:
        if ans_array[j] == -1:
            edge.put(j)
            ans_array[j] == 0 i range(N) range N item = edge.get() item edge.get() edge.get edge get ans_array[item] = str(c_array[i]) ans_array[item] ans_array item str(c_array[i]) str c_array[i] c_array i for j in node_array[item]:
        if ans_array[j] == -1:
            edge.put(j)
            ans_array[j] == 0 j node_array[item] node_array item if ans_array[j] == -1:
            edge.put(j)
            ans_array[j] == 0 ans_array[j] == -1 ans_array[j] ans_array j -1 edge.put(j) edge.put edge put j ans_array[j] == 0 ans_array[j] ans_array j 0 print(ans) print ans print(" ".join(ans_array)) print " ".join(ans_array) " ".join " " join ans_array j = map(int, input().split()) map(int, input().split()) j ans_array = [-1] * N [-1] * N ans_array edge = Queue() Queue() edge item = edge.get() edge.get() item N = int(input()) int(input()) N ans = sum(c_array[1:]) sum(c_array[1:]) ans c_array = sorted(map(int, input().split()), reverse=True) sorted(map(int, input().split()), reverse=True) c_array i, j = map(int, input().split()) map(int, input().split()) i node_array = [[] for i in range(N)] [[] for i in range(N)] node_array
from queue import Queue

N = int(input())
node_array = [[] for i in range(N)]

for _ in range(N - 1):
    i, j = map(int, input().split())
    node_array[i - 1].append(j - 1)
    node_array[j - 1].append(i - 1)

c_array = sorted(map(int, input().split()), reverse=True)
ans = sum(c_array[1:])
ans_array = [-1] * N

edge = Queue()

edge.put(0)
ans_array[0] = 0

for i in range(N):
    item = edge.get()
    ans_array[item] = str(c_array[i])
    for j in node_array[item]:
        if ans_array[j] == -1:
            edge.put(j)
            ans_array[j] == 0

print(ans)
print(" ".join(ans_array))
