n = gets.to_i
edges = []
(n-1).times do
 a, b = gets.chomp.split(" ")
 edges.push([a.to_i, b.to_i])
end

leafs = gets.chomp.split(" ")
leafs.map!(&:to_i)

leafs.sort!.reverse!

score = 0

for a, b in edges
  a_score = leafs[a-1].to_i
  b_score = leafs[b-1].to_i
  if a_score >= b_score
    score += b_score
  elsif a_score < b_score
    score += a_score
  end
end

puts score
print leafs.join(" ") + "\n"