from collections import deque
N = int(input())

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

c = sorted([int(i) for i in input().split()], reverse=True)

visited = [-1] * N
cnt = 0
que = deque([1])

while que:
    i = que.popleft()
    visited[i-1] = c[cnt]
    cnt += 1

    for j in G[i]:
        if visited[j-1] == -1:
            que.append(j)

# print(visited)
print(sum(c[1:]))
print(*visited) from collections import deque N = int(input()) N int(input()) int input() input k: [] for k in range(N+1) for k in range(N+1) k range(N+1) range N+1 N 1 for k in range(N+1) k [] G = {k: [] for k in range(N+1)} G {k: [] for k in range(N+1)} for _ in range(N-1):
    a, b = map(int, input().split())
    # 無向グラフ
    G[a].append(b)
    G[b].append(a) _ 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].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a 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 visited = [-1] * N visited [-1] * N [-1] -1 N cnt = 0 cnt 0 que = deque([1]) que deque([1]) deque [1] 1 while que:
    i = que.popleft()
    visited[i-1] = c[cnt]
    cnt += 1

    for j in G[i]:
        if visited[j-1] == -1:
            que.append(j)

# print(visited) que i = que.popleft() i que.popleft() que.popleft que popleft visited[i-1] = c[cnt] visited[i-1] visited i-1 i 1 c[cnt] c cnt cnt += 1 cnt 1 for j in G[i]:
        if visited[j-1] == -1:
            que.append(j)

# print(visited) j G[i] G i if visited[j-1] == -1:
            que.append(j)

# print(visited) visited[j-1] == -1 visited[j-1] visited j-1 j 1 -1 que.append(j) que.append que append j print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(*visited) print *visited visited N = int(input()) int(input()) N cnt = 0 0 cnt b = map(int, input().split()) map(int, input().split()) b i = que.popleft() que.popleft() i c = sorted([int(i) for i in input().split()], reverse=True) sorted([int(i) for i in input().split()], reverse=True) c a, b = map(int, input().split()) map(int, input().split()) a cnt += 1 1 cnt G = {k: [] for k in range(N+1)} {k: [] for k in range(N+1)} G visited = [-1] * N [-1] * N visited que = deque([1]) deque([1]) que
from collections import deque
N = int(input())

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

c = sorted([int(i) for i in input().split()], reverse=True)

visited = [-1] * N
cnt = 0
que = deque([1])

while que:
    i = que.popleft()
    visited[i-1] = c[cnt]
    cnt += 1

    for j in G[i]:
        if visited[j-1] == -1:
            que.append(j)

# print(visited)
print(sum(c[1:]))
print(*visited)
