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

int main(){
  long long n,k;
  cin>>n;
  vector<long long> v;
  cin>>k;
  v.push_back(k);
  int g = 0;
  for(int i = 0; i < n-1 ; i++){
    cin>>k;
    if(k<0){
      v[v.size()-1] += k;
      while(v[v.size()-1]<0 && v.size() != 1){
        long long y = v[v.size()-1];
        v.pop_back();
        v[v.size()-1] += y;
      }
      g++;
    }
    else if(k == 0){
      if(i == n-2 || g >= 1)
        continue;
      else
        v.push_back(0);
    }
    else{
      v.push_back(k);
    }

  }
  if(v.size()!= 1)
  while(v[v.size()-1] == 0)
    v.pop_back();
  for(int i = 0; i < v.size() ; i++){
    cout<<v[i]<<" ";
  }
}
Copy
b Duukh
GNU G++17
88 ms
3.5 MB
Wrong Answer