n = gets.to_i
vs = []
(n+1).times do |i|
  vs << [[],0]
end
(n-1).times do
  a,b = gets.split.map(&:to_i)
  vs[a][0] << b
  vs[a][1] += 1
  vs[b][0] << a
  vs[b][1] += 1
end
c = gets.split.map(&:to_i).sort.reverse
que = []
(n+1).times do |i|
  t,u = vs[i]
  que << i if u == 1
end

ans = 0
ds = []*(n)
while !que.empty? && !c.empty?
  v = que.shift
  tmp = vs[v]
  t = tmp[0]
  u = tmp[1]
  x = c.pop
  ans += x * u
  ds[v-1] = x
  t.each do |i|
    vs[i][1] -= 1
    if vs[i][1] == 1
      que << i
    end
  end
end
p ans
n.times do |i|
  t = ds[i]
  print "#{t}"
  print i == n-1 ? "" : " "
end
