class Solve def initialize _ = gets.to_i @a_list = gets.chomp.split.map(&:to_i) end def run len = @a_list.size (len - 1).downto(3) do |i| if @a_list[i] != 0 @a_list[i-2] += @a_list[i] @a_list[i] = 0 end end 3.downto(0) do |i| if @a_list[i] != 0 puts i puts @a_list[0..i].join(' ') break end end end end Solve.new.run