from collections import deque
N = int(input())
G = [[] for i in range(N)]
D = [0]*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)
    D[a-1] += 1
    D[b-1] += 1
*C, = map(int, input().split())
C.sort()

U = [1]*N
que = deque()
for i in range(N):
    if D[i] <= 1:
        que.append(i)
        U[i] = 0
it = iter(C).__next__
ans = [0]*N
R = 0
while que:
    v = que.popleft()
    ans[v] = r = it()
    R += r
    for w in G[v]:
        if U[w] == 0:
            continue
        D[w] -= 1
        if D[w] <= 1:
            U[w] = 0
            que.append(w)
print(R - C[-1])
print(*ans) 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) [] G = [[] for i in range(N)] G [[] for i in range(N)] D = [0]*N D [0]*N [0] 0 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)
    D[a-1] += 1
    D[b-1] += 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 D[a-1] += 1 D[a-1] D a-1 a 1 1 D[b-1] += 1 D[b-1] D b-1 b 1 1 *C, = map(int, input().split()) *C C map(int, input().split()) map int input().split() ().split () input split C.sort() C.sort C sort U = [1]*N U [1]*N [1] 1 N que = deque() que deque() deque for i in range(N):
    if D[i] <= 1:
        que.append(i)
        U[i] = 0 i range(N) range N if D[i] <= 1:
        que.append(i)
        U[i] = 0 D[i] <= 1 D[i] D i 1 que.append(i) que.append que append i U[i] = 0 U[i] U i 0 it = iter(C).__next__ it iter(C).__next__ (C) iter C __next__ ans = [0]*N ans [0]*N [0] 0 N R = 0 R 0 while que:
    v = que.popleft()
    ans[v] = r = it()
    R += r
    for w in G[v]:
        if U[w] == 0:
            continue
        D[w] -= 1
        if D[w] <= 1:
            U[w] = 0
            que.append(w) que v = que.popleft() v que.popleft() que.popleft que popleft ans[v] = r = it() ans[v] ans v it() it = r = it() r it() R += r R r for w in G[v]:
        if U[w] == 0:
            continue
        D[w] -= 1
        if D[w] <= 1:
            U[w] = 0
            que.append(w) w G[v] G v if U[w] == 0:
            continue
         U[w] == 0 U[w] U w 0 continue D[w] -= 1 D[w] D w 1 if D[w] <= 1:
            U[w] = 0
            que.append(w) D[w] <= 1 D[w] D w 1 U[w] = 0 U[w] U w 0 que.append(w) que.append que append w print(R - C[-1]) print R - C[-1] R C[-1] C -1 print(*ans) print *ans ans que = deque() deque() que b = map(int, input().split()) map(int, input().split()) b R = 0 0 R U = [1]*N [1]*N U a, b = map(int, input().split()) map(int, input().split()) a it = iter(C).__next__ iter(C).__next__ it D = [0]*N [0]*N D v = que.popleft() que.popleft() v ans = [0]*N [0]*N ans N = int(input()) int(input()) N G = [[] for i in range(N)] [[] for i in range(N)] G r = it() it() r R += r r R
from collections import deque
N = int(input())
G = [[] for i in range(N)]
D = [0]*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)
    D[a-1] += 1
    D[b-1] += 1
*C, = map(int, input().split())
C.sort()

U = [1]*N
que = deque()
for i in range(N):
    if D[i] <= 1:
        que.append(i)
        U[i] = 0
it = iter(C).__next__
ans = [0]*N
R = 0
while que:
    v = que.popleft()
    ans[v] = r = it()
    R += r
    for w in G[v]:
        if U[w] == 0:
            continue
        D[w] -= 1
        if D[w] <= 1:
            U[w] = 0
            que.append(w)
print(R - C[-1])
print(*ans)