from collections import deque

n = int(input())
A = [[int(i) for i in input().split()] for j in range(n - 1)]
C = [int(i) for i in input().split()]
C.sort(reverse=True)

E = [[] for i in range(n + 1)]
V = [0] * (n + 1)

for a, b in A:
    E[a].append(b)
    E[b].append(a)

t = 0
ma = 0

for i, e in enumerate(E):
    if len(e) > ma:
        t = i
        ma = len(e)

Q = deque()
Q.append((t, 0))
i = 0
visited = [False] * (n + 1)
ans = 0

while Q:
    v, c = Q.popleft()
    if visited[v]:
        continue
    visited[v] = True
    V[v] = C[i]
    ans += min(c, C[i])
    for e in E[v]:
        Q.append((e, C[i]))
    i += 1

print(ans)
print(*V[1:]) from collections import deque n = int(input()) n int(input()) int input() input [int(i) for i in input().split()] for j in range(n - 1) for j in range(n - 1) j range(n - 1) range n - 1 n 1 for j in range(n - 1) [int(i) for i in input().split()] A = [[int(i) for i in input().split()] for j in range(n - 1)] A [[int(i) for i in input().split()] for j in range(n - 1)] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i C = [int(i) for i in input().split()] C [int(i) for i in input().split()] C.sort(reverse=True) C.sort C sort reverse=True reverse True [] for i in range(n + 1) for i in range(n + 1) i range(n + 1) range n + 1 n 1 for i in range(n + 1) [] E = [[] for i in range(n + 1)] E [[] for i in range(n + 1)] V = [0] * (n + 1) V [0] * (n + 1) [0] 0 n + 1 n 1 for a, b in A:
    E[a].append(b)
    E[b].append(a) a b A E[a].append(b) [a].append [a] E a append b E[b].append(a) [b].append [b] E b append a t = 0 t 0 ma = 0 ma 0 for i, e in enumerate(E):
    if len(e) > ma:
        t = i
        ma = len(e) i e enumerate(E) enumerate E if len(e) > ma:
        t = i
        ma = len(e) len(e) > ma len(e) len e ma t = i t i ma = len(e) ma len(e) len e Q = deque() Q deque() deque Q.append((t, 0)) Q.append Q append (t, 0) t 0 i = 0 i 0 visited = [False] * (n + 1) visited [False] * (n + 1) [False] False n + 1 n 1 ans = 0 ans 0 while Q:
    v, c = Q.popleft()
    if visited[v]:
        continue
    visited[v] = True
    V[v] = C[i]
    ans += min(c, C[i])
    for e in E[v]:
        Q.append((e, C[i]))
    i += 1 Q v, c = Q.popleft() v Q.popleft() Q.popleft Q popleft c if visited[v]:
        continue
     visited[v] visited v continue visited[v] = True visited[v] visited v True V[v] = C[i] V[v] V v C[i] C i ans += min(c, C[i]) ans min(c, C[i]) min c C[i] C i for e in E[v]:
        Q.append((e, C[i]))
     e E[v] E v Q.append((e, C[i])) Q.append Q append (e, C[i]) e C[i] C i i += 1 i 1 print(ans) print ans print(*V[1:]) print *V[1:] V[1:] V 1: 1 i = 0 0 i n = int(input()) int(input()) n V = [0] * (n + 1) [0] * (n + 1) V t = i i t v, c = Q.popleft() Q.popleft() v E = [[] for i in range(n + 1)] [[] for i in range(n + 1)] E A = [[int(i) for i in input().split()] for j in range(n - 1)] [[int(i) for i in input().split()] for j in range(n - 1)] A c = Q.popleft() Q.popleft() c Q = deque() deque() Q visited = [False] * (n + 1) [False] * (n + 1) visited t = 0 0 t ans = 0 0 ans ma = 0 0 ma C = [int(i) for i in input().split()] [int(i) for i in input().split()] C ans += min(c, C[i]) min(c, C[i]) ans i += 1 1 i ma = len(e) len(e) ma
from collections import deque

n = int(input())
A = [[int(i) for i in input().split()] for j in range(n - 1)]
C = [int(i) for i in input().split()]
C.sort(reverse=True)

E = [[] for i in range(n + 1)]
V = [0] * (n + 1)

for a, b in A:
    E[a].append(b)
    E[b].append(a)

t = 0
ma = 0

for i, e in enumerate(E):
    if len(e) > ma:
        t = i
        ma = len(e)

Q = deque()
Q.append((t, 0))
i = 0
visited = [False] * (n + 1)
ans = 0

while Q:
    v, c = Q.popleft()
    if visited[v]:
        continue
    visited[v] = True
    V[v] = C[i]
    ans += min(c, C[i])
    for e in E[v]:
        Q.append((e, C[i]))
    i += 1

print(ans)
print(*V[1:])