1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/env ruby input = $stdin.readlines.map(&:to_i) lf = 0 posifreq = [true] negifreq = [] loop do input.each do |i| lf += i if lf >= 0 && posifreq[lf] || lf < 0 && negifreq[-lf] puts lf exit end if lf >= 0 posifreq[lf] = true else negifreq[-lf] = true end end end