from collections import deque

N = int(input())

edges = [[] for _ in range(N)]
edgeList = []
for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)
    edgeList.append((fr, to))

C = list(map(int, input().split()))
C.sort()

M = sum(C) - C[-1]

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

while que:
    now = que.popleft()
    values[now] = C.pop()
    for to in edges[now]:
        if values[to] == -1:
            que.append(to)

print(M)
print(*values) 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) [] edges = [[] for _ in range(N)] edges [[] for _ in range(N)] edgeList = [] edgeList [] for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)
    edgeList.append((fr, to)) _ range(N - 1) range N - 1 N 1 fr, to = map(int, input().split()) fr map(int, input().split()) map int input().split() ().split () input split to fr -= 1 fr 1 to -= 1 to 1 edges[fr].append(to) [fr].append [fr] edges fr append to edges[to].append(fr) [to].append [to] edges to append fr edgeList.append((fr, to)) edgeList.append edgeList append (fr, to) fr to 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() C.sort C sort M = sum(C) - C[-1] M sum(C) - C[-1] sum(C) sum C C[-1] C -1 values = [-1] * N values [-1] * N [-1] -1 N que = deque([0]) que deque([0]) deque [0] 0 while que:
    now = que.popleft()
    values[now] = C.pop()
    for to in edges[now]:
        if values[to] == -1:
            que.append(to) que now = que.popleft() now que.popleft() que.popleft que popleft values[now] = C.pop() values[now] values now C.pop() C.pop C pop for to in edges[now]:
        if values[to] == -1:
            que.append(to) to edges[now] edges now if values[to] == -1:
            que.append(to) values[to] == -1 values[to] values to -1 que.append(to) que.append que append to print(M) print M print(*values) print *values values now = que.popleft() que.popleft() now que = deque([0]) deque([0]) que edgeList = [] [] edgeList C = list(map(int, input().split())) list(map(int, input().split())) C fr, to = map(int, input().split()) map(int, input().split()) fr to = map(int, input().split()) map(int, input().split()) to edges = [[] for _ in range(N)] [[] for _ in range(N)] edges fr -= 1 1 fr M = sum(C) - C[-1] sum(C) - C[-1] M N = int(input()) int(input()) N values = [-1] * N [-1] * N values to -= 1 1 to
from collections import deque

N = int(input())

edges = [[] for _ in range(N)]
edgeList = []
for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)
    edgeList.append((fr, to))

C = list(map(int, input().split()))
C.sort()

M = sum(C) - C[-1]

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

while que:
    now = que.popleft()
    values[now] = C.pop()
    for to in edges[now]:
        if values[to] == -1:
            que.append(to)

print(M)
print(*values)