N=gets.to_i
es = {}
(N-1).times do
  a,b = gets.split.map(&:to_i)
  es[a] ||= []
  es[a] << b
  es[b] ||= []
  es[b] << a
end
cs = gets.split.map(&:to_i).sort
ret = cs[0..-2].inject(&:+)

arr = [0,cs.pop]
q = es[1]
until q.empty?
  n = q.shift
  next if arr[n]
  arr[n] = cs.pop
  q += es[n]
end

p ret
puts arr[1..-1].join(' ')
