Go: package main

import (
        "fmt"
)

func main() {
        for {
                var n int
                _, err := fmt.Scanf("%d", &n)
                if err != nil || n == 0 {
                        break
                }

                var sum [5001]int

                for i := 1; i <= n; i++ {
                        fmt.Scanf("%d", &(sum[i]))
                        if i > 1 {
                                sum[i] += sum[i-1]
                        }
                }

                max_sum := sum[1]
                for i := 1; i <= n; i++ {
                        for j := 0; j < i; j++ {
                                if max_sum < sum[i] - sum[j] {
                                        max_sum = sum[i] - sum[j]
                                }
                        }
                }
                fmt.Printf("%d\n", max_sum)
        }
}

Python: import sys

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

    for i in range(len(a)+1):
        for j in range(i, len(a)+1):
            if sum(a[i:j]) > sumMax:
                sumMax = sum(a[i:j])

    print(sumMax)

Python: def max_sub(array):
    x = max(array[0], 0)
    ans = 0
    for a in array[1:]:
        x = max(0, x+a)
        ans = max(ans, x)
    return ans

while True:
    n = int(input())
    if n == 0:
        break
    array = [int(input()) for _ in range(n)]
    print(max_sub(array))

Python: import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    a = [int(f.readline()) for i in range(n)]
    sum_max = now = 0
    for ai in a:
        now = max(0, now + ai)
        sum_max = max(sum_max, now)
    print(sum_max if sum_max else max(a))

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 length > 1 and L[0] <= 0:
            L.pop(0)
            continue
        if length > 1 and 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
    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 True:
    n = int(input())
    if n == 0:
        break
    a = [int(input()) for _ in range(n)]
    minv = tmp = 0
    maxv = -500000
    for i in range(n):
        tmp += a[i]
        maxv = max(maxv, tmp - minv)
        minv = min(minv, tmp)
    print(maxv)

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

    ans=0
    l=[]

    for _ in range(n):
        l.append(int(input()))
    for i in range(n):
        s=-1e100
        for j in range(i,n):
            s+=l[j]
            if ans < s: ans = s
    print(ans)

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

n = []
a = []

i = 0
while True:
    n.append(int(input()))
    if n[i] == 0:
        break
    for j in range(0,n[i]):
        a.append(int(input()))
    i += 1

for i in range(0,len(n)):
    sumMax = 0
    for j in range(sum(n[0:i-1 if i != 0 else 0]),sum(n[0:i])):
        for k in range(j,sum(n[0:i])+1):
            if j==k:
                continue
            sumMax = max(sumMax,sum(a[j:k]))
    print(sumMax)

Python: while True:
    N = int(input())
    if N == 0:
        break
    A = [int(input()) for i in range(N)]
    ans = 0
    cur = 0
    for i in A:
        cur = max(cur + i,i)
        ans = max(ans,cur)
    print(ans)

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()))
    maximum=0
    for i in range(n):
        for j in range(i,n+1):
            maximum=max(maximum,sum_list[j]-sum_list[i])

    if maximum==0:
        sum_list.pop(0)
        maximum=max(sum_list)
    print(maximum)

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

def grouping(nums):
    it = iter(nums)
    L = [next(nums)]
    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
    while 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 = grouping(int(stdin.readline()) for _ in xrange(n))
    if len(L) > 1 and L[0] <= 0:
        L.pop(0)
    if len(L) > 1 and L[-1] <= 0:
        L.pop()

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

    val += collect(list(reversed(L[:idx])))
    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)]
    result=[]
    for i in range(n):
        result1=a[i]
        result.append(result1)
        for j in range(i+1,n):
            result1=result1+a[j]
            result.append(result1)
    print(max(result))

Python: while True:
    N = int(input())
    if N == 0:
        break
    A = [int(input()) for i in range(N)]
    ans = A [0]
    cur = 0
    for i in A:
        cur = max(cur + i,i)
        ans = max(ans,cur)
    print(ans)

Python: while(1):
	n = int(input())
	if n == 0:
		break
	elif n == 1:
		print(int(input()))
	else:
		a = [0 for i in range(n+1)]
		for i in range(n):
			b = int(input())
			a[i+1] = a[i] + b
		c = [0 for i in range(n)]
		for i in range(n):
			c[i] = max(a[i+1:]) - a[i]
		print(max(c))

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

    a = (int(raw_input()) for _ in xrange(n))
    b = []
    c = 0
    for ai in a:
        if ai == 0:
            continue
        if (c > 0 and ai < 0) or (c < 0 and ai > 0):
            b.append(c)
            c = 0
        c += ai

    if c != 0:
        b.append(c)

    l = len(b)
    i = 0 if b[0] > 0 else 1
    s = b[i]
    ans = s

    j = i + 2
    while j < l:
        s += b[j - 1] + b[j]
        while i < j:
            ns = s - b[i] - b[i + 1]
            if ns < s:
                break
            s = ns
            i += 2
        ans = max(s, ans)
        j += 2

    print(ans)

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