Source Code
#include <bits/stdc++.h>
#define mod 1000000007
#define point complex<long double>
#define pi acos(-1)

typedef long long ll;
typedef long double ld;

using namespace std;

void Fastio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

bool runTestCase() {
	int n; cin >> n;
	vector<ll>v;
	ll x; cin >> x;
	v.push_back(x);
	for (int i = 1; i < n; i++) {
		cin >> x;
		if (x < 0) {
			v.back() += x;
			while (v.size() > 1 && v.back() < 0)
				v[v.size() - 2] += v.back(), v.pop_back();
		}
		else v.push_back(x);
	}
	while (v.size() > 1 && v.back() == 0)
		v[v.size() - 2] += v.back(), v.pop_back();
	for (auto i : v) { cout << i << " "; }cout << '\n';
	return 0;
}
int main()
{
	Fastio();
	int ttt = 1; //cin >> ttt;
	while (ttt--) {
		runTestCase();
	}

	return 0;
}
Copy
b 7eZb_ElNa7o
GNU G++17
34 ms
3.6 MB
Accepted