Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func max(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func main() {
	sum := make([]int, 5001)
	for {
		n := nextInt()
		if n == 0 {
			break
		}
		for i := 0; i < n; i++ {
			a := nextInt()
			sum[i+1] = sum[i]+a
		}
		ret := sum[1]
		for i := 0; i <= n; i++ {
			for j := i + 1; j <= n; j++ {
				ret = max(ret, sum[j]-sum[i])
			}
		}
		fmt.Println(ret)
	}
}

Python: while(1):
    a=[]
    result=0
    n = int(input())
    if n==0:
        break
    a=[int(input()) for i in range(n)]
    for i in range(n):
        if a[i]>=0:
            result=result+a[i]
    print(result)

Python: a = []
b = []


def searchs(i, n, m):
    b.clear()
    for k in range(i+1, n+1):
        b.append(sum(a[i:k]))
    # print(b)
    if m < max(b):
        m = max(b)
    if i+1 < n:
        m = searchs(i+1, n, m)
    return m


while True:
    user = input()
    n = int(user)
    if n == 0:
        break
    a.clear()
    for i in range(n):
        a.append(int(input()))
    # print(a)
    print(searchs(0, n, 0))

Python: while True:
    n = int(input())
    if not n:
        break
    l = [int(input()) for j in range(n)]
    s = [l[0]]
    for k, v in enumerate(l[1:]):
        s.append(max(v, v + s[k]))
    print(max(s))

Python: while True:
	n = int(input())
	
	if n == 0:
		break
	
	res = -1111111111
	s = 0
	for i in range(n):
		a = int(input())
		s = max(s + a, a, 0)
		res = max(s, res)
	
	print(res)

Python: import sys

n = int(sys.stdin.readline())
while n:
    l = list()
    for _ in range(n):
        l.append(int(input()))

    maxs = 0
    for i in range(n):
        for j in range(i,n):
            s = 0
            for k in range(i,j+1):
                s += l[k]
            maxs = max(maxs,s)
    n = int(sys.stdin.readline())
    print(maxs)

Python: while True:
    n = int(input())
    if n == 0:
        break
    else:
        a = [int(input()) for i in range(n)]
        for i in range(1, n):
            a[i] = max(a[i - 1] + a[i], a[i])
        print(max(a))

Python: while 1:
    n = int(input())
    if n == 0:
        break
    a = []
    for _ in range(n):
        a.append(int(input()))

    dp = []
    dp.append(a[0])
    for i in range(1,len(a)):
        dp.append(max(dp[i-1] + a[i], a[i]))

    print(max(dp))

Python: # -*- coding: utf-8 -*-

import sys
import os

def max_seq(A):
    acc = max(0, A[0])
    ans = max(0, A[0])

    for i in range(1, len(A)):
        v = acc + A[i]
        if v < 0:
            # ???????????§???????????????acc??????????¶???????????????????
            ans = max(ans, acc)
            # ????????????
            acc = max(0, A[i])
        else:
            acc = v
            ans = max(ans, acc)
    return ans

while True:
    s = input().strip()
    if s == '0':
        break

    N = int(s)
    A = []
    for i in range(N):
        v = int(input())
        A.append(v)

    print(max_seq(A))

Python: while(1):
    N = int(input())
    if N==0: break
    sums = []
    s = 0
    for i in range(N):
        s += int(input())
        if s<0: s=0
        sums.append(s)
    print(max(sums))

Python: # Aizu Problem 0022: Maximum Sum Sequence
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def max_sum_subsequence(seq):
    maxsofar = 0
    maxendinghere = 0
    for s in seq:
        # invariant: maxendinghere and maxsofar are accurate
        # are accurate up to s
        maxendinghere = max(maxendinghere + s, 0)
        maxsofar = max(maxsofar, maxendinghere)
    return max(maxsofar, max(seq))

while True:
    N = int(input())
    if N == 0:
        break
    seq = [int(input()) for _ in range(N)]
    print(max_sum_subsequence(seq))

Python: def MSS(li):
    s = sum(li)
    l = len(li)
    for i in range(l):
        for j in range(l-i):
            if j == 0:
                continue
            e = l - j
            tmp = sum(li[i:e])
            if s < tmp:
                s = tmp
    return s


if __name__ == "__main__":
    assert MSS([-5, -1, 6, 4, 9, -6, -7]) == 19, "sample1"
    assert MSS([1, 2, 3, 2, -2, -1, 1, 2, 3, 2, 1, -2, 1]) == 14, "sample2"
    while True:
        n = int(input())
        if n == 0:
            break
        li = []
        for _ in range(n):
            li.append(int(input()))
        print(MSS(li))

Python: def MSS(li):
    s = sum(li)
    l = len(li)
    for i in range(l):
        for j in range(l-i):
            if j == 0:
                continue
            e = l - j
            tmp = sum(li[i:e])
            if s < tmp:
                s = tmp
    return s


if __name__ == "__main__":
    assert MSS([-5, -1, 6, 4, 9, -6, -7]) == 19, "sample1"
    assert MSS([1, 2, 3, 2, -2, -1, 1, 2, 3, 2, 1, -2, 1]) == 14, "sample2"
    answer = []
    while True:
        n = int(input())
        if n == 0:
            break
        li = []
        for _ in range(n):
            li.append(int(input()))
        answer.append(MSS(li))
    print(*answer, sep="\n")

Python: import sys
while True:
    n=int(input())
    if n==0:
        sys.exit()

    sum_list=[0]
    for i in range(n):
        sum_list.append(sum_list[-1]+int(input()))

    if max(sum_list)==0:
        sum_list.pop(0)
        print(max(sum_list))
    maximum=0
    for i in range(n):
        for j in range(i,n+1):
            maximum=max(sum_list[j]-sum_list[i],maximum)

    print(maximum)

Python: n = int(input())
while n > 0:
    q = 0
    m = 0
    for j in range(n):
        i = int(input())
        if q == 0:
            if i <= 0:
                continue
            else:
                q = i
        else:
            q += i
            if q < 0:
                q = 0
            else:
                m = max(m,q)
    print(m)
    n = int(input())

Python: while(True):
    n = int(input())
    if n == 0:
        break
    r = -20000005
    s = 0
    for _ in range(n):
        a = int(input())
        s = max(a+s,a)
        r = max(r,s)
    print(r)

Python: import itertools
while True:
    n = int(input())
    if n == 0:
        break
    a = [int(input()) for _ in range(n)]
    a[:0] = [0]
    for i in range(1, len(a)):
        a[i] += a[i - 1]
    print(max(map(lambda x:x[1] - x[0], itertools.combinations(a, 2))))

Python: n=int(input())
while n!=0:
    sum_list=[0]
    for i in range(n):
        sum_list.append(sum_list[-1]+int(input()))

    if max(sum_list)==0:
        sum_list.pop(0)
        print(max(sum_list))
    maximum=0
    for i in range(n):
        for j in range(i,n+1):
            maximum=max(sum_list[j]-sum_list[i],maximum)

    print(maximum)
    n=int(input())

Python: from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from array import array

def grouping(nums):
    sign = None
    L = array(b'i')
    for s, n in ((i < 0, i) for i in nums):
        if sign is s:
            L[-1] += n
        else:
            sign = s
            L.append(n)

    if len(L) and L[-1] <= 0:
        del L[-1]
    if len(L) and L[0] <= 0:
        del L[0]
    return L

while True:
    n = int(stdin.readline())
    if not n:
        break

    L = array(b'i', (int(stdin.readline()) for _ in xrange(n)))
    val = max(L)
    if val <= 0:
        print(val)
        continue

    ans, temp = 0, 0
    for i in grouping(L):
        temp = max(0, temp+i)
        ans = max(ans, temp)
    print(ans)

Python: while True:
  INF = 10 ** 20
  n = int(input())
  if not n:
    break
  cum_sum = [0]
  acc = 0
  for i in range(n):
    acc += int(input())
    cum_sum.append(acc)
  ans = -INF
  for i in range(n):
    ans = max(ans, max(cum_sum[i:]) - cum_sum[i])
  print(ans)