import sys
sys.setrecursionlimit(10**5*5)
n=int(input())
ab=[[]for _ in range(n)]
for _ in range(n-1):
	a,b=map(int,input().split())
	ab[a-1].append(b-1)
	ab[b-1].append(a-1)
c=list(map(int,input().split()))
c.sort()
print(sum(c)-c[-1])
ans=[-1]*n
cnt=0
node=[]
visited=[0]*n
ans=[-1]*n
def dfs(x):
	global cnt
	ans[x]=c[-cnt-1]
	visited[x]=1
	for next in ab[x]:
		if visited[next]==0:
			cnt+=1
			dfs(next)
dfs(0)
print(*ans)
 import sys sys sys.setrecursionlimit(10**5*5) sys.setrecursionlimit sys setrecursionlimit 10**5*5 10**5 10 5 5 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) [] ab=[[]for _ in range(n)] ab [[]for _ in range(n)] for _ in range(n-1):
	a,b=map(int,input().split())
	ab[a-1].append(b-1)
	ab[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 ab[a-1].append(b-1) [a-1].append [a-1] ab a-1 a 1 append b-1 b 1 ab[b-1].append(a-1) [b-1].append [b-1] ab b-1 b 1 append a-1 a 1 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 print(sum(c)-c[-1]) print sum(c)-c[-1] sum(c) sum c c[-1] c -1 ans=[-1]*n ans [-1]*n [-1] -1 n cnt=0 cnt 0 node=[] node [] visited=[0]*n visited [0]*n [0] 0 n ans=[-1]*n ans [-1]*n [-1] -1 n def dfs(x):
	global cnt
	ans[x]=c[-cnt-1]
	visited[x]=1
	for next in ab[x]:
		if visited[next]==0:
			cnt+=1
			dfs(next) dfs global cnt ans[x]=c[-cnt-1] ans[x] ans x c[-cnt-1] c -cnt-1 -cnt cnt 1 visited[x]=1 visited[x] visited x 1 for next in ab[x]:
		if visited[next]==0:
			cnt+=1
			dfs(next) next ab[x] ab x if visited[next]==0:
			cnt+=1
			dfs(next) visited[next]==0 visited[next] visited next 0 cnt+=1 cnt 1 dfs(next) dfs next x x dfs(0) dfs 0 print(*ans) print *ans ans n=int(input()) int(input()) n ans=[-1]*n [-1]*n ans ab=[[]for _ in range(n)] [[]for _ in range(n)] ab def dfs(x):
	global cnt
	ans[x]=c[-cnt-1]
	visited[x]=1
	for next in ab[x]:
		if visited[next]==0:
			cnt+=1
			dfs(next) def dfs(x):
	global cnt
	ans[x]=c[-cnt-1]
	visited[x]=1
	for next in ab[x]:
		if visited[next]==0:
			cnt+=1
			dfs(next) dfs cnt=0 0 cnt node=[] [] node ans=[-1]*n [-1]*n ans visited=[0]*n [0]*n visited b=map(int,input().split()) map(int,input().split()) b c=list(map(int,input().split())) list(map(int,input().split())) c a,b=map(int,input().split()) map(int,input().split()) a
import sys
sys.setrecursionlimit(10**5*5)
n=int(input())
ab=[[]for _ in range(n)]
for _ in range(n-1):
	a,b=map(int,input().split())
	ab[a-1].append(b-1)
	ab[b-1].append(a-1)
c=list(map(int,input().split()))
c.sort()
print(sum(c)-c[-1])
ans=[-1]*n
cnt=0
node=[]
visited=[0]*n
ans=[-1]*n
def dfs(x):
	global cnt
	ans[x]=c[-cnt-1]
	visited[x]=1
	for next in ab[x]:
		if visited[next]==0:
			cnt+=1
			dfs(next)
dfs(0)
print(*ans)

