from collections import deque
n = int(input())
g = [[] for _ in range(n)]

for _ in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

c = [int(x) for x in input().split()]
c = sorted(c, reverse=True)

q = deque([0])
visited = [False]*n
ans = [-1]*n
ans[0] = c[0]
i = 1

while q:
    v = q.popleft()
    visited[v] = True
    for w in g[v]:
        if not visited[w]:
            ans[w] = c[i]
            i += 1
            q.append(w)

print(sum(c[1:]))
print(*ans) from collections import deque n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] g = [[] for _ in range(n)] g [[] for _ in range(n)] for _ in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1) _ 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 g[a-1].append(b-1) [a-1].append [a-1] g a-1 a 1 append b-1 b 1 g[b-1].append(a-1) [b-1].append [b-1] g b-1 b 1 append a-1 a 1 int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x c = [int(x) for x in input().split()] c [int(x) for x in input().split()] c = sorted(c, reverse=True) c sorted(c, reverse=True) sorted c reverse=True reverse True q = deque([0]) q deque([0]) deque [0] 0 visited = [False]*n visited [False]*n [False] False n ans = [-1]*n ans [-1]*n [-1] -1 n ans[0] = c[0] ans[0] ans 0 c[0] c 0 i = 1 i 1 while q:
    v = q.popleft()
    visited[v] = True
    for w in g[v]:
        if not visited[w]:
            ans[w] = c[i]
            i += 1
            q.append(w) q v = q.popleft() v q.popleft() q.popleft q popleft visited[v] = True visited[v] visited v True for w in g[v]:
        if not visited[w]:
            ans[w] = c[i]
            i += 1
            q.append(w) w g[v] g v if not visited[w]:
            ans[w] = c[i]
            i += 1
            q.append(w) not visited[w] visited[w] visited w ans[w] = c[i] ans[w] ans w c[i] c i i += 1 i 1 q.append(w) q.append q append w print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(*ans) print *ans ans i += 1 1 i v = q.popleft() q.popleft() v b = map(int, input().split()) map(int, input().split()) b i = 1 1 i g = [[] for _ in range(n)] [[] for _ in range(n)] g visited = [False]*n [False]*n visited q = deque([0]) deque([0]) q n = int(input()) int(input()) n c = sorted(c, reverse=True) sorted(c, reverse=True) c a, b = map(int, input().split()) map(int, input().split()) a c = [int(x) for x in input().split()] [int(x) for x in input().split()] c ans = [-1]*n [-1]*n ans
from collections import deque
n = int(input())
g = [[] for _ in range(n)]

for _ in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

c = [int(x) for x in input().split()]
c = sorted(c, reverse=True)

q = deque([0])
visited = [False]*n
ans = [-1]*n
ans[0] = c[0]
i = 1

while q:
    v = q.popleft()
    visited[v] = True
    for w in g[v]:
        if not visited[w]:
            ans[w] = c[i]
            i += 1
            q.append(w)

print(sum(c[1:]))
print(*ans)
