import sys
input = sys.stdin.buffer.readline

from collections import deque

N = int(input())
neighbor = [[] for _ in range(N)]

for _ in range(N-1):
    a, b = map(int, input().split())
    neighbor[a-1].append(b-1)
    neighbor[b-1].append(a-1)

c = list(map(int, input().split()))
c.sort(reverse = True)

d = deque([0])
ans = [c[0]] + [0]*(N-1)
M = 0
c_idx = 1

while d:
    p = d.popleft()
    for n in neighbor[p]:
        if not ans[n]:
            ans[n] = c[c_idx]
            M += c[c_idx]
            c_idx += 1
            d.append(n)

print(M)
print(*ans) import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline from collections import deque N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] neighbor = [[] for _ in range(N)] neighbor [[] for _ in range(N)] for _ in range(N-1):
    a, b = map(int, input().split())
    neighbor[a-1].append(b-1)
    neighbor[b-1].append(a-1) _ 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 neighbor[a-1].append(b-1) [a-1].append [a-1] neighbor a-1 a 1 append b-1 b 1 neighbor[b-1].append(a-1) [b-1].append [b-1] neighbor 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(reverse = True) c.sort c sort reverse = True reverse True d = deque([0]) d deque([0]) deque [0] 0 ans = [c[0]] + [0]*(N-1) ans [c[0]] + [0]*(N-1) [c[0]] c[0] c 0 [0]*(N-1) [0] 0 N-1 N 1 M = 0 M 0 c_idx = 1 c_idx 1 while d:
    p = d.popleft()
    for n in neighbor[p]:
        if not ans[n]:
            ans[n] = c[c_idx]
            M += c[c_idx]
            c_idx += 1
            d.append(n) d p = d.popleft() p d.popleft() d.popleft d popleft for n in neighbor[p]:
        if not ans[n]:
            ans[n] = c[c_idx]
            M += c[c_idx]
            c_idx += 1
            d.append(n) n neighbor[p] neighbor p if not ans[n]:
            ans[n] = c[c_idx]
            M += c[c_idx]
            c_idx += 1
            d.append(n) not ans[n] ans[n] ans n ans[n] = c[c_idx] ans[n] ans n c[c_idx] c c_idx M += c[c_idx] M c[c_idx] c c_idx c_idx += 1 c_idx 1 d.append(n) d.append d append n print(M) print M print(*ans) print *ans ans ans = [c[0]] + [0]*(N-1) [c[0]] + [0]*(N-1) ans M = 0 0 M M += c[c_idx] c[c_idx] M input = sys.stdin.buffer.readline sys.stdin.buffer.readline input c = list(map(int, input().split())) list(map(int, input().split())) c d = deque([0]) deque([0]) d b = map(int, input().split()) map(int, input().split()) b c_idx = 1 1 c_idx N = int(input()) int(input()) N neighbor = [[] for _ in range(N)] [[] for _ in range(N)] neighbor c_idx += 1 1 c_idx p = d.popleft() d.popleft() p a, b = map(int, input().split()) map(int, input().split()) a
import sys
input = sys.stdin.buffer.readline

from collections import deque

N = int(input())
neighbor = [[] for _ in range(N)]

for _ in range(N-1):
    a, b = map(int, input().split())
    neighbor[a-1].append(b-1)
    neighbor[b-1].append(a-1)

c = list(map(int, input().split()))
c.sort(reverse = True)

d = deque([0])
ans = [c[0]] + [0]*(N-1)
M = 0
c_idx = 1

while d:
    p = d.popleft()
    for n in neighbor[p]:
        if not ans[n]:
            ans[n] = c[c_idx]
            M += c[c_idx]
            c_idx += 1
            d.append(n)

print(M)
print(*ans)