import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

c = list(map(int, input().split()))
c.sort(reverse=True)
sum_val = sum(c) - max(c)

assign = [0] * N
index = 0

q = deque()
q.append(0)
visited = []

while len(q) != 0:
    cur = q.popleft()
    assign[cur] = c[index]
    index += 1
    for n in graph[cur]:
        if n not in visited:
            q.append(n)
    visited.append(cur)

print(sum_val)
print(" ".join(map(str, assign))) import sys sys import math math import fractions fractions from collections import deque from collections import defaultdict sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 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) [] graph = [[] for _ in range(N)] graph [[] for _ in range(N)] for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a) _ 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 -= 1 a 1 b -= 1 b 1 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph 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(reverse=True) c.sort c sort reverse=True reverse True sum_val = sum(c) - max(c) sum_val sum(c) - max(c) sum(c) sum c max(c) max c assign = [0] * N assign [0] * N [0] 0 N index = 0 index 0 q = deque() q deque() deque q.append(0) q.append q append 0 visited = [] visited [] while len(q) != 0:
    cur = q.popleft()
    assign[cur] = c[index]
    index += 1
    for n in graph[cur]:
        if n not in visited:
            q.append(n)
    visited.append(cur) len(q) != 0 len(q) len q 0 cur = q.popleft() cur q.popleft() q.popleft q popleft assign[cur] = c[index] assign[cur] assign cur c[index] c index index += 1 index 1 for n in graph[cur]:
        if n not in visited:
            q.append(n)
     n graph[cur] graph cur if n not in visited:
            q.append(n)
     n not in visited n visited q.append(n) q.append q append n visited.append(cur) visited.append visited append cur print(sum_val) print sum_val print(" ".join(map(str, assign))) print " ".join(map(str, assign)) " ".join " " join map(str, assign) map str assign b = map(int, input().split()) map(int, input().split()) b c = list(map(int, input().split())) list(map(int, input().split())) c visited = [] [] visited assign = [0] * N [0] * N assign N = int(input()) int(input()) N a -= 1 1 a index += 1 1 index graph = [[] for _ in range(N)] [[] for _ in range(N)] graph sum_val = sum(c) - max(c) sum(c) - max(c) sum_val a, b = map(int, input().split()) map(int, input().split()) a index = 0 0 index q = deque() deque() q cur = q.popleft() q.popleft() cur b -= 1 1 b
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

c = list(map(int, input().split()))
c.sort(reverse=True)
sum_val = sum(c) - max(c)

assign = [0] * N
index = 0

q = deque()
q.append(0)
visited = []

while len(q) != 0:
    cur = q.popleft()
    assign[cur] = c[index]
    index += 1
    for n in graph[cur]:
        if n not in visited:
            q.append(n)
    visited.append(cur)

print(sum_val)
print(" ".join(map(str, assign)))
