import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
edges = [set() for _ in range(N+1)] # 1-indexed
for _ in range(N-1):
    a, b = lr()
    edges[a].add(b)
    edges[b].add(a)
    
C = lr()
C.sort(reverse=True)
degrees = [len(x) for x in edges]
deg_1 = [i for i, d in enumerate(degrees) if d == 1]
score = sum(C[1:])
answer = [None] * (N+1)

# start from every leaf
while deg_1:
    leaf = deg_1.pop()
    answer[leaf] = C.pop()
    if not deg_1:
        break
    cand = edges[leaf].pop()
    edges[cand].remove(leaf)
    degrees[cand] -= 1
    if degrees[cand] == 1:
        deg_1.append(cand)

print(score)
print(*answer[1:]) import sys sys sr = lambda: sys.stdin.readline().rstrip() sr lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip ir = lambda: int(sr()) ir lambda: int(sr()) int(sr()) int sr() sr lr = lambda: list(map(int, sr().split())) lr lambda: list(map(int, sr().split())) list(map(int, sr().split())) list map(int, sr().split()) map int sr().split() ().split () sr split N = ir() N ir() ir 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 edges = [set() for _ in range(N+1)] edges [set() for _ in range(N+1)] for _ in range(N-1):
    a, b = lr()
    edges[a].add(b)
    edges[b].add(a)
     _ range(N-1) range N-1 N 1 a, b = lr() a lr() lr b edges[a].add(b) [a].add [a] edges a add b edges[b].add(a) [b].add [b] edges b add a C = lr() C lr() lr C.sort(reverse=True) C.sort C sort reverse=True reverse True len(x) for x in edges for x in edges x edges for x in edges len(x) len x degrees = [len(x) for x in edges] degrees [len(x) for x in edges] i for i, d in enumerate(degrees) if d == 1 for i, d in enumerate(degrees) if d == 1 i d enumerate(degrees) enumerate degrees d == 1 d 1 if d == 1 i deg_1 = [i for i, d in enumerate(degrees) if d == 1] deg_1 [i for i, d in enumerate(degrees) if d == 1] score = sum(C[1:]) score sum(C[1:]) sum C[1:] C 1: 1 answer = [None] * (N+1) answer [None] * (N+1) [None] None N+1 N 1 while deg_1:
    leaf = deg_1.pop()
    answer[leaf] = C.pop()
    if not deg_1:
        break
    cand = edges[leaf].pop()
    edges[cand].remove(leaf)
    degrees[cand] -= 1
    if degrees[cand] == 1:
        deg_1.append(cand) deg_1 leaf = deg_1.pop() leaf deg_1.pop() deg_1.pop deg_1 pop answer[leaf] = C.pop() answer[leaf] answer leaf C.pop() C.pop C pop if not deg_1:
        break
     not deg_1 deg_1 break cand = edges[leaf].pop() cand edges[leaf].pop() [leaf].pop [leaf] edges leaf pop edges[cand].remove(leaf) [cand].remove [cand] edges cand remove leaf degrees[cand] -= 1 degrees[cand] degrees cand 1 if degrees[cand] == 1:
        deg_1.append(cand) degrees[cand] == 1 degrees[cand] degrees cand 1 deg_1.append(cand) deg_1.append deg_1 append cand print(score) print score print(*answer[1:]) print *answer[1:] answer[1:] answer 1: 1 answer = [None] * (N+1) [None] * (N+1) answer N = ir() ir() N leaf = deg_1.pop() deg_1.pop() leaf a, b = lr() lr() a score = sum(C[1:]) sum(C[1:]) score deg_1 = [i for i, d in enumerate(degrees) if d == 1] [i for i, d in enumerate(degrees) if d == 1] deg_1 degrees = [len(x) for x in edges] [len(x) for x in edges] degrees ir = lambda: int(sr()) lambda: int(sr()) ir lr = lambda: list(map(int, sr().split())) lambda: list(map(int, sr().split())) lr edges = [set() for _ in range(N+1)] [set() for _ in range(N+1)] edges C = lr() lr() C b = lr() lr() b sr = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() sr cand = edges[leaf].pop() edges[leaf].pop() cand
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
edges = [set() for _ in range(N+1)] # 1-indexed
for _ in range(N-1):
    a, b = lr()
    edges[a].add(b)
    edges[b].add(a)
    
C = lr()
C.sort(reverse=True)
degrees = [len(x) for x in edges]
deg_1 = [i for i, d in enumerate(degrees) if d == 1]
score = sum(C[1:])
answer = [None] * (N+1)

# start from every leaf
while deg_1:
    leaf = deg_1.pop()
    answer[leaf] = C.pop()
    if not deg_1:
        break
    cand = edges[leaf].pop()
    edges[cand].remove(leaf)
    degrees[cand] -= 1
    if degrees[cand] == 1:
        deg_1.append(cand)

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