N = gets.to_i
edges = N.times.map{[]}
AB = (N-1).times.map{
  gets.split.map(&:to_i).map &:pred
}
AB.each{|a, b|
  edges[a] << b
  edges[b] << a
}
C = gets.split.map &:to_i
# 記入するスコアを昇順ソート
C.sort!

lens = [1e9] * N
edges.each_with_index{|tos, from|
  next if tos.size > 1
  lens[from] = 0
  to = tos[0]
  f = ->from, dist{
    return if lens[from] < dist
    lens[from] = dist
    edges[from].each{|to|
      f[to, dist + 1]
    }
  }
  f[to, 1]
}

scores = edges.zip(lens).map.with_index{|_, i|
  [_, i]
}.sort_by{|(e, len), i|
  # 隣接する点の数＞端までの距離で昇順ソート
  [e.size, len]
}.zip(C).sort_by{|(_, i), c|
  i
}.map{|(e, i), c|
  c
}
ans = AB.map{|a, b|
  [scores[a], scores[b]].min
}.inject:+
p ans
puts scores*" "
