N=gets.to_i
tmp = $<.read.split.map(&:to_i)
abs=tmp[0..(N-1)*2-1].each_slice(2)
cs=tmp[(N-1)*2..-1].sort

tree = Array.new(N+1) { [] }

abs.each do |a,b|
  tree[a] << b
end

q = tree[1]
ret = Array.new(N+1) { 0 }
ret[1] = cs.pop
while !q.empty?
  e=q.shift
  ret[e]=cs.pop
  q+=tree[e]
end

sum = 0
abs.each do |a,b|
  sum += [ret[a],ret[b]].min
end

puts sum
puts ret[1..-1].join(' ')
