n = gets.chomp.split[0].to_i
edge = []
near = Array.new(n).map{Array.new}
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)
    near[a-1].push(b)
    degree[a-1][1] += 1
    near[b-1].push(a)
    degree[b-1][1] += 1
    degree[i-1][0] = i
end

degree[n-1][0] = n

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

stack = [degree[0][0]]
cnt = 0

while stack.size > 0
    tar = stack.shift
    node[tar-1] = c[cnt]
    cnt += 1
    points = near[tar-1]
    for nxt in points do
        if node[nxt-1] == 0
            stack.push(nxt)
        end
    end
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]