import sys
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
nodes = [set() for _ in range(N+1)]

for _ in range(N-1):
    a, b = rl()
    nodes[a].add(b)
    nodes[b].add(a)

C = rl()
C.sort(reverse=True)
score = sum(C[1:])
deg = [len(x) for x in nodes]
deg_1 = [i for i, x in enumerate(deg) if x == 1]
answer = [0] * (N+1)

while deg_1:
    v = deg_1.pop()
    if not deg_1:
        answer[v] = C.pop()
        break
    w = nodes[v].pop()
    nodes[w].remove(v)
    deg[w] -= 1
    if deg[w] == 1:
        deg_1.append(w)
    answer[v] = C.pop()

print(score)
print(*answer[1:]) import sys sys stdin = sys.stdin stdin sys.stdin sys stdin ri = lambda: int(rs()) ri lambda: int(rs()) int(rs()) int rs() rs rl = lambda: list(map(int, stdin.readline().split())) rl lambda: list(map(int, stdin.readline().split())) list(map(int, stdin.readline().split())) list map(int, stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split rs = lambda: stdin.readline().rstrip() rs lambda: stdin.readline().rstrip() stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip N = ri() N ri() ri set() for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) set() set nodes = [set() for _ in range(N+1)] nodes [set() for _ in range(N+1)] for _ in range(N-1):
    a, b = rl()
    nodes[a].add(b)
    nodes[b].add(a) _ range(N-1) range N-1 N 1 a, b = rl() a rl() rl b nodes[a].add(b) [a].add [a] nodes a add b nodes[b].add(a) [b].add [b] nodes b add a C = rl() C rl() rl C.sort(reverse=True) C.sort C sort reverse=True reverse True score = sum(C[1:]) score sum(C[1:]) sum C[1:] C 1: 1 len(x) for x in nodes for x in nodes x nodes for x in nodes len(x) len x deg = [len(x) for x in nodes] deg [len(x) for x in nodes] i for i, x in enumerate(deg) if x == 1 for i, x in enumerate(deg) if x == 1 i x enumerate(deg) enumerate deg x == 1 x 1 if x == 1 i deg_1 = [i for i, x in enumerate(deg) if x == 1] deg_1 [i for i, x in enumerate(deg) if x == 1] answer = [0] * (N+1) answer [0] * (N+1) [0] 0 N+1 N 1 while deg_1:
    v = deg_1.pop()
    if not deg_1:
        answer[v] = C.pop()
        break
    w = nodes[v].pop()
    nodes[w].remove(v)
    deg[w] -= 1
    if deg[w] == 1:
        deg_1.append(w)
    answer[v] = C.pop() deg_1 v = deg_1.pop() v deg_1.pop() deg_1.pop deg_1 pop if not deg_1:
        answer[v] = C.pop()
        break
     not deg_1 deg_1 answer[v] = C.pop() answer[v] answer v C.pop() C.pop C pop break w = nodes[v].pop() w nodes[v].pop() [v].pop [v] nodes v pop nodes[w].remove(v) [w].remove [w] nodes w remove v deg[w] -= 1 deg[w] deg w 1 if deg[w] == 1:
        deg_1.append(w)
     deg[w] == 1 deg[w] deg w 1 deg_1.append(w) deg_1.append deg_1 append w answer[v] = C.pop() answer[v] answer v C.pop() C.pop C pop print(score) print score print(*answer[1:]) print *answer[1:] answer[1:] answer 1: 1 rs = lambda: stdin.readline().rstrip() lambda: stdin.readline().rstrip() rs a, b = rl() rl() a v = deg_1.pop() deg_1.pop() v deg = [len(x) for x in nodes] [len(x) for x in nodes] deg answer = [0] * (N+1) [0] * (N+1) answer w = nodes[v].pop() nodes[v].pop() w stdin = sys.stdin sys.stdin stdin nodes = [set() for _ in range(N+1)] [set() for _ in range(N+1)] nodes b = rl() rl() b score = sum(C[1:]) sum(C[1:]) score deg_1 = [i for i, x in enumerate(deg) if x == 1] [i for i, x in enumerate(deg) if x == 1] deg_1 ri = lambda: int(rs()) lambda: int(rs()) ri C = rl() rl() C rl = lambda: list(map(int, stdin.readline().split())) lambda: list(map(int, stdin.readline().split())) rl N = ri() ri() N
import sys
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
nodes = [set() for _ in range(N+1)]

for _ in range(N-1):
    a, b = rl()
    nodes[a].add(b)
    nodes[b].add(a)

C = rl()
C.sort(reverse=True)
score = sum(C[1:])
deg = [len(x) for x in nodes]
deg_1 = [i for i, x in enumerate(deg) if x == 1]
answer = [0] * (N+1)

while deg_1:
    v = deg_1.pop()
    if not deg_1:
        answer[v] = C.pop()
        break
    w = nodes[v].pop()
    nodes[w].remove(v)
    deg[w] -= 1
    if deg[w] == 1:
        deg_1.append(w)
    answer[v] = C.pop()

print(score)
print(*answer[1:])
