from collections import deque

n = int(input())
Adj_dict = {x:[] for x in range(1, n+1)}

for _ in range(n-1):
    a, b = map(int, input().split())
    Adj_dict[a].append(b)
    Adj_dict[b].append(a)
C = list(map(int, input().split()))
C = sorted(C, reverse=True)
dq_C = deque(C)

max_node = 0
max_edge_count = 0
for i in range(1,n+1):
    if len(Adj_dict[i]) >= max_edge_count:
        max_edge_count = len(Adj_dict[i])
        max_node = i

dq = deque()
dq.append(max_node)

Visited = [-1 for _ in range(n+1)]
Visited[max_node] = dq_C.popleft()
ans = 0

while dq:
    node = dq.popleft()
    Adjs = Adj_dict[node]
    for next_node in Adjs:
        if Visited[next_node] != -1:
            continue
        dq.append(next_node)
        Visited[next_node] = dq_C.popleft()
        ans += Visited[next_node]

print(ans)
print(' '.join(map(str, Visited[1:]))) from collections import deque n = int(input()) n int(input()) int input() input x:[] for x in range(1, n+1) for x in range(1, n+1) x range(1, n+1) range 1 n+1 n 1 for x in range(1, n+1) x [] Adj_dict = {x:[] for x in range(1, n+1)} Adj_dict {x:[] for x in range(1, n+1)} for _ in range(n-1):
    a, b = map(int, input().split())
    Adj_dict[a].append(b)
    Adj_dict[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 Adj_dict[a].append(b) [a].append [a] Adj_dict a append b Adj_dict[b].append(a) [b].append [b] Adj_dict 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 = sorted(C, reverse=True) C sorted(C, reverse=True) sorted C reverse=True reverse True dq_C = deque(C) dq_C deque(C) deque C max_node = 0 max_node 0 max_edge_count = 0 max_edge_count 0 for i in range(1,n+1):
    if len(Adj_dict[i]) >= max_edge_count:
        max_edge_count = len(Adj_dict[i])
        max_node = i i range(1,n+1) range 1 n+1 n 1 if len(Adj_dict[i]) >= max_edge_count:
        max_edge_count = len(Adj_dict[i])
        max_node = i len(Adj_dict[i]) >= max_edge_count len(Adj_dict[i]) len Adj_dict[i] Adj_dict i max_edge_count max_edge_count = len(Adj_dict[i]) max_edge_count len(Adj_dict[i]) len Adj_dict[i] Adj_dict i max_node = i max_node i dq = deque() dq deque() deque dq.append(max_node) dq.append dq append max_node -1 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) -1 Visited = [-1 for _ in range(n+1)] Visited [-1 for _ in range(n+1)] Visited[max_node] = dq_C.popleft() Visited[max_node] Visited max_node dq_C.popleft() dq_C.popleft dq_C popleft ans = 0 ans 0 while dq:
    node = dq.popleft()
    Adjs = Adj_dict[node]
    for next_node in Adjs:
        if Visited[next_node] != -1:
            continue
        dq.append(next_node)
        Visited[next_node] = dq_C.popleft()
        ans += Visited[next_node] dq node = dq.popleft() node dq.popleft() dq.popleft dq popleft Adjs = Adj_dict[node] Adjs Adj_dict[node] Adj_dict node for next_node in Adjs:
        if Visited[next_node] != -1:
            continue
        dq.append(next_node)
        Visited[next_node] = dq_C.popleft()
        ans += Visited[next_node] next_node Adjs if Visited[next_node] != -1:
            continue
         Visited[next_node] != -1 Visited[next_node] Visited next_node -1 continue dq.append(next_node) dq.append dq append next_node Visited[next_node] = dq_C.popleft() Visited[next_node] Visited next_node dq_C.popleft() dq_C.popleft dq_C popleft ans += Visited[next_node] ans Visited[next_node] Visited next_node print(ans) print ans print(' '.join(map(str, Visited[1:]))) print ' '.join(map(str, Visited[1:])) ' '.join ' ' join map(str, Visited[1:]) map str Visited[1:] Visited 1: 1 C = list(map(int, input().split())) list(map(int, input().split())) C dq_C = deque(C) deque(C) dq_C ans = 0 0 ans max_node = 0 0 max_node node = dq.popleft() dq.popleft() node Adjs = Adj_dict[node] Adj_dict[node] Adjs b = map(int, input().split()) map(int, input().split()) b max_node = i i max_node n = int(input()) int(input()) n max_edge_count = 0 0 max_edge_count Visited = [-1 for _ in range(n+1)] [-1 for _ in range(n+1)] Visited Adj_dict = {x:[] for x in range(1, n+1)} {x:[] for x in range(1, n+1)} Adj_dict C = sorted(C, reverse=True) sorted(C, reverse=True) C ans += Visited[next_node] Visited[next_node] ans max_edge_count = len(Adj_dict[i]) len(Adj_dict[i]) max_edge_count dq = deque() deque() dq a, b = map(int, input().split()) map(int, input().split()) a
from collections import deque

n = int(input())
Adj_dict = {x:[] for x in range(1, n+1)}

for _ in range(n-1):
    a, b = map(int, input().split())
    Adj_dict[a].append(b)
    Adj_dict[b].append(a)
C = list(map(int, input().split()))
C = sorted(C, reverse=True)
dq_C = deque(C)

max_node = 0
max_edge_count = 0
for i in range(1,n+1):
    if len(Adj_dict[i]) >= max_edge_count:
        max_edge_count = len(Adj_dict[i])
        max_node = i

dq = deque()
dq.append(max_node)

Visited = [-1 for _ in range(n+1)]
Visited[max_node] = dq_C.popleft()
ans = 0

while dq:
    node = dq.popleft()
    Adjs = Adj_dict[node]
    for next_node in Adjs:
        if Visited[next_node] != -1:
            continue
        dq.append(next_node)
        Visited[next_node] = dq_C.popleft()
        ans += Visited[next_node]

print(ans)
print(' '.join(map(str, Visited[1:])))
