#実験用の写経
N = int(input())
g = {i: [] for i in range(N)}
for i in range(N - 1):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
    g[b - 1].append(a - 1)
c = list(map(int, input().split()))
c.sort()
print(sum(c[:-1]))
nums = [0] * N
stack = [0]
while stack:
    d = stack.pop()
    nums[d] = c.pop()
    for node in g[d]:
        if nums[node] == 0:
            stack.append(node)
print(' '.join(map(str, nums))) N = int(input()) N int(input()) int input() input i: [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) i [] g = {i: [] for i in range(N)} g {i: [] for i in range(N)} for i in range(N - 1):
    a, b = map(int, input().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, input().split()) a map(int, input().split()) map int input().split() ().split () input 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, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort() c.sort c sort print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 nums = [0] * N nums [0] * N [0] 0 N stack = [0] stack [0] 0 while stack:
    d = stack.pop()
    nums[d] = c.pop()
    for node in g[d]:
        if nums[node] == 0:
            stack.append(node) stack d = stack.pop() d stack.pop() stack.pop stack pop nums[d] = c.pop() nums[d] nums d c.pop() c.pop c pop for node in g[d]:
        if nums[node] == 0:
            stack.append(node) node g[d] g d if nums[node] == 0:
            stack.append(node) nums[node] == 0 nums[node] nums node 0 stack.append(node) stack.append stack append node print(' '.join(map(str, nums))) print ' '.join(map(str, nums)) ' '.join ' ' join map(str, nums) map str nums a, b = map(int, input().split()) map(int, input().split()) a N = int(input()) int(input()) N g = {i: [] for i in range(N)} {i: [] for i in range(N)} g b = map(int, input().split()) map(int, input().split()) b nums = [0] * N [0] * N nums d = stack.pop() stack.pop() d c = list(map(int, input().split())) list(map(int, input().split())) c stack = [0] [0] stack
#実験用の写経
N = int(input())
g = {i: [] for i in range(N)}
for i in range(N - 1):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
    g[b - 1].append(a - 1)
c = list(map(int, input().split()))
c.sort()
print(sum(c[:-1]))
nums = [0] * N
stack = [0]
while stack:
    d = stack.pop()
    nums[d] = c.pop()
    for node in g[d]:
        if nums[node] == 0:
            stack.append(node)
print(' '.join(map(str, nums)))
