import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import deque

n = int(readline())
graph = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, readline().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
c = list(map(int, readline().split()))
c.sort()
print(sum(c[:-1]))
q = deque([0])
d = [-1] * n

while q:
    x = q.popleft()
    d[x] = c.pop()
    for next in graph[x]:
        if d[next] < 0:
            q.appendleft(next)

print(*d) import sys sys read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 from collections import deque n = int(readline()) n int(readline()) int readline() readline [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] graph = [[] for _ in range(n)] graph [[] for _ in range(n)] for i in range(n - 1):
    a, b = map(int, readline().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1) i range(n - 1) range n - 1 n 1 a, b = map(int, readline().split()) a map(int, readline().split()) map int readline().split() ().split () readline split b graph[a - 1].append(b - 1) [a - 1].append [a - 1] graph a - 1 a 1 append b - 1 b 1 graph[b - 1].append(a - 1) [b - 1].append [b - 1] graph b - 1 b 1 append a - 1 a 1 c = list(map(int, readline().split())) c list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split c.sort() c.sort c sort print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 q = deque([0]) q deque([0]) deque [0] 0 d = [-1] * n d [-1] * n [-1] -1 n while q:
    x = q.popleft()
    d[x] = c.pop()
    for next in graph[x]:
        if d[next] < 0:
            q.appendleft(next) q x = q.popleft() x q.popleft() q.popleft q popleft d[x] = c.pop() d[x] d x c.pop() c.pop c pop for next in graph[x]:
        if d[next] < 0:
            q.appendleft(next) next graph[x] graph x if d[next] < 0:
            q.appendleft(next) d[next] < 0 d[next] d next 0 q.appendleft(next) q.appendleft q appendleft next print(*d) print *d d a, b = map(int, readline().split()) map(int, readline().split()) a readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines n = int(readline()) int(readline()) n q = deque([0]) deque([0]) q c = list(map(int, readline().split())) list(map(int, readline().split())) c graph = [[] for _ in range(n)] [[] for _ in range(n)] graph readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline d = [-1] * n [-1] * n d b = map(int, readline().split()) map(int, readline().split()) b x = q.popleft() q.popleft() x read = sys.stdin.buffer.read sys.stdin.buffer.read read
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import deque

n = int(readline())
graph = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, readline().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
c = list(map(int, readline().split()))
c.sort()
print(sum(c[:-1]))
q = deque([0])
d = [-1] * n

while q:
    x = q.popleft()
    d[x] = c.pop()
    for next in graph[x]:
        if d[next] < 0:
            q.appendleft(next)

print(*d)
