N = gets.to_i
as = Array.new(N-1)
bs = Array.new(N-1)
es = Hash.new(0)
(N-1).times do |i|
  a, b = gets.split.map(&:to_i)
  es[a] += 1
  es[b] += 1
  as[i] = a
  bs[i] = b
end
es = es.sort_by { |k,v| -v }

cs = gets.split.map(&:to_i).sort.reverse

ds = Array.new(N)
# puts es.inspect
# puts cs.inspect

es.each_with_index do |e, i|
  ds[e[0]-1] = cs[i]
end

# puts ds.inspect

ans = 0
(N-1).times do |i|
  a = as[i]
  b = bs[i]
  ans += [ds[a-1] ,ds[b-1]].min
end
puts ans
puts ds.join(" ")