from collections import deque

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

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

not_visited = [-1]*N
not_visited[0] = 0
stack = deque()
stack.append(0)

ans = [0]*N
ans[0] = c[0]

cut = 1
while stack:
    now = stack.popleft()
    for nex in lines[now]:
        if not_visited[nex] == -1:
            ans[nex] = c[cut]
            not_visited[nex] =  not_visited[now] + 1
            cut += 1
            stack.append(nex)

print(sum(c[1:]))
print(*ans) 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) [] lines = [[] for _ in range(N)] lines [[] for _ in range(N)] for i in range(N-1):
    a,b = map(int,input().split())
    lines[a-1].append(b-1)
    lines[b-1].append(a-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 lines[a-1].append(b-1) [a-1].append [a-1] lines a-1 a 1 append b-1 b 1 lines[b-1].append(a-1) [b-1].append [b-1] lines 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 not_visited = [-1]*N not_visited [-1]*N [-1] -1 N not_visited[0] = 0 not_visited[0] not_visited 0 0 stack = deque() stack deque() deque stack.append(0) stack.append stack append 0 ans = [0]*N ans [0]*N [0] 0 N ans[0] = c[0] ans[0] ans 0 c[0] c 0 cut = 1 cut 1 while stack:
    now = stack.popleft()
    for nex in lines[now]:
        if not_visited[nex] == -1:
            ans[nex] = c[cut]
            not_visited[nex] =  not_visited[now] + 1
            cut += 1
            stack.append(nex) stack now = stack.popleft() now stack.popleft() stack.popleft stack popleft for nex in lines[now]:
        if not_visited[nex] == -1:
            ans[nex] = c[cut]
            not_visited[nex] =  not_visited[now] + 1
            cut += 1
            stack.append(nex) nex lines[now] lines now if not_visited[nex] == -1:
            ans[nex] = c[cut]
            not_visited[nex] =  not_visited[now] + 1
            cut += 1
            stack.append(nex) not_visited[nex] == -1 not_visited[nex] not_visited nex -1 ans[nex] = c[cut] ans[nex] ans nex c[cut] c cut not_visited[nex] =  not_visited[now] + 1 not_visited[nex] not_visited nex not_visited[now] + 1 not_visited[now] not_visited now 1 cut += 1 cut 1 stack.append(nex) stack.append stack append nex print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(*ans) print *ans ans ans = [0]*N [0]*N ans now = stack.popleft() stack.popleft() now c = list(map(int,input().split())) list(map(int,input().split())) c not_visited = [-1]*N [-1]*N not_visited N = int(input()) int(input()) N cut += 1 1 cut a,b = map(int,input().split()) map(int,input().split()) a lines = [[] for _ in range(N)] [[] for _ in range(N)] lines b = map(int,input().split()) map(int,input().split()) b cut = 1 1 cut stack = deque() deque() stack
from collections import deque

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

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

not_visited = [-1]*N
not_visited[0] = 0
stack = deque()
stack.append(0)

ans = [0]*N
ans[0] = c[0]

cut = 1
while stack:
    now = stack.popleft()
    for nex in lines[now]:
        if not_visited[nex] == -1:
            ans[nex] = c[cut]
            not_visited[nex] =  not_visited[now] + 1
            cut += 1
            stack.append(nex)

print(sum(c[1:]))
print(*ans)