import sys
input = sys.stdin.buffer.readline

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

s = []
parent = [-1]*n
order = []
s.append(0)
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if parent[v] == u:
            continue
        parent[u] = v
        s.append(u)

order.reverse()
ans = 0
D = [0]*n
for i in range(n):
    v = order[i]
    D[v] = C[i]
    if i != n-1:
        ans += C[i]
print(ans)
print(*D) import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline 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 i in range(n-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].append(b)
    g[b].append(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 a, b = a-1, b-1 a a-1 a 1 b b-1 b 1 g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g b append a 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() C.sort C sort s = [] s [] parent = [-1]*n parent [-1]*n [-1] -1 n order = [] order [] s.append(0) s.append s append 0 while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if parent[v] == u:
            continue
        parent[u] = v
        s.append(u) s v = s.pop() v s.pop() s.pop s pop order.append(v) order.append order append v for u in g[v]:
        if parent[v] == u:
            continue
        parent[u] = v
        s.append(u) u g[v] g v if parent[v] == u:
            continue
         parent[v] == u parent[v] parent v u continue parent[u] = v parent[u] parent u v s.append(u) s.append s append u order.reverse() order.reverse order reverse ans = 0 ans 0 D = [0]*n D [0]*n [0] 0 n for i in range(n):
    v = order[i]
    D[v] = C[i]
    if i != n-1:
        ans += C[i] i range(n) range n v = order[i] v order[i] order i D[v] = C[i] D[v] D v C[i] C i if i != n-1:
        ans += C[i] i != n-1 i n-1 n 1 ans += C[i] ans C[i] C i print(ans) print ans print(*D) print *D D ans += C[i] C[i] ans order = [] [] order input = sys.stdin.buffer.readline sys.stdin.buffer.readline input b = a-1, b-1 b-1 b C = list(map(int, input().split())) list(map(int, input().split())) C D = [0]*n [0]*n D v = order[i] order[i] v a, b = a-1, b-1 a-1 a n = int(input()) int(input()) n v = s.pop() s.pop() v b = map(int, input().split()) map(int, input().split()) b s = [] [] s a, b = map(int, input().split()) map(int, input().split()) a ans = 0 0 ans g = [[] for _ in range(n)] [[] for _ in range(n)] g parent = [-1]*n [-1]*n parent
import sys
input = sys.stdin.buffer.readline

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

s = []
parent = [-1]*n
order = []
s.append(0)
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if parent[v] == u:
            continue
        parent[u] = v
        s.append(u)

order.reverse()
ans = 0
D = [0]*n
for i in range(n):
    v = order[i]
    D[v] = C[i]
    if i != n-1:
        ans += C[i]
print(ans)
print(*D)
