from collections import deque

N = int(input())

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

print(sum(C) - max(C))
    
Q = deque()
Q.append(N - 1)
order = []
visited = [False] * N
while len(Q) != 0:
    q = Q.popleft()
    visited[q] = True
    c = C.pop()
    order.append([q, c])
    for g in G[q]:
        if visited[g] == False:
            Q.append(g)

sorted_order = sorted(order)
ans2 = []
for i in range(N):
    ans2.append(sorted_order[i][1])
    
print(' '.join(map(str, ans2))) 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)] for _ in range(N - 1):
    a, b = map(int, input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 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 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()]) C sorted([int(i) for i in input().split()]) sorted [int(i) for i in input().split()] print(sum(C) - max(C)) print sum(C) - max(C) sum(C) sum C max(C) max C Q = deque() Q deque() deque Q.append(N - 1) Q.append Q append N - 1 N 1 order = [] order [] visited = [False] * N visited [False] * N [False] False N while len(Q) != 0:
    q = Q.popleft()
    visited[q] = True
    c = C.pop()
    order.append([q, c])
    for g in G[q]:
        if visited[g] == False:
            Q.append(g) len(Q) != 0 len(Q) len Q 0 q = Q.popleft() q Q.popleft() Q.popleft Q popleft visited[q] = True visited[q] visited q True c = C.pop() c C.pop() C.pop C pop order.append([q, c]) order.append order append [q, c] q c for g in G[q]:
        if visited[g] == False:
            Q.append(g) g G[q] G q if visited[g] == False:
            Q.append(g) visited[g] == False visited[g] visited g False Q.append(g) Q.append Q append g sorted_order = sorted(order) sorted_order sorted(order) sorted order ans2 = [] ans2 [] for i in range(N):
    ans2.append(sorted_order[i][1])
     i range(N) range N ans2.append(sorted_order[i][1]) ans2.append ans2 append sorted_order[i][1] [i] sorted_order i 1 print(' '.join(map(str, ans2))) print ' '.join(map(str, ans2)) ' '.join ' ' join map(str, ans2) map str ans2 C = sorted([int(i) for i in input().split()]) sorted([int(i) for i in input().split()]) C order = [] [] order ans2 = [] [] ans2 N = int(input()) int(input()) N G = [[] for i in range(N)] [[] for i in range(N)] G Q = deque() deque() Q q = Q.popleft() Q.popleft() q a, b = map(int, input().split()) map(int, input().split()) a sorted_order = sorted(order) sorted(order) sorted_order visited = [False] * N [False] * N visited c = C.pop() C.pop() c b = map(int, input().split()) map(int, input().split()) b
from collections import deque

N = int(input())

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

print(sum(C) - max(C))
    
Q = deque()
Q.append(N - 1)
order = []
visited = [False] * N
while len(Q) != 0:
    q = Q.popleft()
    visited[q] = True
    c = C.pop()
    order.append([q, c])
    for g in G[q]:
        if visited[g] == False:
            Q.append(g)

sorted_order = sorted(order)
ans2 = []
for i in range(N):
    ans2.append(sorted_order[i][1])
    
print(' '.join(map(str, ans2)))