Problem:


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 contiquous 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 end 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



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


Sample output:
19
14
1001

Submission status: Wrong Answer

Solution code:
while 1:
    n = int(raw_input())
    if n==0:
        break
    m = 0
    pm = 0
    for i in range(n):
        m += int(raw_input())
        pm = max(m, pm)
    print pm

Submission status: Wrong Answer

Solution code:
while True:
    n=int(input())
    if n==0:
        break
    a=[int(input()) for i in range(n)]
    MAX=0
    SUM=0
    for i in a:
        SUM+=i
        MAX=max(MAX,SUM)
        if SUM<0:
            SUM=0
    print(MAX)

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
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]==0:
            continue
        elif 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))

Submission status: Wrong Answer

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

    a = []
    for i in range(n):
        a.append(int(input()))

    maxp = 0
    maxcont = 0

    for i in range(n):
        maxcont = max(0, maxcont + a[i])
        maxp = max(maxp, maxcont)

    print(maxp)

Submission status: Wrong Answer

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

    a = []
    for i in range(n):
        a.append(int(input()))

    maxp = 0
    maxcont = 0

    for i in range(n):
        maxcont = max(a[i], maxcont + a[i])
        maxp = max(maxp, maxcont)

    print(maxp)

Submission status: Wrong Answer

Solution code:
# -*- 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 = max(0, A[i])
        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))

Submission status: Wrong Answer

Solution code:
# -*- 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 = max(0, A[i])
        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))

Submission status: Wrong Answer

Solution code:
# -*- 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))

Submission status: Wrong Answer

Solution code:


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))

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
import sys
while True:
    n=int(input())
    if n==0:
        sys.exit()

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

    maximum=0
    for i in range(n+1):
        for j in range(i):
            maximum=max(maximum,a[i]-a[j])


    if maximum==0:
        b=a.copy()
        b.pop(0)
        maximum=max(b)

    print(maximum)

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0022: Maximum Sum Sequence
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def max_sum_subsequence(seq):
    maxsofar = 0
    maxendinghere = 0
    for s in seq:
        # invariant: maxendinghere and maxsofar are accurate
        # are accurate up to s
        maxendinghere = max(maxendinghere + s, 0)
        maxsofar = max(maxsofar, maxendinghere)
    return maxsofar

while True:
    try:
        N = int(input())
    except EOFError:
        break
    if N == 0:
        break
    seq = [int(input()) for _ in range(N)]
    print(max_sum_subsequence(seq))

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0022: Maximum Sum Sequence
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def max_sum_subsequence(seq):
    maxsofar = 0
    maxendinghere = 0
    for s in seq:
        # invariant: maxendinghere and maxsofar are accurate
        # are accurate up to s
        maxendinghere = max(maxendinghere + s, 0)
        maxsofar = max(maxsofar, maxendinghere)
    return maxsofar

while True:
    N = int(input())
    if N == 0:
        break
    seq = [int(input()) for _ in range(N)]
    print(max_sum_subsequence(seq))

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0022: Maximum Sum Sequence
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def max_sum_subsequence(seq):
    maxsofar = 0
    maxendinghere = 0
    for s in seq:
        # invariant: maxendinghere and maxsofar are accurate
        # are accurate up to s
        maxendinghere = max(maxendinghere + s, 0)
        maxsofar = max(maxsofar, maxendinghere)
    return max(maxsofar, max(seq))

while True:
    N = int(input())
    if N == 0:
        break
    seq = [int(input()) for _ in range(N)]
    print(max_sum_subsequence(seq))

Submission status: Wrong Answer

Solution code:
while 1:
  n = input()
  if n == 0: break
  a = []
  for i in range(n):
    k = input()
    a.append(k)
  print max([sum(a[i:(j+1)]) for i in range(7) for j in range(i,7)])

Submission status: Wrong Answer

