n    = gets.to_i
tree = Array.new(n, 0)
list = []
ans  = Array.new(n, 0)

(n - 1).times do
  a, b = gets.split.map(&:to_i)
  tree[a - 1] += 1
  tree[b - 1] += 1
  list << [a, b]
end

aryC = gets.split.map(&:to_i).sort

n.times do |i|
  tree[i] = [tree[i], i]
end

tree.sort!

n.times do |i|
  ans[tree[i][1]] = aryC[i]
end

sum = 0

(n - 1).times do |i|
  sum += [ans[list[i][0] - 1], ans[list[i][1] - 1]].min
end

puts sum, ans.join(" ")
