n = gets.to_i
edge = Array.new(n+1) {[]}

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

c = gets.split.map(&:to_i).sort_by{|e| -e}
m = c.inject(:+) - c[0]

que = [1]
ans = Array.new(n+1)
while now = que.shift
    next if ans[now]
    ans[now] = c.shift
    que += edge[now]
end

puts m, ans[1..-1] * " "