Solution code:
while 1:
  n = input()
  if n == 0: break
  a = []
  sum = 0
  for i in range(n):
    k = input()
    a.append(k)
    sum += k
  i = 0
  j = n - 1
  sums = [sum]
  while i != j:
  	if a[i] < a[j]:
  		sums.append(sums[-1] - a[i])
  		i += 1
  	else:
  		sums.append(sums[-1] - a[j])
  		j -= 1
  print max(sums)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    sumlist=[]
    sum=0
    max=-10000
    if n==0:
        break
    for i in range(n):
         sumlist.append(int(input()))
    for i in range(n):
        sum=0
        for j in range(0,n-i):
            sum=sumlist[i+j]+sum
            if max<sum:
                max=sum

    print(max)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    sumlist=[]
    max=-100000
    if n==0:
        break
    for i in range(n):
         sumlist.append(int(input()))
    sum=0
    buf=0
    for i in range(n):
        buf = sumlist[i]+buf
        if sumlist[i] > max:
            sum = sumlist[i]
            max = sumlist[i]
            buf = 0
        if buf + sum > max:
            sum = sum+buf
            max=sum
            buf = 0

    print(max)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    sumlist=[]
    max=-100000
    if n==0:
        break
    for i in range(n):
         sumlist.append(int(input()))
    sum=0
    buf=0
    for i in range(n):
        buf = sumlist[i]+buf
        if buf + sum > max:
            sum = sum+buf
            max=sum
            buf = 0
        if sumlist[i] > max:
            sum = sumlist[i]
            max = sumlist[i]
            buf = 0
    print(max)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    sumlist=[]
    max=-100000
    if n==0:
        break
    for i in range(n):
         sumlist.append(int(input()))
    sum=0
    buf=0
    for i in range(n):
        buf = sumlist[i]+buf
        if buf + sum > max:
            sum = sum+buf
            max=sum
            buf = 0
        if sumlist[i] > max and (buf <= 0):
            sum = sumlist[i]
            max = sumlist[i]
            buf = 0

    print(max)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    sumlist=[]
    max=-100000
    if n==0:
        break
    for i in range(n):
         sumlist.append(int(input()))
    sum=0
    buf=0
    for i in range(n):
        buf = sumlist[i]+buf

        if sumlist[i] > max and (buf <= 0):
            sum = 0
            max = sumlist[i]
            buf = 0

        elif buf + sum > max:
            sum = sum+buf
            max=sum
            buf = 0
    print(max)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    sumlist=[]
    max=-100000
    if n==0:
        break
    for i in range(n):
         sumlist.append(int(input()))
    sum=0
    buf=0
    for i in range(n):
        buf = sumlist[i]+buf
        if buf + sum > max:
            sum = sum+buf if sum + buf > sumlist[i] else sumlist[i]
            max=sum
            buf = 0
        elif sumlist[i] > max and (buf <= 0):
            sum = 0
            max = sumlist[i]
            buf = 0
    print(max)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())

    if n == 0:
        break

    sum = 0

    for _ in range(n):
        sum += max(int(input()), 0)

    print(sum)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
while n!=0:
    list=[0]*n
    for i in range(n):
        list.append(int(input()))
    a=0
    b=0
    for i in range(n):
        for j in range(n-i):
            a+=list[i+j]
        if b<a:
            b=a
    print a
    n=int(input())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
while(1):
    a=[]
    result=0
    n = int(input())
    if n==0:
        break
    a=[int(input()) for i in range(n)]
    for i in range(n):
        if a[i]>=0:
            result=result+a[i]
    print(result)

Submission status: Wrong Answer

Solution code:
while(1):
    a=[]
    n = int(input())
    if n==0:
        break
    a=[int(input()) for i in range(n)]
    result=0
    for i in range(n):
        result1=a[i]
        if result<=a[i]:
            result=a[i]
        for j in range(i+1,n):
            result1=result1+a[j]
            if result<=result1:
                result=result1
    print(result)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
n = input()
while n != 0:
    slist = []
    for i in range(n):
        slist.append(input())
    a = 0
    b = 0

    for i in range(n):
        for j in range(n - i):
            a += slist[i + j]
            if b < a:
                b = a
        a = 0

    print b
    n = input()

Submission status: Wrong Answer

Solution code:
import math

def sign(x):
    if x >= 0:
        return True
    else:
        return False

