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 puts get_ans_list[0] puts get_ans_list[1] end private def get_ans_list list = [] 3.downto(1) do |i| if @a_list[i] != 0 && @a_list[i] != nil list << i list << @a_list[0..i].join(' ') break end end if list.empty? list << 0 list << @a_list[0] end list end end Solve.new.run