from collections import defaultdict, deque

N = int(input())
X = defaultdict(list)
for i in range(N - 1):
    ai, bi = map(lambda s: int(s) - 1, input().split())
    X[ai].append(bi)
    X[bi].append(ai)
c = list(map(int, input().split()))

c.sort(reverse=True)
print(sum(c[1:]))
d = [0] * N
q = deque([0])
i = 0
while len(q) > 0:
    v = q.popleft()
    d[v] = c[i]
    i += 1
    for x in X[v]:
        if d[x] != 0:
            continue
        q.append(x)
print(' '.join(list(map(str, d)))) from collections import defaultdict, deque N = int(input()) N int(input()) int input() input X = defaultdict(list) X defaultdict(list) defaultdict list for i in range(N - 1):
    ai, bi = map(lambda s: int(s) - 1, input().split())
    X[ai].append(bi)
    X[bi].append(ai) i range(N - 1) range N - 1 N 1 ai, bi = map(lambda s: int(s) - 1, input().split()) ai map(lambda s: int(s) - 1, input().split()) map lambda s: int(s) - 1 int(s) - 1 int(s) int s 1 s input().split() ().split () input split bi X[ai].append(bi) [ai].append [ai] X ai append bi X[bi].append(ai) [bi].append [bi] X bi append ai c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort(reverse=True) c.sort c sort reverse=True reverse True print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 d = [0] * N d [0] * N [0] 0 N q = deque([0]) q deque([0]) deque [0] 0 i = 0 i 0 while len(q) > 0:
    v = q.popleft()
    d[v] = c[i]
    i += 1
    for x in X[v]:
        if d[x] != 0:
            continue
        q.append(x) len(q) > 0 len(q) len q 0 v = q.popleft() v q.popleft() q.popleft q popleft d[v] = c[i] d[v] d v c[i] c i i += 1 i 1 for x in X[v]:
        if d[x] != 0:
            continue
        q.append(x) x X[v] X v if d[x] != 0:
            continue
         d[x] != 0 d[x] d x 0 continue q.append(x) q.append q append x print(' '.join(list(map(str, d)))) print ' '.join(list(map(str, d))) ' '.join ' ' join list(map(str, d)) list map(str, d) map str d c = list(map(int, input().split())) list(map(int, input().split())) c ai, bi = map(lambda s: int(s) - 1, input().split()) map(lambda s: int(s) - 1, input().split()) ai v = q.popleft() q.popleft() v i = 0 0 i bi = map(lambda s: int(s) - 1, input().split()) map(lambda s: int(s) - 1, input().split()) bi d = [0] * N [0] * N d i += 1 1 i N = int(input()) int(input()) N X = defaultdict(list) defaultdict(list) X q = deque([0]) deque([0]) q
from collections import defaultdict, deque

N = int(input())
X = defaultdict(list)
for i in range(N - 1):
    ai, bi = map(lambda s: int(s) - 1, input().split())
    X[ai].append(bi)
    X[bi].append(ai)
c = list(map(int, input().split()))

c.sort(reverse=True)
print(sum(c[1:]))
d = [0] * N
q = deque([0])
i = 0
while len(q) > 0:
    v = q.popleft()
    d[v] = c[i]
    i += 1
    for x in X[v]:
        if d[x] != 0:
            continue
        q.append(x)
print(' '.join(list(map(str, d))))
