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: while 1:
	n=int(input())
	if n==0: break
	a=[int(input()) for i in range(n)]
	temp = 0
	most = -999999999
	for i in range(n):
		temp = 0
		for j in range(i,n):
			temp +=	a[j]
			if temp > most:
				most = temp
	print(most)

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

Python: #!/usr/bin/python

from sys import stdin

def datasets():
    raw_input()
    for l in stdin:
        yield int(l)
        
def main():
    ds = datasets()
    first = next(ds)
    lastnum = count = maximum = first
    for n in ds:
        if n == lastnum:
            count += n
        else:
            if count > maximum:
                maximum = count
            count = n
        lastnum = n
        
if __name__ == '__main__':
    main()

Python: while 1:
 n=int(input())
 if n==0:break
 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(1):
    n = int(input())
    if n == 0:
        break
    else:
        ary = []
        mx = 0
        for i in range(n):
            ary.append(int(input()))
        for i in range(1, n + 1):
            sm = sum(ary[0:i])
            if mx < sm:
                mx = sm
            for j in range(1, n - i + 1):
                sm = sm - ary[j - 1] + ary[j + i - 1]
                if mx < sm:
                    mx = sm
        print(mx)

Python: while True:
    n = int(input())
    if n==0: break
    sum_li = [int(input()) for i in range(n)]
    for i in range(2,n+1):
        for j in range(n-i+1):
            sum_li.append(sum(sum_li[j:j+i]))
    print(sorted(sum_li)[-1])

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

Python: def maximumSubArray(array):
    n = len(array)
    dp = [0]*n # dp[i]: i番目までの全ての部分配列の中の最大の総和
    dp[0] = array[0]
    for i in range(1, n):
        dp[i] = max(dp[i - 1] + array[i], array[i])
    return max(dp)

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

Python: #!/usr/bin/python
# -*- coding: utf-8 -*-


def main():
    while True:
        terms = int(raw_input())
        if terms == 0:
            break

        data = []
        for i in xrange(terms):
            data.append(int(raw_input()))

        print(max_sum_sequence(data))


def max_sum_sequence(data):
    max = 0
    num_data = len(data)
    for l in xrange(num_data + 1):
        sum = max_at_length(data, l)
        if sum > max:
            max = sum

    return max


def max_at_length(data, length):
    max = 0
    num_data = len(data)

    first = 0
    before = None

    for begin in xrange(num_data):
        sum = 0
        if begin > 0:
            first = data[begin - 1]

        if before is None:
            for i in xrange(begin, begin + length):
                if i >= num_data:
                    continue

                sum += data[i]

        else:
            if begin + length + 1 < num_data:
                sum = before - first + data[begin + length + 1]
            else:
                sum = before - first;

        before = sum

        if sum > max:
            max = sum

    return max


main()

Python: while True:
	n=int(input())
	if n==0:
		break
	A=[]
	for i in range(n):
		x=int(input())
		A.append(x)

	B=[int()]*n
	B[0]=A[0]
	for i in range(1,n):
		if A[i]>=A[i]+B[i-1]:
			B[i]=A[i]
		else:
			B[i]=A[i]+B[i-1]
	print(max(B))

Python: while True:
    n = int(input())
    if n == 0:
        break
    a = [int(input()) for _ in range(n)]
    b = []
    while a != []:
        for i in range(len(a)):
            if i == len(a) - 1 or a[i] * a[i + 1] < 0:
                b.append(sum(a[:i + 1]))
                a[:i + 1] = []
                break
    print(max(max(sum(b[j:j + i + 1]) for j in range(len(b) - i)) for i in range(len(b))))

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

Python: 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(a) - min(a))

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

def grouping(nums):
    it = iter(nums)
    try:
        L = [next(it)]
    except StopIteration:
        return []
    minus = L[0] < 0
    for n in it:
        if (n < 0 and minus) or (n >= 0 and not minus):
            L[-1] += n
        else:
            L.append(n)
            minus = not minus
    return L

def collect(nl):
    result = 0
    if len(nl) % 2 != 0:
        nl = grouping(nl)
    while len(nl) > 0:
        if len(nl) > 1:
            nl = grouping(nl[i] + nl[i+1] for i in xrange(0, len(nl), 2))
        if len(nl) > 0 and nl[-1] <= 0:
            nl.pop()
        if len(nl) > 0 and  nl[0] >= 0:
            result += nl[0]
            nl.pop(0)
    return result

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

    L = [int(stdin.readline()) for _ in xrange(n)]
    val = max(L)
    idx = L.index(val)

    if len(L[:idx]) > 1:
        val += collect(list(reversed(L[:idx])))
    if len(L[idx+1:]) > 1:
        val += collect(L[idx+1:])

    print(val)

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

Python: import sys

for i in sys.stdin:
    n = int(i)
    if n == 0:
        break
    max = -100000
    sum = int(input())
    for i in range(n-1):
        m = int(input())
        if sum < 0 and sum < m:
            sum = m
        elif m < 0 and max < sum:
            max = sum
            sum += m
        else:
            sum += m
    if sum > max:
        max = sum
    print(max)

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):
        m = a[i]
        for j in range(i + 1, n):
            m += a[j]
            if m > max_:
                max_ = m
    print(max_)

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

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

    m = -100000 * 5000
    for i in range(0, n):
        for j in range(i + 1, n + 1):
            m = max(m, s[j] - s[i])

    print(m)