Go: package main

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

var infty = 100000 * 2
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() (int, error) {
	scanner.Scan()
	return strconv.Atoi(scanner.Text())
}

func findMaximumSubarray(arr []int) int {
	currentMaxSum, maxSumContainsTail := -infty, -infty

	for j := range arr {
		if arr[j] > maxSumContainsTail+arr[j] {
			maxSumContainsTail = arr[j]
		} else {
			maxSumContainsTail += arr[j]
		}
		if currentMaxSum < maxSumContainsTail {
			currentMaxSum = maxSumContainsTail
		}

		// fmt.Println(arr[j], currentMaxSum, maxSumContainsTail)
	}
	return currentMaxSum
}

func main() {
	scanner.Split(bufio.ScanWords)
	for {
		size, _ := nextInt()
		if size == 0 {
			break
		}

		arr := make([]int, size)
		for i := range arr {
			n, _ := nextInt()
			arr[i] = n
		}

		maximumSum := findMaximumSubarray(arr)
		fmt.Println(maximumSum)
	}
}

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)

Python: while True:
    t = int(input())
    if t == 0:
        break
    tmp = [int(input()) for i in range(t)]
    res = [tmp[0]]
    for i in range(1,t):
        res.append(max(tmp[i], tmp[i]+res[i-1]))

    print(max(res))

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, 0)
		res = max(s, res)
	
	print(res)

Python: def solve():
    from sys import stdin
    f_i = stdin
    
    while True:
        n = int(f_i.readline())
        if n == 0:
            break
        
        ans = int(f_i.readline())
        s = ans
        for i in range(n - 1):
            if s > 0:
                s += int(f_i.readline())
            else:
                s = int(f_i.readline())
            if s > ans:
                ans = s
        print(ans)

solve()

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

Python: """
全探索１　O(n^3)
while 1:
    a = []
    n = int(input())
    if n == 0:
        break
    a = [int(input()) for i in range(n)]
    max = 0
    for i in range(n):
        for j in range(i, n):
            sum = 0
            for k in range(i, j+1):
                sum += a[k]
            if max < sum:
                max = sum
    print(max)
"""
while 1:
    a = []
    n = int(input())
    if n == 0:
        break
    a = [int(input()) for i in range(n)]
    max = -111111111111111
    for i in range(n):
        sum = 0
        for j in range(i, n):
            sum += a[j]
            if sum > max:
                max = sum

    print(max)

Python: while True:
    n = int(input())
    m = -1000000 
    if n == 0:
        break
    dataset = [int(input()) for _ in range(n)]
    for i in range(n):
        for j in range(i, n + 1):
            if dataset[i:j] != []:
                s = sum(dataset[i:j])
                if m < s:
                    m = s
    print(m)

Python: import sys

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

    for i in range(len(a)+1):
        for j in range(i, len(a)+1):
            sums.append(sum(a[i:j]))

    print(max(sums))

Python: import math

def sign(x):
    if x >= 0:
        return True
    else:
        return False

n = int(input())
while n != 0:
    a = []
    for i in range(n):
        a.append(int(input()))
    b = []
    b.append(a[0])
    for i in range(1,len(a)):
        if sign(b[len(b)-1]) == sign(a[i]):
            b[len(b)-1] = b[len(b)-1] + a[i]
        else:
            b.append(a[i])

    ans = b[0]
    for i in range(len(b)):
        S = 0
        for j in range(i,len(b)):
            S = S + b[j]
            ans = max(S, ans)
    print(ans)
    n = int(input())

Python: import sys

n = int(sys.stdin.readline())
while n:
    l = list()
    for _ in range(n):
        l.append(int(input()))
    a = l.copy()
    for i in range(1,n):
        a[i] = max(a[i-1] + l[i], l[i])
    print(max(a))
    n = int(sys.stdin.readline())