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

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

  vector<int> ans;
  int running = 0;
  for (int 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 (int 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
0 ms
572 KB
Wrong Answer