import           Control.Applicative
import           Control.Monad
import           Data.Array
import           Data.List

solve :: [Int] -> Int
solve = maximum . scanl1 (\a b -> max (a + b) b)

main :: IO ()
main = getContents >>= loop . map read . lines
  where
    loop (0:_)  = return ()
    loop (x:xs) = let (as, rs) = splitAt x xs
                  in  print (solve as) >> loop rs