d = gets.chomp!.to_i
node = []
counter = {}

require 'pp'
require 'set'

(d - 1).times do |i|
    a, b = gets.chomp!.split(" ").map(&:to_i)
    counter[a] = (counter[a] || 0) + 1
    counter[b] = (counter[b] || 0) + 1
    node << [a, b]
end
c = gets.chomp!.split(" ").map(&:to_i)

x = counter.sort {|(_, v1), (_, v2)| v2 <=> v1 }
m = x.map {|index, count|
    c[index - 1]
}

sum = 0
node.each do |x|
    #pp x
    #puts [m[x[0] - 1], m[x[1]-1]].min 
    sum += [m[x[0]-1], m[x[1]-1]].min 
end

puts sum
puts m.join(" ")
