n = gets.chomp.split[0].to_i
edge = []
degree = Array.new(n).map{Array.new(2,0)}
node = Array.new(n,0)

for i in 1..n-1
    input = gets.chomp.split(" ").map(&:to_i)
    a = input[0]
    b = input[1]
    edge.push(input)
    degree[a-1][1] += 1
    degree[b-1][1] += 1
    degree[i-1][0] = i
end

degree[n-1][0] = n

degree.sort! {|a,b| a[1] <=> b[1]}
c = gets.chomp.split(" ").map(&:to_i)
c.sort!


for i in 1..n do
    j = degree[i-1][0]
    node[j-1] = c[i-1]
end

score = 0
for i in 1..n-1 do
    a,b = edge[i-1]
    score += [node[a-1],node[b-1]].min
end

p score
for i in 1..n-1 do
    print node[i-1], " "
end
puts node[n-1]