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: # -*- 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 = 0
        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: 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))