import sys
from heapq import heappush, heappop
sys.setrecursionlimit(10**6)

n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
	a, b = map(int, input().split())
	adj[a-1].append(b-1)
	adj[b-1].append(a-1)
num = [0 for _ in range(n)]
c = list(map(int, input().split()))
h = []
for i in range(n):
	heappush(h, -c[i])

def dfs(x):
	global h
	for v in adj[x]:
		if num[v] != 0:
			continue
		num[v] = -heappop(h)
		dfs(v)
	return

num[0] = -heappop(h)
dfs(0)
print(sum(c) - max(c))
print(*num) import sys sys from heapq import heappush, heappop sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 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) [] adj = [[] for _ in range(n)] adj [[] for _ in range(n)] for _ in range(n-1):
	a, b = map(int, input().split())
	adj[a-1].append(b-1)
	adj[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 adj[a-1].append(b-1) [a-1].append [a-1] adj a-1 a 1 append b-1 b 1 adj[b-1].append(a-1) [b-1].append [b-1] adj b-1 b 1 append a-1 a 1 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 num = [0 for _ in range(n)] num [0 for _ in range(n)] c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split h = [] h [] for i in range(n):
	heappush(h, -c[i]) i range(n) range n heappush(h, -c[i]) heappush h -c[i] c[i] c i def dfs(x):
	global h
	for v in adj[x]:
		if num[v] != 0:
			continue
		num[v] = -heappop(h)
		dfs(v)
	return dfs global h for v in adj[x]:
		if num[v] != 0:
			continue
		num[v] = -heappop(h)
		dfs(v)
	 v adj[x] adj x if num[v] != 0:
			continue
		 num[v] != 0 num[v] num v 0 continue num[v] = -heappop(h) num[v] num v -heappop(h) heappop(h) heappop h dfs(v) dfs v return x x num[0] = -heappop(h) num[0] num 0 -heappop(h) heappop(h) heappop h dfs(0) dfs 0 print(sum(c) - max(c)) print sum(c) - max(c) sum(c) sum c max(c) max c print(*num) print *num num c = list(map(int, input().split())) list(map(int, input().split())) c h = [] [] h adj = [[] for _ in range(n)] [[] for _ in range(n)] adj a, b = map(int, input().split()) map(int, input().split()) a b = map(int, input().split()) map(int, input().split()) b def dfs(x):
	global h
	for v in adj[x]:
		if num[v] != 0:
			continue
		num[v] = -heappop(h)
		dfs(v)
	return def dfs(x):
	global h
	for v in adj[x]:
		if num[v] != 0:
			continue
		num[v] = -heappop(h)
		dfs(v)
	return dfs num = [0 for _ in range(n)] [0 for _ in range(n)] num n = int(input()) int(input()) n
import sys
from heapq import heappush, heappop
sys.setrecursionlimit(10**6)

n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
	a, b = map(int, input().split())
	adj[a-1].append(b-1)
	adj[b-1].append(a-1)
num = [0 for _ in range(n)]
c = list(map(int, input().split()))
h = []
for i in range(n):
	heappush(h, -c[i])

def dfs(x):
	global h
	for v in adj[x]:
		if num[v] != 0:
			continue
		num[v] = -heappop(h)
		dfs(v)
	return

num[0] = -heappop(h)
dfs(0)
print(sum(c) - max(c))
print(*num)