loop do
  cnt = STDIN.gets.chomp
  break if cnt == '0'

  max = 0
  input = Array.new(cnt.to_i) { STDIN.gets.chomp.to_i }
  sp = input.index { |i| i > 0 } || 0
  ep = input.rindex { |i| i > 0 } || (input.length - 1)
  next if input.empty?
  input[sp..ep].inject(0) do |a, e|
    a = 0 if a < 0
    max = a + e if (a + e) > max
    a + e
  end
  puts max
end