n = gets.to_i
sides = []
(n - 1).times do
  sides.push(gets.split(" ").map(&:to_i))
end
cs = gets.split(" ").map(&:to_i)
cs.sort!

scores = []
n.times do |index|
  scores.push ([index, 0, 0])
end

sides.each do |side|
  scores[side[0] - 1][1] += 1
  scores[side[1] - 1][1] += 1
end

scores.sort! {|a,b| a[1] <=> b[1]}

scores.each_with_index do |score, index|
  scores[index][2] = cs[index]
end

scores.sort! {|a,b| a[0] <=> b[0]}

sum = 0

sides.each do |side|
  sum += [scores[side[0] - 1][2], scores[side[1] - 1][2]].min
end

puts sum

string = ""

scores.each do |score|
  string = string + score[2].to_s + " "
end

puts string
