from collections import deque
N=int(input())

tree = {i: set() for i in range(1, N+1)}
for i in range(N-1):
    a,b = map(int,input().split())
    tree[a].add(b)
    tree[b].add(a)

inl = list(map(int,input().split()))
inl.sort()

res = [0]*(N+1)
qu = deque([1])
res[1] = inl.pop()

while qu:
    i = qu.popleft()
    for j in tree[i]:
        if res[j] == 0:
            res[j] = inl.pop()
            qu.append(j)

print(sum(res[2:]))
for i in res[1:]:
    print(i, end=' ')
print('') from collections import deque N=int(input()) N int(input()) int input() input i: set() for i in range(1, N+1) for i in range(1, N+1) i range(1, N+1) range 1 N+1 N 1 for i in range(1, N+1) i set() set tree = {i: set() for i in range(1, N+1)} tree {i: set() for i in range(1, N+1)} for i in range(N-1):
    a,b = map(int,input().split())
    tree[a].add(b)
    tree[b].add(a) i 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 tree[a].add(b) [a].add [a] tree a add b tree[b].add(a) [b].add [b] tree b add a inl = list(map(int,input().split())) inl list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split inl.sort() inl.sort inl sort res = [0]*(N+1) res [0]*(N+1) [0] 0 N+1 N 1 qu = deque([1]) qu deque([1]) deque [1] 1 res[1] = inl.pop() res[1] res 1 inl.pop() inl.pop inl pop while qu:
    i = qu.popleft()
    for j in tree[i]:
        if res[j] == 0:
            res[j] = inl.pop()
            qu.append(j) qu i = qu.popleft() i qu.popleft() qu.popleft qu popleft for j in tree[i]:
        if res[j] == 0:
            res[j] = inl.pop()
            qu.append(j) j tree[i] tree i if res[j] == 0:
            res[j] = inl.pop()
            qu.append(j) res[j] == 0 res[j] res j 0 res[j] = inl.pop() res[j] res j inl.pop() inl.pop inl pop qu.append(j) qu.append qu append j print(sum(res[2:])) print sum(res[2:]) sum res[2:] res 2: 2 for i in res[1:]:
    print(i, end=' ') i res[1:] res 1: 1 print(i, end=' ') print i end=' ' end ' ' print('') print '' b = map(int,input().split()) map(int,input().split()) b i = qu.popleft() qu.popleft() i a,b = map(int,input().split()) map(int,input().split()) a res = [0]*(N+1) [0]*(N+1) res tree = {i: set() for i in range(1, N+1)} {i: set() for i in range(1, N+1)} tree N=int(input()) int(input()) N inl = list(map(int,input().split())) list(map(int,input().split())) inl qu = deque([1]) deque([1]) qu
from collections import deque
N=int(input())

tree = {i: set() for i in range(1, N+1)}
for i in range(N-1):
    a,b = map(int,input().split())
    tree[a].add(b)
    tree[b].add(a)

inl = list(map(int,input().split()))
inl.sort()

res = [0]*(N+1)
qu = deque([1])
res[1] = inl.pop()

while qu:
    i = qu.popleft()
    for j in tree[i]:
        if res[j] == 0:
            res[j] = inl.pop()
            qu.append(j)

print(sum(res[2:]))
for i in res[1:]:
    print(i, end=' ')
print('')
