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: class Node:
    prev = next = None

    def __init__(self, value):
        self.value = value

    def get_nxnx(self):
        next_n = self.next
        if not next_n: return None, None
        return next_n, next_n.next

    def get_prpr(self):
        prev_n = self.prev
        if not prev_n: return None, None
        return prev_n, prev_n.prev

    def combine(self):
        next_n, nxnx_n = self.get_nxnx()
        if not next_n or not nxnx_n: return False

        if self.value > abs(next_n.value) < nxnx_n.value:
            self.value += next_n.value + nxnx_n.value
            self.next = nxnx_n.next
            _, prpr = self.get_prpr()
            if prpr:
                prpr.combine()
            return True
        return False

    def iter(self):
        a = self
        while a:
            yield a.value
            a = a.next


while True:
    n = int(input())
    if not n:
        break
    a0 = a = Node(int(input()))
    n -= 1
    while n:
        an = int(input())
        if an * a.value < 0:
            at = Node(an)
            at.prev = a
            a.next = at
            a = at
        else:
            a.value += an
        n -= 1
    if a0.value < 0:
        a0 = a0.next
    a = a0
    while a:
        while a.combine():
            pass
        _, a = a.get_nxnx()
    print(max(v for v in a0.iter()))

Python: while 1:
    n=int(input())
    if n==0:break
    nlist=[]
    nans=-100001
    for i in range(n):
        nlist.append(int(input()))
    nkeep=0
    for i in nlist:
        if i>0 and nkeep>=0:
            nkeep+=i
        elif nkeep<0:
            nkeep=i
        if nans<nkeep:nans=nkeep
    if max(nlist)<0:nans=max(nlist)
    print(nans)

Python: while 1:
    n=int(input())
    if n==0:break
    nlist=[]
    nans=-100001
    for i in range(n):
        nlist.append(int(input()))
        for j in range(i+1):
            nn=0
            for k in range(j,i+1):
                nn+=nlist[k]
            if nans<nn:nans=nn
    print(nans)

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

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

Python: while 1:
    n=input()
    if n==0:break
    a=[int(raw_input()) for _ in xrange(n)]
    max_a=-1000000
    for i in xrange(n):
        for j in xrange(i+1,n+1):
            if max_a<sum(a[i:j]):
                max_a=sum(a[i:j])
    print(max_a)

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

Python: while 1:
    n=int(input())
    if not n:break
    l=[int(input()) for _ in range(n)]
    print(max(sum(l[j:i]) for i in range(len(l)+1) for j in range(i)))

Python: def solve0(v):
    w = []
    for i in range(len(v)):
        for j in range(len(v[i:])):
            w.append(sum(v[i:(j+1)]))
    return(max(w))

def sign(x):
    if x >= 0:
        s=+1
    else:
        s=-1
    return(s)

def solve(v):
    w = []
    sig=sign(v[0])
    x = v[0]    
    for i in range(1,len(v)):
        if v[i] * sig >= 0:
            x += v[i]
        else:
            w.append(x)
            sig=sign(v[i])
            x=v[i]
    w.append(x)
#    print(w)
    return(solve0(w))      

if __name__ == "__main__":
    while True:
        n = int(input())
        if n == 0:
            break
        v = []    
        for i in range(n):
            v.append(int(input()))

        print(solve(v))

Python: while True:
    n = int(input())
    if n == 0:
        break
    a = [int(input()) for _ in range(n)]
    minv = maxv = tmp = 0
    for i in range(n):
        tmp += a[i]
        maxv = max(maxv, tmp - minv)
        minv = min(minv, tmp)
    print(maxv)

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

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

    tup = tuple(int(stdin.readline()) for _ in xrange(n))
    L = [tup[0]]
    for i in tup[1:]:
        if 0 > i and 0 > L[-1]:
            L[-1] += i
        elif 0 <= i and 0 <= L[-1]:
            L[-1] += i
        else:
            L.append(i)

    while True:
        length = len(L)
        if L[0] <= 0:
            L.pop(0)
            continue
        if L[-1] <= 0:
            L.pop()
            continue
        if length > 1 and L[0] + L[1] <= 0:
            L.pop(0)
            L.pop(0)
            continue
        if length > 1 and L[-1] + L[-2] <= 0:
            L.pop()
            L.pop()
            continue
        if length > 2 and sum(L[:3]) >= L[2]:
            L[2] = sum(L[:3])
            L.pop(0)
            L.pop(0)
            continue
        if length > 2 and sum(L[-3:]) >= L[-3]:
            L[-3] = sum(L[-3:])
            L.pop(0)
            L.pop(0)
            continue
        break

    m = 0
    for i in xrange(len(L)):
        for j in xrange(1 + i, len(L) + 1):
            t = sum(L[i:j])
            if t > m:
                m = t
    print(m)

Python: while True:
	n=int(input())
	if n==0:
		break
	A=[]
	for i in range(n):
		A.append(int(input()))
	MAX=[0]*(len(A))
	end=len(A)
	for i in range(end):
		for j in range(i+1,end+1):
			if MAX[i]<sum(A[i:j]):
				MAX[i]=sum(A[i:j])
	MAX.sort()
	print(MAX[-1])

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

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=input()
    if n==0:break
    a=[int(raw_input()) for _ in xrange(n)]
    wa=[0]*(n+1)
    max_a=-1000000
    for i in xrange(n):
        wa[i+1]=a[i]+wa[i]
    for i in xrange(n):
        for j in xrange(i,n+1):
            if max_a<wa[j]-wa[i]:
                max_a=wa[j]-wa[i]
    print(max_a)