Source Code
n = int(input())
a = list(map(int, input().strip().split()))

ans = []
for i in a:
    if i >= 0:
        ans.append(i)
    else:
        while i < 0 and ans:
            i += ans[-1]
            ans.pop()
        ans.append(i)

while ans[-1] == 0 and len(ans) > 1:
    ans.pop()

print(" ".join(map(str, ans)))
Copy
b m-salti
Python 3
162 ms
39.5 MB
Accepted