Title: Maximum Sum Sequence

Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that a subsequence of one element is also a contiguous subsequence.

Input:
The input consists of multiple datasets. Each data set consists of:
n
a1
a2
...
an

You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.

The input ends with a line consisting of a single 0.

Output:
For each dataset, print the maximum sum in a line.

Sample Input:
7
-5
-1
6
4
9
-6
-7
13
1
2
3
2
-2
-1
1
2
3
2
1
-2
1
3
1000
-200
201
0

Output for the Sample Input:
19
14
1001

# Buggy version:
while True:
    N=int(input())
    if N==0:
        break
    num=0
    res=-11111111
    for i in range(N):
        a=int(input())
        num=max(num+a,a)
        res=max(num,res)

    print(num)



# Fixed version:
while True:
    N=int(input())
    
    if N==0:
        break
    
    num=0
    res=-11111111
    for i in range(N):
        a=int(input())
        num=max(num+a, a)
        #print(num)
        res=max(num, res)
        #print(res)

    print(res)

# Buggy version:
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)

# Fixed version:
while True:
    n=int(input())
    if n == 0: break

    ans=-1e100
    l=[]

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

# Buggy version:


while True:
    num = int(input())

    if num == 0: 
        break
    
    list = []
    for i in range(num):
        list.append(int(input()))

    for i in range(1, num):
        list[i] = max(list[i - 1] + list[i], list[i])

    print((list))

# Fixed version:


while True:
    num = int(input())

    if num == 0: 
        break
    
    list = []
    for i in range(num):
        list.append(int(input()))

    for i in range(1, num):
        list[i] = max(list[i - 1] + list[i], list[i])

    print(max(list))

# Buggy version:
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))

# Fixed version:
from sys import stdin

def getMax(array):
    mx = mx2 = array[0]
    for i in array[1:]:
        mx2 = max(i, mx2 + i)
        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))

# Buggy version:
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))


# Fixed version:
while 1:
 n=int(input())
 if n==0:break
 a=[int(input())for _ in[0]*n]
 for i in range(1,n):a[i]=max(a[i],a[i]+a[i-1])
 print(max(a))

# Buggy version:
while True:
  n = int(input())
  if n==0:
    break
  l = []
  for i in range(n):
    l.append(int(input()))
  for i in range(1,n):
    l[i]=max()l[i-1]+l[i],l[i])
  print(max(l))

# Fixed version:
while True:
    n = int(input())
    if n == 0: break
    a = []
    for i in range(n):
        a.append(int(input()))
    for i in range(1, n):
        a[i] = max(a[i - 1] + a[i], a[i])
    print(max(a))