from collections import deque

N = int(input())
X = [[] for i in range(N)]
Y = [-1] * N
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

C = sorted([int(a) for a in input().split()])
print(sum(C)-max(C))

que = deque([0])
while que:
    i = deque.popleft(que)
    if Y[i] < 0:
        Y[i] = C.pop()
        for a in X[i]:
            deque.append(que, a)
print(*Y) from collections import deque 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) [] X = [[] for i in range(N)] X [[] for i in range(N)] Y = [-1] * N Y [-1] * N [-1] -1 N for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1) i range(N-1) range N-1 N 1 x, y = map(int, input().split()) x map(int, input().split()) map int input().split() ().split () input split y X[x-1].append(y-1) [x-1].append [x-1] X x-1 x 1 append y-1 y 1 X[y-1].append(x-1) [y-1].append [y-1] X y-1 y 1 append x-1 x 1 int(a) for a in input().split() for a in input().split() a input().split() ().split () input split for a in input().split() int(a) int a C = sorted([int(a) for a in input().split()]) C sorted([int(a) for a in input().split()]) sorted [int(a) for a in input().split()] print(sum(C)-max(C)) print sum(C)-max(C) sum(C) sum C max(C) max C que = deque([0]) que deque([0]) deque [0] 0 while que:
    i = deque.popleft(que)
    if Y[i] < 0:
        Y[i] = C.pop()
        for a in X[i]:
            deque.append(que, a) que i = deque.popleft(que) i deque.popleft(que) deque.popleft deque popleft que if Y[i] < 0:
        Y[i] = C.pop()
        for a in X[i]:
            deque.append(que, a) Y[i] < 0 Y[i] Y i 0 Y[i] = C.pop() Y[i] Y i C.pop() C.pop C pop for a in X[i]:
            deque.append(que, a) a X[i] X i deque.append(que, a) deque.append deque append que a print(*Y) print *Y Y y = map(int, input().split()) map(int, input().split()) y X = [[] for i in range(N)] [[] for i in range(N)] X Y = [-1] * N [-1] * N Y C = sorted([int(a) for a in input().split()]) sorted([int(a) for a in input().split()]) C x, y = map(int, input().split()) map(int, input().split()) x que = deque([0]) deque([0]) que i = deque.popleft(que) deque.popleft(que) i N = int(input()) int(input()) N
from collections import deque

N = int(input())
X = [[] for i in range(N)]
Y = [-1] * N
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

C = sorted([int(a) for a in input().split()])
print(sum(C)-max(C))

que = deque([0])
while que:
    i = deque.popleft(que)
    if Y[i] < 0:
        Y[i] = C.pop()
        for a in X[i]:
            deque.append(que, a)
print(*Y)