Source Code
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

void solve() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }

  vector<ll> ans;
  ll running = 0;
  for (ll i = n - 1; i >= 0; --i) {
    running += a[i];
    if (i == 0) {
      ans.push_back(running);
      break;
    }
    if (running >= 0) {
      ans.push_back(running);
      running = 0;
    }
  }

  reverse(ans.begin(), ans.end());
  while (ans.size() > 1 && ans.back() == 0) {
    ans.pop_back();
  }

  for (ll x : ans) {
    cout << x << ' ';
  }
  cout << '\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
Copy
b Kamoon
GNU G++17
33 ms
6.0 MB
Accepted