from collections import deque
N = int(input())
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[b].append(a)
c = list(map(int, input().split()))
c.sort(reverse=True)
score = sum(c) - max(c)
num = [-1] * N
que = deque()
que.append(0)
i = 0
while que:
    v = que.pop()
    if num[v] != -1:
        continue
    num[v] = c[i]
    i += 1
    for nv in adj[v]:
        que.append(nv)
print(score)
print(*num) 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) [] adj = [[] for _ in range(N)] adj [[] for _ in range(N)] for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[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 a -= 1 a 1 b -= 1 b 1 adj[a].append(b) [a].append [a] adj a append b adj[b].append(a) [b].append [b] adj b append a 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 score = sum(c) - max(c) score sum(c) - max(c) sum(c) sum c max(c) max c num = [-1] * N num [-1] * N [-1] -1 N que = deque() que deque() deque que.append(0) que.append que append 0 i = 0 i 0 while que:
    v = que.pop()
    if num[v] != -1:
        continue
    num[v] = c[i]
    i += 1
    for nv in adj[v]:
        que.append(nv) que v = que.pop() v que.pop() que.pop que pop if num[v] != -1:
        continue
     num[v] != -1 num[v] num v -1 continue num[v] = c[i] num[v] num v c[i] c i i += 1 i 1 for nv in adj[v]:
        que.append(nv) nv adj[v] adj v que.append(nv) que.append que append nv print(score) print score print(*num) print *num num b = map(int, input().split()) map(int, input().split()) b score = sum(c) - max(c) sum(c) - max(c) score N = int(input()) int(input()) N adj = [[] for _ in range(N)] [[] for _ in range(N)] adj a -= 1 1 a b -= 1 1 b c = list(map(int, input().split())) list(map(int, input().split())) c que = deque() deque() que a, b = map(int, input().split()) map(int, input().split()) a i = 0 0 i v = que.pop() que.pop() v num = [-1] * N [-1] * N num i += 1 1 i
from collections import deque
N = int(input())
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[b].append(a)
c = list(map(int, input().split()))
c.sort(reverse=True)
score = sum(c) - max(c)
num = [-1] * N
que = deque()
que.append(0)
i = 0
while que:
    v = que.pop()
    if num[v] != -1:
        continue
    num[v] = c[i]
    i += 1
    for nv in adj[v]:
        que.append(nv)
print(score)
print(*num)
