Go: package main

import "fmt"

func main() {
	var result []int
	for {
		var n, a, maxSum int
		fmt.Scan(&n)

		if n == 0 {
			break
		}

		maxSum = 0
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			if i == 0 {
				sum = a
				maxSum = sum
			} else if sum+a < a {
				sum = a
				if maxSum < sum {
					maxSum = sum
				}
			} else {
				sum = sum + a
				if maxSum < sum {
					maxSum = sum
				}
			}
		}

		result = append(result, maxSum)
	}

	for _, v := range result {
		fmt.Println(v)
	}
}

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]
    return L

def collect(nl):
    result = 0
    while True:
        nl = grouping(nl)
        if len(nl) and nl[0] >= 0:
            result += nl.pop(0)
        if not len(nl):
            return result
        nl = array(b'i', (nl[i] + nl[i+1] for i in xrange(0, len(nl), 2)))

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

    L = grouping(L)
    val = max(L)
    idx = L.index(val)

    Lprev = L[:idx]
    Lprev.reverse()
    Lnext = L[idx+1:]

    val += collect(Lprev) + collect(Lnext)
    print(val)

Python: while True:
    n = int(input())
    if n==0: break
    line = [int(input()) for i in range(n)]
    sum_li = [sum(line[:i]) for i in range(1,n+1)]
    key = sum_li.index(max(sum_li))+1
    result = [sum(line[i:key]) for i in range(key)]
    print(max(result))

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

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

    print(max(dp))

Python: while 1:
    n = int(input())
    if n == 0:
        break

    m = [None] * n
    t = [None] * n
    for i in range(n):
        a = int(input())
        m[i] =  t[i] = a
        for j in range(i):
            t[j] += a
            m[j] = max(m[j], t[j])

    print(max(m))

Python: while True:
    num = int(input())
    if not num: break
    result, tmp = -1e2, 0
    for _ in range(num):
        new = int(input())
        tmp = max(new, new+tmp)
        result = max(tmp, result)
    print(result)

Python: while True:
    n = int(input())

    if n == 0:
        break

    sum = 0

    for _ in range(n):
        sum += max(int(input()), 0)

    print(sum)

Python: n = int(input())
while n > 0:
    s = [0]
    m = 0
    for i in range(n):
        m += int(input())
        s.append(m)
    m = s[0]
    for i in range(1,n+1):
        for j in s[0:i]:
            m = max(m,s[i]-j)
    print(m)
    n = int(input())

Python: while True:
    n = int(input())
    if n==0 : break

    a = []
    for i in range(n):
        a.append(int(input()))

    maxp = -100001
    maxcont = -100001
    for i in range(n):
        maxcont = max(a[i], maxcont + a[i])
        maxp = max(maxp, maxcont)

    print(maxp)

Python: while 1:
 n=int(input())
 if n:
  a=[int(input())for _ in[0]*n]
  print(max([sum(a[i:j+1])for i in range(n)for j in range(i,n)]))

Python: while True:
    n = int(input())
    if n == 0:
        break
    a = [int(input()) for _ in range(n)]
    d = [a[0]]
    for i in range(n):
        if d[0] == 0:
            d[0] += a[i]
        elif a[i] / d[-1] >= 0:
            d[-1] += a[i]
        else:
            d.append(a[i])
    if d[0] < 0:
        d.pop(0)
    if d == []:
        print(max(a))
    elif len(d) <= 2:
        print(max(d))
    else:
        maxd = max(d)
        for i in range(0, len(d), 2):
            for j in range(i + 1, len(d) + 1, 2):
                maxd = max(maxd, sum(d[i:j]))
        print(maxd)

Python: n=int(input())
while(n!=0):
    index=0
    list=[]
    sum=0
    max=0
    for i in range(n):
        list.append(int(input()))

    while (index < len(list) and list[index] < 0):
        sum += list[index]
        index += 1

    max=sum
    sum=0

    while (index < len(list) and list[index] >= 0):
        sum += list[index]
        index += 1

    if sum>0: max=sum

    while(index<len(list)):
        plus_sum = 0
        minus_sum = 0

        while (index < len(list) and list[index] < 0):
            minus_sum += list[index]
            index += 1

        while (index < len(list) and list[index] >= 0):
            plus_sum += list[index]
            index += 1

        if abs(minus_sum)<plus_sum:
            sum+=(minus_sum+plus_sum)
            if max<sum: max=sum
        else:
            sum=plus_sum
            if max<sum: max=sum
    print(max)
    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]
    return L

def collect(nl):
    result = 0
    while True:
        nl = grouping(nl)
        if len(nl) and nl[0] >= 0:
            result += nl.pop(0)
        if not len(nl):
            return result
        nl = array(b'i', (nl[i] + nl[i+1] for i in xrange(0, len(nl), 2)))

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

    L = array(b'i', (int(stdin.readline()) for _ in xrange(n)))
    val = max(L)
    idx = L.index(val)

    Lprev = L[:idx]
    Lprev.reverse()
    Lnext = L[idx+1:]

    val += collect(Lprev) + collect(Lnext)
    print(val)

Python: while True:
    n = int(input())
    if n == 0:
        break
    max_ = -999999
    a = [int(input()) for x in range(n)]
    for i in range(n - 1):
        m = a[i]
        for j in range(i + 1, n):
            m += a[j]
            if m > max_:
                max_ = m
    print(max_)

Python: # Maximum Sum Sequence
n = int(input())
while not n == 0:
    cnd = []
    cs = 0
    for _ in range(n):
        if cs < 0 : cs = 0
        cs += int(input())
        cnd.append(cs)
    print(max(cnd))
    try: n = int(input())
    except EOFError: break

Python: from sys import stdin

def getMax(array):
    mx = mx2 = array[0]
    for i in array[1:]:
        mx2 = max(i, mx2 + i)
        print(mx2,mx)
        mx = max(mx, mx2)
    return mx

for line in stdin:
    n = int(line)
    if n == 0:
        break
    array = []
    for line in stdin:
        line = int(line)
        array.append(line)
        if len(array) == n:
            break
    print(getMax(array))

Python: # AOJ 0022 Maximum Sum Sequence
# Python3 2018.6.16 bal4u

while 1:
	n = int(input())
	if n == 0: break
	s = [0]*5001
	ans = s[0] = int(input())
	for i in range(1, n):
		v = int(input())
		s[i] = v if s[i-1]+v < v else s[i-1]+v
		if s[i] > ans: ans = s[i]
	print(ans)

Python: import itertools

while True:
    n = int(input())
    if n == 0:
        break
    else:
        a = [int(input()) for i in range(n)]
        a = list(itertools.accumulate(a))
        a.insert(0, 0)
        ans = -100000
        for j in range(n):
            for k in range(j + 1, n + 1):
                ans = max(ans, a[k] - a[j])
        print(ans)