n = int(input())
while n != 0:
    a = []
    for i in range(n):
        a.append(int(input()))
    b = []
    b.append(a[0])
    for i in range(1,len(a)):
        if sign(b[len(b)-1]) == sign(a[i]):
            b[len(b)-1] = b[len(b)-1] + a[i]
        else:
            b.append(a[i])

    ans = b[0]
    for i in range(len(b)):
        S = 0
        for j in range(i,len(b)):
            S = S + b[j]
            ans = max(S, ans)
    print(ans)
    n = int(input())

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
while(1):
    N = int(input())
    if N==0: break
    sums = []
    s = 0
    for i in range(N):
        s += int(input())
        if s<0: s=0
        sums.append(s)
    print(max(sums))

Submission status: Wrong Answer

Solution code:
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])
    for i in range(2, len(d)):
        if d[i - 1] < 0 and d[i -2] > d[i - 1] and d[i] > d[i - 1]:
            d[i] += d[i -2] + d[i -1]
    print(max(d))

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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 len(d) <= 2:
        print(max(max(d), max(a)))
    else:
        if d[0] < 0:
            d.pop(0)
        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)

Submission status: Wrong Answer

Solution code:
while True:
  INF = 10 ** 20
  n = int(input())
  if not n:
    break
  cum_sum = [0]
  acc = 0
  for i in range(n):
    acc += int(input())
    cum_sum.append(acc)
  ans = -INF
  for i in range(n):
    ans = max(ans, max(cum_sum[i:]) - cum_sum[i])
  print(ans)

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  acc = int(input())
  min_n = min(0, acc)
  max_n = acc
  ans = acc
  for _ in range(n - 1):
    acc += int(input())
    min_n = min(min_n, acc)
    max_n = max(max_n, acc)
    ans = max(ans, max_n - min_n)
  print(ans)

Submission status: Wrong Answer

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  acc = int(input())
  min_n = min(0, acc)
  max_n = max(0, acc)
  ans = acc
  for _ in range(n - 1):
    acc += int(input())
    min_n = min(min_n, acc)
    max_n = max(max_n, acc)
    ans = max(ans, max_n - min_n)
  print(ans)

Submission status: Wrong Answer

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  acc = int(input())
  min_n = acc
  max_n = acc
  ans = acc
  for _ in range(n - 1):
    acc += int(input())
    min_n = min(min_n, acc)
    max_n = max(max_n, acc)
    ans = max(ans, max_n - min_n, max_n)
  print(ans)

Submission status: Wrong Answer

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  acc = int(input())
  min_n = min(0, acc)
  max_n = acc
  ans = acc
  for _ in range(n - 1):
    acc += int(input())
    max_n = max(max_n, acc)
    ans = max(ans, max_n - min_n)
    min_n = min(min_n, acc)
  print(ans)

Submission status: Wrong Answer

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

import sys

def solve():
    while True:
        n = input()
        if n > 0:
            tmp_lst = []
            for i in xrange(n):
                tmp_lst.append(input())
            lst = compress(tmp_lst)
            accumurate_lst = calc_accumurate_lst(lst)
            max_value = 0
            size = len(lst)
            for to in xrange(size):
                for frm in xrange(to + 1):
                    value = accumurate_lst[to] if frm == 0 else accumurate_lst[to] - accumurate_lst[frm - 1]
                    if max_value < value:
                        max_value = value
            print max_value
        else:
            sys.exit()

def calc_accumurate_lst(lst):
    accum = [0 for i in lst]
    accum[0] = lst[0]
    for i in xrange(1, len(lst)):
        accum[i] = accum[i - 1] + lst[i]
    return accum
        
#同じ符号の数列は圧縮できる
def compress(tmp_lst):
    lim = len(tmp_lst)
    #リスト圧縮
    lst = []
    index = 0
    while index < lim:
        tmp = tmp_lst[index]
        index += 1
        while index < lim and tmp * tmp_lst[index] > 0:
            tmp += tmp_lst[index]
            index += 1
        lst.append(tmp)
    return lst
        

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!/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()

Submission status: Wrong Answer

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    numbers = [int(raw_input()) for i in range(n)]
    for i in range(n):
        ans = 0
        tmp = 0
        for j in range(i,n):
            tmp += j
            ans = max(ans,tmp)
    print ans

Submission status: Wrong Answer

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    numbers = [int(raw_input()) for i in range(n)]
    ans = 0
    for i in range(n):
        tmp = 0
        for j in range(i,n):
            tmp += j
            ans = max(ans,tmp)
    print ans

