Source Code
#include <bits/stdc++.h>
using namespace std;

const int N = 100000;

int n,a[N*3 + 10],nxt[N*3 + 10];

int main() {
	
	scanf("%d",&n);
	for(int i = 1;i<=n;i++){
		scanf("%d",&a[i]);
		nxt[i] = i + 1;
	}	
	for(int i =1;i<=n;i++){
		if(nxt[i] <= n && a[nxt[i]] < 0){
			a[i] += a[nxt[i]];
			nxt[i] = nxt[nxt[i]];
			i--;
		}
	}
	vector<int> res;
	int tmp = 1;
	while(tmp <= n){
		res.push_back(a[tmp]);
//		printf("%d ",a[tmp]);
		tmp = nxt[tmp];
	}
	tmp = res.size();
	for(int i = res.size() - 1; i>=0;i--){
		if(res[i] == 0){
			tmp = i;
		}else{
			break;
		}
	}
	for(int i = 0;i<tmp;i++){
		printf("%d ",res[i]);
	}
	return 0;
}
Copy
b bnahmad15
GNU G++17
1 ms
684 KB
Wrong Answer