n = gets.chomp.to_i
s = []
cn = []
(n-1).times do
  a, b = gets.chomp.split.map(&:to_i)
  if s.include?(a)
    cn << a if !cn.include?(a)
  end
  if s.include?(b)
    cn << b if !cn.include?(b)
  end
  s << a
  s << b
end
cn.each do |cc|
  s.delete(cc)
end
c = gets.chomp.split.map(&:to_i).sort
puts c[0..(n-2)].inject(:+)
ans = []
s.each do |ss|
  ans[ss-1] = c.shift
end
n.times do |i|
  if !ans[i]
    ans[i] = c.shift
  end
end
n.times do |i|
  print ans[i]
  print " " if i != n-1
end
print "\n"