Submission status: Wrong Answer

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    numbers = [int(raw_input()) for i in range(n)]
    ans = 0
    for i in range(n):
        tmp = 0
        for j in range(i,n):
            tmp += numbers[j]
            ans = max(ans,tmp)
    print ans

Submission status: Wrong Answer

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    numbers = [int(raw_input()) for i in range(n)]
    if max(numbers) <= 0:
        print max(numbers)
    ans = 0
    for i in range(n):
        tmp = 0
        for j in range(i,n):
            tmp += numbers[j]
            ans = max(ans,tmp)
    print ans

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
while True:
	n = input()
	if n == 0:
		break
	else:
		nums = []
		for val in range(1,n+1):
			num = int(raw_input())
			nums.append(num)
		sums = []
		for val in range(0,n):
			sums.append(nums[val-1]+nums[val])
		sums.sort()
		sums.reverse()
		print sums[0]
		continue

Submission status: Wrong Answer

Solution code:

import sys


def max_sum_seq(lis):
    m = 0
    lis = compress(lis)
    l = len(lis)

    max_val = 0
    tmp_val = 0
    for i in lis:
        tmp_val = max(0, tmp_val + i)
        max_val = max(max_val, tmp_val)
    return max_val

#def every_slice(lis, n):
#    l = len(lis)
#    for i in range(l-n+1):
#        yield lis[i:i+n]

def compress(tmp_lst):
    lim = len(tmp_lst)
    lst = []
    index = 0
    while index < lim:
        tmp = tmp_lst[index]
        index += 1
        while tmp > 0 and index < lim and tmp * tmp_lst[index] > 0:
            tmp += tmp_lst[index]
            index += 1
        lst.append(tmp)
    return lst


while True:
    n = int(sys.stdin.readline())
    if n == 0:
        break
    lis = []
    for i in range(n):
        lis.append(int(sys.stdin.readline()))
    print max_sum_seq(lis)

Submission status: Wrong Answer

Solution code:

import sys


def max_sum_seq(lis):
    m = 0
    lis = compress(lis)
    l = len(lis)

    max_val = 0
    tmp_val = 0
    for i in xrange(0, l):
        tmp_val = 0
        for j in xrange(i, l):
            tmp_val = tmp_val + lis[j]
            max_val = max(max_val, tmp_val)

    return max_val

#def every_slice(lis, n):
#    l = len(lis)
#    for i in range(l-n+1):
#        yield lis[i:i+n]

def compress(tmp_lst):
    lim = len(tmp_lst)
    lst = []
    index = 0
    while index < lim:
        tmp = tmp_lst[index]
        index += 1
        while tmp > 0 and index < lim and tmp * tmp_lst[index] > 0:
            tmp += tmp_lst[index]
            index += 1
        lst.append(tmp)
    return lst


while True:
    n = int(sys.stdin.readline())
    if n == 0:
        break
    lis = []
    for i in range(n):
        lis.append(int(sys.stdin.readline()))
    print max_sum_seq(lis)

Submission status: Wrong Answer

Solution code:

import sys


def max_sum_seq(lis):
    m = 0
#    lis = compress(lis)
    l = len(lis)

    max_val = 0
    tmp_val = 0
    for i in xrange(0, l):
        tmp_val = 0
        for j in xrange(i, l):
            tmp_val = tmp_val + lis[j]
            max_val = max(max_val, tmp_val)

    return max_val

#def every_slice(lis, n):
#    l = len(lis)
#    for i in range(l-n+1):
#        yield lis[i:i+n]

#def compress(tmp_lst):
#    lim = len(tmp_lst)
#    lst = []
#    index = 0
#    while index < lim:
#        tmp = tmp_lst[index]
#        index += 1
#        while tmp > 0 and index < lim and tmp * tmp_lst[index] > 0:
#            tmp += tmp_lst[index]
#            index += 1
#        lst.append(tmp)
#    return lst


while True:
    n = int(sys.stdin.readline())
    if n == 0:
        break
    lis = []
    for i in range(n):
        lis.append(int(sys.stdin.readline()))
    print max_sum_seq(lis)