Source Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define ld long double
#define pii pair<int,int>
#define vll vector<vector<ll> >
#define vi vector<int>(n)
#define vl vector<ll>
#define deb(x) cout<<x<<"\n";
#define disp(x) cout<<x.first<<" "<<x.second<<"\n";
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const int MOD=1000000007;
const int MOD2=998244353;
const ll MAX=9187201950435737471;
const int N=100005;
const int M=100005;
const int INF=2;
const double EPS=1e-9;
const long double PI=acos(-1);
#define endl '/n'

struct stableSum {
    int cnt=0;
    vector<double>v,pref{0};
    void operator+=(double a){
        assert(a>=0);
        int s=++cnt;
        while(s%2==0){
            a+=v.back();
            v.pop_back(),pref.pop_back();
            s/=2;
        }
        v.push_back(a);
        pref.push_back(pref.back() + a);
    }
    double val() {return pref.back();}
};


int cmp(int a,int b){
    return a>b;
}


ll po(ll x,ll y){
    x%=MOD;
    ll ret=1;
    while(y>0){
        if(y&1){
            ret=(ret*x)%MOD;
        }
        y=y>>1ll;
        x=(x*x)%MOD;
    }
    return ret%MOD;
}

string tostring(ll x){
    string ret="";
    while(x){
        ret+=char(x%10+'0');
        x/=10;
    }
    reverse(ret.begin(),ret.end());
    return ret;
}



void solve(int T){
    int n;
    cin>>n;
    vector<ll>a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    stack<ll>s;
    for(int i=0;i<n;i++){
        s.push(a[i]);
        while(s.top()<0){
            ll x=s.top();
            s.pop();
            bool t=0;
            if(!s.empty()){
                ll y=s.top();
                s.pop();
                x+=y;
                t=1;
            }
            s.push(x);
            if(t==0)break;
        }
    }
    while(s.top()<=0){
        ll x=s.top();
        s.pop();
        bool t=0;
        if(!s.empty()){
            ll y=s.top();
            s.pop();
            x+=y;
            t=1;
        }
        s.push(x);
        if(t==0)break;
    }

    vector<int>ans;
    while(!s.empty()){
        ans.pb(s.top());
        s.pop();
    }
    int m=ans.size();
    for(int i=m-1;i>=0;i--){
        cout<<ans[i]<<" ";
    }
}

int main(){
    //freopen("test_input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    fastio
    int T=1;
    //cin>>T;
    for(int i=1;i<=T;i++){
        solve(i);
        //if(i<T)cout<<"\n";
    }
    return 0;
}

/*


*/
Copy
b mr_robo
GNU G++17
0 ms
740 KB
Wrong Answer