n    = gets.to_i
node = Array.new(n).map{Array.new}
list = []
ans  = Array.new(n)

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

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

n.times do |i|
  node[i].unshift(i)
end

top = node.sort_by{|a| a.size}[0][0]

queue = []
queue << top

check = Array.new(n, 0)

while queue.size > 0
  now = queue.shift
  if check[now] == 0
    ans[now] = aryC.pop
    node[now].each do |n|
      queue << n
    end
  check[now] = 1
  end
end

sum = 0
list.each do |ls|
  sum += [ans[ls[0]], ans[ls[1]]].min
end

puts sum, ans.join(" ")
