gets a = gets.split.map(&:to_i) while a.size > 3 do a[-3] += a[-1] a[-1] = 0 a.pop end a.pop while a[-1] == 0 puts a.size > 0 ? [a.size - 1, a.join(" ")] : [0, 0]