require 'matrix' n = gets.chomp.to_i ary = gets.chomp.split(' ').map(&:to_i).sort_by { |a| a * -1 } diag = []; n.times { diag << ary.shift } Matrix.diagonal(*diag).map { |e| e == 0 ? ary.shift : e }.to_a.each { |e| puts e.join(' ') }