gets a=gets.split.map(&:to_i) lcm=a.map{[]} a.size.times{|i|(i+1...a.size).each{|j|lcm[i][j]=lcm[j][i]=a[i].lcm(a[j])}} idx=[*0...a.size] a.size.times{|i|idx[i+1..-1]=idx[i+1..-1].sort_by{|j|[lcm[idx[i]][j],a[j]]}} puts idx.map{|i|a[i]}*" "