import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline

N, *abc = map(int, read().split())
c = abc[2 * (N - 1):]
c.sort()
graph = [[] for _ in range(N + 1)]
cnt = [0] * (N + 1)
for a, b in zip(*[iter(abc[:2 * (N - 1)])] * 2):
    graph[a].append(b)
    graph[b].append(a)

m = max(cnt)
nodes = [0] * (N + 1)
score = sum(c) - c[-1]
start = 1
nodes[start] = c.pop()
queue = deque([start])
while queue:
    V = queue.popleft()
    for v in graph[V]:
        if nodes[v] == 0:
            nodes[v] = c.pop()
            queue.append(v)

print(score)
print(' '.join(map(str, nodes[1:]))) import sys sys from collections import deque read = sys.stdin.read read sys.stdin.read sys.stdin sys stdin read readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline N, *abc = map(int, read().split()) N map(int, read().split()) map int read().split() ().split () read split *abc abc c = abc[2 * (N - 1):] c abc[2 * (N - 1):] abc 2 * (N - 1): 2 * (N - 1) 2 N - 1 N 1 c.sort() c.sort c sort [] for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [] graph = [[] for _ in range(N + 1)] graph [[] for _ in range(N + 1)] cnt = [0] * (N + 1) cnt [0] * (N + 1) [0] 0 N + 1 N 1 for a, b in zip(*[iter(abc[:2 * (N - 1)])] * 2):
    graph[a].append(b)
    graph[b].append(a) a b zip(*[iter(abc[:2 * (N - 1)])] * 2) zip *[iter(abc[:2 * (N - 1)])] * 2 [iter(abc[:2 * (N - 1)])] * 2 [iter(abc[:2 * (N - 1)])] iter(abc[:2 * (N - 1)]) iter abc[:2 * (N - 1)] abc :2 * (N - 1) 2 * (N - 1) 2 N - 1 N 1 2 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a m = max(cnt) m max(cnt) max cnt nodes = [0] * (N + 1) nodes [0] * (N + 1) [0] 0 N + 1 N 1 score = sum(c) - c[-1] score sum(c) - c[-1] sum(c) sum c c[-1] c -1 start = 1 start 1 nodes[start] = c.pop() nodes[start] nodes start c.pop() c.pop c pop queue = deque([start]) queue deque([start]) deque [start] start while queue:
    V = queue.popleft()
    for v in graph[V]:
        if nodes[v] == 0:
            nodes[v] = c.pop()
            queue.append(v) queue V = queue.popleft() V queue.popleft() queue.popleft queue popleft for v in graph[V]:
        if nodes[v] == 0:
            nodes[v] = c.pop()
            queue.append(v) v graph[V] graph V if nodes[v] == 0:
            nodes[v] = c.pop()
            queue.append(v) nodes[v] == 0 nodes[v] nodes v 0 nodes[v] = c.pop() nodes[v] nodes v c.pop() c.pop c pop queue.append(v) queue.append queue append v print(score) print score print(' '.join(map(str, nodes[1:]))) print ' '.join(map(str, nodes[1:])) ' '.join ' ' join map(str, nodes[1:]) map str nodes[1:] nodes 1: 1 read = sys.stdin.read sys.stdin.read read readline = sys.stdin.readline sys.stdin.readline readline m = max(cnt) max(cnt) m nodes = [0] * (N + 1) [0] * (N + 1) nodes score = sum(c) - c[-1] sum(c) - c[-1] score cnt = [0] * (N + 1) [0] * (N + 1) cnt N, *abc = map(int, read().split()) map(int, read().split()) N start = 1 1 start V = queue.popleft() queue.popleft() V c = abc[2 * (N - 1):] abc[2 * (N - 1):] c queue = deque([start]) deque([start]) queue graph = [[] for _ in range(N + 1)] [[] for _ in range(N + 1)] graph
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline

N, *abc = map(int, read().split())
c = abc[2 * (N - 1):]
c.sort()
graph = [[] for _ in range(N + 1)]
cnt = [0] * (N + 1)
for a, b in zip(*[iter(abc[:2 * (N - 1)])] * 2):
    graph[a].append(b)
    graph[b].append(a)

m = max(cnt)
nodes = [0] * (N + 1)
score = sum(c) - c[-1]
start = 1
nodes[start] = c.pop()
queue = deque([start])
while queue:
    V = queue.popleft()
    for v in graph[V]:
        if nodes[v] == 0:
            nodes[v] = c.pop()
            queue.append(v)

print(score)
print(' '.join(map(str, nodes[1:])))
