from collections import defaultdict

V = defaultdict(list)
N = int(input())
for _ in range(N-1):
    a, b = map(int, input().split())
    V[a].append(b)
    V[b].append(a)
*c, = map(int, input().split())
c.sort(reverse=True)

root, _ = max(V.items(), key=lambda x:len(x[1]))
visited = [False] * (N+1)
identifier = [0] * (N+1)

p = 0
q = [root]
while q:
    a = q.pop()
    visited[a] = True
    identifier[a] = p

    p+=1

    for i in V[a]:
        if visited[i]:continue
        q.append(i)

cost = 0
visited = [False] * (N+1)
q = [root]
while q:
    a = q.pop()
    visited[a] = True
    p+=1

    for b in V[a]:
        if visited[b]:continue
        cost += min(c[identifier[a]], c[identifier[b]])
        q.append(b)

print(cost)
print(*[c[i] for i in identifier[1:]]) from collections import defaultdict V = defaultdict(list) V defaultdict(list) defaultdict list N = int(input()) N int(input()) int input() input for _ in range(N-1):
    a, b = map(int, input().split())
    V[a].append(b)
    V[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 V[a].append(b) [a].append [a] V a append b V[b].append(a) [b].append [b] V b append a *c, = map(int, input().split()) *c c map(int, input().split()) map int input().split() ().split () input split c.sort(reverse=True) c.sort c sort reverse=True reverse True root, _ = max(V.items(), key=lambda x:len(x[1])) root max(V.items(), key=lambda x:len(x[1])) max V.items() V.items V items key=lambda x:len(x[1]) key lambda x:len(x[1]) len(x[1]) len x[1] x 1 x _ visited = [False] * (N+1) visited [False] * (N+1) [False] False N+1 N 1 identifier = [0] * (N+1) identifier [0] * (N+1) [0] 0 N+1 N 1 p = 0 p 0 q = [root] q [root] root while q:
    a = q.pop()
    visited[a] = True
    identifier[a] = p

    p+=1

    for i in V[a]:
        if visited[i]:continue
        q.append(i) q a = q.pop() a q.pop() q.pop q pop visited[a] = True visited[a] visited a True identifier[a] = p identifier[a] identifier a p p+=1 p 1 for i in V[a]:
        if visited[i]:continue
        q.append(i) i V[a] V a if visited[i]:continue
         visited[i] visited i q.append(i) q.append q append i cost = 0 cost 0 visited = [False] * (N+1) visited [False] * (N+1) [False] False N+1 N 1 q = [root] q [root] root while q:
    a = q.pop()
    visited[a] = True
    p+=1

    for b in V[a]:
        if visited[b]:continue
        cost += min(c[identifier[a]], c[identifier[b]])
        q.append(b) q a = q.pop() a q.pop() q.pop q pop visited[a] = True visited[a] visited a True p+=1 p 1 for b in V[a]:
        if visited[b]:continue
        cost += min(c[identifier[a]], c[identifier[b]])
        q.append(b) b V[a] V a if visited[b]:continue
         visited[b] visited b cost += min(c[identifier[a]], c[identifier[b]]) cost min(c[identifier[a]], c[identifier[b]]) min c[identifier[a]] c identifier[a] identifier a c[identifier[b]] c identifier[b] identifier b q.append(b) q.append q append b print(cost) print cost c[i] for i in identifier[1:] for i in identifier[1:] i identifier[1:] identifier 1: 1 for i in identifier[1:] c[i] c i print(*[c[i] for i in identifier[1:]]) print *[c[i] for i in identifier[1:]] [c[i] for i in identifier[1:]] a = q.pop() q.pop() a p+=1 1 p cost = 0 0 cost V = defaultdict(list) defaultdict(list) V p+=1 1 p cost += min(c[identifier[a]], c[identifier[b]]) min(c[identifier[a]], c[identifier[b]]) cost N = int(input()) int(input()) N q = [root] [root] q a, b = map(int, input().split()) map(int, input().split()) a p = 0 0 p identifier = [0] * (N+1) [0] * (N+1) identifier a = q.pop() q.pop() a root, _ = max(V.items(), key=lambda x:len(x[1])) max(V.items(), key=lambda x:len(x[1])) root visited = [False] * (N+1) [False] * (N+1) visited _ = max(V.items(), key=lambda x:len(x[1])) max(V.items(), key=lambda x:len(x[1])) _ visited = [False] * (N+1) [False] * (N+1) visited q = [root] [root] q b = map(int, input().split()) map(int, input().split()) b
from collections import defaultdict

V = defaultdict(list)
N = int(input())
for _ in range(N-1):
    a, b = map(int, input().split())
    V[a].append(b)
    V[b].append(a)
*c, = map(int, input().split())
c.sort(reverse=True)

root, _ = max(V.items(), key=lambda x:len(x[1]))
visited = [False] * (N+1)
identifier = [0] * (N+1)

p = 0
q = [root]
while q:
    a = q.pop()
    visited[a] = True
    identifier[a] = p

    p+=1

    for i in V[a]:
        if visited[i]:continue
        q.append(i)

cost = 0
visited = [False] * (N+1)
q = [root]
while q:
    a = q.pop()
    visited[a] = True
    p+=1

    for b in V[a]:
        if visited[b]:continue
        cost += min(c[identifier[a]], c[identifier[b]])
        q.append(b)

print(cost)
print(*[c[i] for i in identifier[1:]])