from heapq import heapify, heappop
N = int(input())
AB = [tuple(map(int, input().split())) for _ in range(N - 1)]
C = list(map(int, input().split()))

M = sum(C) - max(C)

nl = [list() for i in range(N)]
for a, b in AB:
    nl[a - 1].append(b - 1)
    nl[b - 1].append(a - 1)

v0 = -1
mlen = 0
for i, n in enumerate(nl):
    if len(n) > mlen:
        mlen = len(n)
        v0 = i

q = [-c for c in C]
heapify(q)
ans = [0] * N
stack = [(v0, -1)]
while len(stack) > 0:
    v, p = stack.pop()
    c = heappop(q)
    ans[v] = -c
    for next_v in nl[v]:
        if next_v != p:
            stack.append((next_v, v))
print(M)
print(*ans) from heapq import heapify, heappop N = int(input()) N int(input()) int input() input tuple(map(int, input().split())) for _ in range(N - 1) for _ in range(N - 1) _ range(N - 1) range N - 1 N 1 for _ in range(N - 1) tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split AB = [tuple(map(int, input().split())) for _ in range(N - 1)] AB [tuple(map(int, input().split())) for _ in range(N - 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 M = sum(C) - max(C) M sum(C) - max(C) sum(C) sum C max(C) max C list() for i in range(N) for i in range(N) i range(N) range N for i in range(N) list() list nl = [list() for i in range(N)] nl [list() for i in range(N)] for a, b in AB:
    nl[a - 1].append(b - 1)
    nl[b - 1].append(a - 1) a b AB nl[a - 1].append(b - 1) [a - 1].append [a - 1] nl a - 1 a 1 append b - 1 b 1 nl[b - 1].append(a - 1) [b - 1].append [b - 1] nl b - 1 b 1 append a - 1 a 1 v0 = -1 v0 -1 mlen = 0 mlen 0 for i, n in enumerate(nl):
    if len(n) > mlen:
        mlen = len(n)
        v0 = i i n enumerate(nl) enumerate nl if len(n) > mlen:
        mlen = len(n)
        v0 = i len(n) > mlen len(n) len n mlen mlen = len(n) mlen len(n) len n v0 = i v0 i -c for c in C for c in C c C for c in C -c c q = [-c for c in C] q [-c for c in C] heapify(q) heapify q ans = [0] * N ans [0] * N [0] 0 N stack = [(v0, -1)] stack [(v0, -1)] (v0, -1) v0 -1 while len(stack) > 0:
    v, p = stack.pop()
    c = heappop(q)
    ans[v] = -c
    for next_v in nl[v]:
        if next_v != p:
            stack.append((next_v, v)) len(stack) > 0 len(stack) len stack 0 v, p = stack.pop() v stack.pop() stack.pop stack pop p c = heappop(q) c heappop(q) heappop q ans[v] = -c ans[v] ans v -c c for next_v in nl[v]:
        if next_v != p:
            stack.append((next_v, v)) next_v nl[v] nl v if next_v != p:
            stack.append((next_v, v)) next_v != p next_v p stack.append((next_v, v)) stack.append stack append (next_v, v) next_v v print(M) print M print(*ans) print *ans ans mlen = 0 0 mlen nl = [list() for i in range(N)] [list() for i in range(N)] nl ans = [0] * N [0] * N ans AB = [tuple(map(int, input().split())) for _ in range(N - 1)] [tuple(map(int, input().split())) for _ in range(N - 1)] AB v0 = i i v0 v, p = stack.pop() stack.pop() v c = heappop(q) heappop(q) c p = stack.pop() stack.pop() p M = sum(C) - max(C) sum(C) - max(C) M C = list(map(int, input().split())) list(map(int, input().split())) C mlen = len(n) len(n) mlen v0 = -1 -1 v0 stack = [(v0, -1)] [(v0, -1)] stack q = [-c for c in C] [-c for c in C] q N = int(input()) int(input()) N
from heapq import heapify, heappop
N = int(input())
AB = [tuple(map(int, input().split())) for _ in range(N - 1)]
C = list(map(int, input().split()))

M = sum(C) - max(C)

nl = [list() for i in range(N)]
for a, b in AB:
    nl[a - 1].append(b - 1)
    nl[b - 1].append(a - 1)

v0 = -1
mlen = 0
for i, n in enumerate(nl):
    if len(n) > mlen:
        mlen = len(n)
        v0 = i

q = [-c for c in C]
heapify(q)
ans = [0] * N
stack = [(v0, -1)]
while len(stack) > 0:
    v, p = stack.pop()
    c = heappop(q)
    ans[v] = -c
    for next_v in nl[v]:
        if next_v != p:
            stack.append((next_v, v))
print(M)
print(*ans)