Source Code
n = int(input())
a = [*map(int,input().split())]
b = []
i = 0
while(i<n):
    mini = 0
    if(i<n and a[i]>0):
        b.append(a[i])
        i+=1
    if(i<n and a[i]==0):
        i+=1
    while(i<n and a[i]<0):
        mini += a[i]
        i+=1
    if(mini!=0):
        b.append(mini)
c = []
last = 0
for i in range(len(b)-1):
    if(a[i]==b[i] and b[i+1]<0):
        c.append(b[i]+b[i+1])
        last = i+2
        break
    else:
        c.append(b[i])
if(last<len(b)):
    c.append(sum(b[last:]))
if(len(c)==1):
    print(*c)
    exit(0)
d = [c[i] for i in range(len(c)-2)]
if(c[-1]+c[-2]<a[len(c)-2]):
    d += [c[-1]+c[-2]]
else:
    d+= [c[i] for i in range(len(c)-2,len(c))]
print(*d)
Copy
b tamahom
Python 3
21 ms
4.0 MB
Wrong Answer