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

typedef long long int Long;
const int N = 3e5 + 5;

int n;
pair<Long, int> a[N];

int main() {
  cin >> n;
  Long sum = 0;
  for (int i = 0; i < n; i++) {
    Long x; cin >> x;
    sum += x;
    a[i] = {sum, -i};
  }
  sort(a, a + n);

  int cur = 0, lastIdx = -1;
  Long last = 0;
  while (cur < n) {
    Long v = a[cur].first;
    int idx = -a[cur].second;
    cout << (v - last);
    last = v;
    lastIdx = idx;
    
    while (cur < n && -a[cur].second <= lastIdx) {
      ++cur;
    }
    if (cur < n) cout << " ";
  }
  cout << '\n';
  return 0;
}
Copy
b dauom
GNU G++17
134 ms
7.0 MB
Wrong Answer