#!/usr/bin/env python
from collections import defaultdict

N = int(input())

ab = []
for _ in range(N - 1):
    ab.append(tuple(map(int, input().split())))
c = sorted(list(map(int, input().split())), reverse=True)

nodes = defaultdict(set)
for a, b in ab:
    nodes[a].add(b)
    nodes[b].add(a)

d = {}
done = set()
stack = [1]
ci = 0
node = 1
while stack:
    node = stack.pop()
    d[node] = c[ci]
    stack += list(nodes[node] - done)
    done.add(node)
    ci += 1

print(sum(c[1:]))
for i in range(N):
    print(d[i + 1], end=' ')
print()
 from collections import defaultdict N = int(input()) N int(input()) int input() input ab = [] ab [] for _ in range(N - 1):
    ab.append(tuple(map(int, input().split()))) _ range(N - 1) range N - 1 N 1 ab.append(tuple(map(int, input().split()))) ab.append ab append tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split c = sorted(list(map(int, input().split())), reverse=True) c sorted(list(map(int, input().split())), reverse=True) sorted list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split reverse=True reverse True nodes = defaultdict(set) nodes defaultdict(set) defaultdict set for a, b in ab:
    nodes[a].add(b)
    nodes[b].add(a) a b ab nodes[a].add(b) [a].add [a] nodes a add b nodes[b].add(a) [b].add [b] nodes b add a d = {} d {} done = set() done set() set stack = [1] stack [1] 1 ci = 0 ci 0 node = 1 node 1 while stack:
    node = stack.pop()
    d[node] = c[ci]
    stack += list(nodes[node] - done)
    done.add(node)
    ci += 1 stack node = stack.pop() node stack.pop() stack.pop stack pop d[node] = c[ci] d[node] d node c[ci] c ci stack += list(nodes[node] - done) stack list(nodes[node] - done) list nodes[node] - done nodes[node] nodes node done done.add(node) done.add done add node ci += 1 ci 1 print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 for i in range(N):
    print(d[i + 1], end=' ') i range(N) range N print(d[i + 1], end=' ') print d[i + 1] d i + 1 i 1 end=' ' end ' ' print() print node = 1 1 node nodes = defaultdict(set) defaultdict(set) nodes d = {} {} d stack = [1] [1] stack node = stack.pop() stack.pop() node ci += 1 1 ci done = set() set() done N = int(input()) int(input()) N stack += list(nodes[node] - done) list(nodes[node] - done) stack ab = [] [] ab c = sorted(list(map(int, input().split())), reverse=True) sorted(list(map(int, input().split())), reverse=True) c ci = 0 0 ci
#!/usr/bin/env python
from collections import defaultdict

N = int(input())

ab = []
for _ in range(N - 1):
    ab.append(tuple(map(int, input().split())))
c = sorted(list(map(int, input().split())), reverse=True)

nodes = defaultdict(set)
for a, b in ab:
    nodes[a].add(b)
    nodes[b].add(a)

d = {}
done = set()
stack = [1]
ci = 0
node = 1
while stack:
    node = stack.pop()
    d[node] = c[ci]
    stack += list(nodes[node] - done)
    done.add(node)
    ci += 1

print(sum(c[1:]))
for i in range(N):
    print(d[i + 1], end=' ')
print()

