Source Code
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> v(n);
  for (long long& a : v) cin >> a;

  int at = 0;
  vector<bool> was(n, true);
  while (at < n) {
    int nxt = at + 1;
    while (nxt < n && v[at] + v[nxt] <= v[at]) {
      v[at] += v[nxt];
      was[nxt] = false;
      ++nxt;
    }
    at = nxt;
  }
  for (int i = 0; i < n; i++) {
    if (was[i]) {
      cout << v[i] << ' ';
    }
  }
  cout << endl;
}
Copy
b MaxHeap
GNU G++17
0 ms
684 KB
Wrong Answer