n=gets.to_i
v=$<.map{|s|s.split.map{|m| m.to_i-1}}
c=v.pop
c=c.map{|m|m+1}.sort
v.sort!

@p = Array.new(n,nil)
@r = Array.new(n,0)
@k = Array.new(n,0)

def union(x, y)
  @p[x] = y
  @k[x] += 1
  @k[y] += 1
end

v.each do |e|
  union(e[1],e[0])
end



@k.map.with_index{|m,i| [m,i]}.sort.reverse.each do |e|
  @p[e[1]] = c.pop
end

p v.inject(0){|s,i|s += [@p[i[0]], @p[i[1]]].min}
puts @p.join(' ')