class A def initialize d = gets.chomp.to_i a = gets.chomp.split(' ').map(&:to_i).reverse 0.upto(d-3) do |num| a[num+2] += a[num] a[num] = 0 end if a.index{|i| i != 0} puts d - a.index{|i| i != 0} else puts 0 end #a.delete(0) puts encode(a).join(' ') end def encode(answer) index = answer.index{|n| n > 0} index && answer[index.to_i..-1].reverse || [0] end end A.new