1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int n;
  cin >> n;
  vector<int> sum(n, -1000LL * 1000 * 1000 * 1000 * 1000);
  sum[0] = 0;
  int buffer = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    auto found = lower_bound(sum.rend() - i, sum.rend(), -buffer);
    int found_nr = (found == sum.rend()) ? -1 : (sum.rend() - found);
    if (found_nr != -1) {
      sum[found_nr] = -buffer;
    }
    buffer += x;
  }
  auto found = lower_bound(sum.rbegin(), sum.rend(), -buffer);
  int res = (found == sum.rend()) ? -1 : (n - (sum.rend() - found));
  cout << res << endl;
  return 0;
}