Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int have[100000];
int main(){
    ios_base::sync_with_stdio(0);
    int n;
    cin>>n;
    stack<ll> st;
    for (int i=0;i<n;i++) {
        ll x;
        cin>>x;
        st.push(x);
        while(st.size()>1 && st.top()<0){
            ll x = st.top();
            st.pop();
            ll y = st.top();
            st.pop();
            st.push(x+y);
        }
    }
    vector<ll> ret;
    while(!st.empty()) {
        ret.push_back(st.top());
        st.pop();
    }
    reverse(ret.begin(), ret.end());
    while(ret.size()>1 && ret.back()==0) ret.pop_back();
    cout<<ret[0];
    for (int i=1;i<ret.size();i++) cout<<' '<<ret[i];
    cout<<endl;
}

Copy
b RedNextCentury
GNU G++17
49 ms
4.8 MB
Accepted