from collections import deque
N = int(input())
G = {k: [] for k in range(N)}
E = [0]*N
ans = [-1]*N

for _ in range(N-1):
    a, b = map(int, input().split())
    # 無向グラフ
    G[a-1].append(b-1)
    G[b-1].append(a-1)

    E[a-1] += 1
    E[b-1] += 1

C = sorted([int(i) for i in input().split()], reverse=True)
print(sum(C[1:]))


ans[E.index(max(E))] = C[0]
que = deque([(E.index(max(E)), -1)])

idx = 0
while que:
    ci, p = que.popleft()

    for ni in G[ci]:
        if ni != p:
            idx += 1
            ans[ni] = C[idx]
            que.append((ni, ci))

print(*ans) from collections import deque N = int(input()) N int(input()) int input() input k: [] for k in range(N) for k in range(N) k range(N) range N for k in range(N) k [] G = {k: [] for k in range(N)} G {k: [] for k in range(N)} E = [0]*N E [0]*N [0] 0 N ans = [-1]*N ans [-1]*N [-1] -1 N for _ in range(N-1):
    a, b = map(int, input().split())
    # 無向グラフ
    G[a-1].append(b-1)
    G[b-1].append(a-1)

    E[a-1] += 1
    E[b-1] += 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 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 E[a-1] += 1 E[a-1] E a-1 a 1 1 E[b-1] += 1 E[b-1] E b-1 b 1 1 int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i C = sorted([int(i) for i in input().split()], reverse=True) C sorted([int(i) for i in input().split()], reverse=True) sorted [int(i) for i in input().split()] reverse=True reverse True print(sum(C[1:])) print sum(C[1:]) sum C[1:] C 1: 1 ans[E.index(max(E))] = C[0] ans[E.index(max(E))] ans E.index(max(E)) E.index E index max(E) max E C[0] C 0 que = deque([(E.index(max(E)), -1)]) que deque([(E.index(max(E)), -1)]) deque [(E.index(max(E)), -1)] (E.index(max(E)), -1) E.index(max(E)) E.index E index max(E) max E -1 idx = 0 idx 0 while que:
    ci, p = que.popleft()

    for ni in G[ci]:
        if ni != p:
            idx += 1
            ans[ni] = C[idx]
            que.append((ni, ci)) que ci, p = que.popleft() ci que.popleft() que.popleft que popleft p for ni in G[ci]:
        if ni != p:
            idx += 1
            ans[ni] = C[idx]
            que.append((ni, ci)) ni G[ci] G ci if ni != p:
            idx += 1
            ans[ni] = C[idx]
            que.append((ni, ci)) ni != p ni p idx += 1 idx 1 ans[ni] = C[idx] ans[ni] ans ni C[idx] C idx que.append((ni, ci)) que.append que append (ni, ci) ni ci print(*ans) print *ans ans N = int(input()) int(input()) N ci, p = que.popleft() que.popleft() ci idx = 0 0 idx idx += 1 1 idx b = map(int, input().split()) map(int, input().split()) b C = sorted([int(i) for i in input().split()], reverse=True) sorted([int(i) for i in input().split()], reverse=True) C que = deque([(E.index(max(E)), -1)]) deque([(E.index(max(E)), -1)]) que ans = [-1]*N [-1]*N ans E = [0]*N [0]*N E p = que.popleft() que.popleft() p G = {k: [] for k in range(N)} {k: [] for k in range(N)} G a, b = map(int, input().split()) map(int, input().split()) a
from collections import deque
N = int(input())
G = {k: [] for k in range(N)}
E = [0]*N
ans = [-1]*N

for _ in range(N-1):
    a, b = map(int, input().split())
    # 無向グラフ
    G[a-1].append(b-1)
    G[b-1].append(a-1)

    E[a-1] += 1
    E[b-1] += 1

C = sorted([int(i) for i in input().split()], reverse=True)
print(sum(C[1:]))


ans[E.index(max(E))] = C[0]
que = deque([(E.index(max(E)), -1)])

idx = 0
while que:
    ci, p = que.popleft()

    for ni in G[ci]:
        if ni != p:
            idx += 1
            ans[ni] = C[idx]
            que.append((ni, ci))

print(*ans)
