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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <cstdio>

#define ll long long

const ll INF = 1001002003001002003LL;

using namespace std;

ll h[500123];
ll s[500123];
int m[500123];

int longest(ll* a, int n) {
  for(int i=0;i<n;i++) {
    if(a[i] < 0) {
      a[i] = INF;
    }
  }

  ll last = a[n-1];
  int len = 0;
  for(int i=1;i<n;i++) {
    int lo = 1;
    int hi = len;
    while(lo <= hi) {
      int mid = (lo+hi+1)/2;
      if(a[m[mid]] > a[i] || a[m[mid]] > last) {
        hi = mid-1;
      } else {
        lo = mid+1;
      }
    }
    int nl = lo;
    m[nl] = i;
    if(nl > len) {
      len = nl;
    }
  }

  return len; 
}

int solve(int n) {
  for(int i=0;i<n;i++) {
    s[i+1] = s[i] + h[i];
  }
  if(s[n] < 0) {
    return -1;
  }
  return n - longest(s, n + 1);
}

int main(int argc, char** argv) {
  int n;
  scanf("%d", &n);
  for(int i=0;i<n;i++) {
    scanf("%lld", &(h[i]));
  }

  int res = solve(n);

  printf("%d\n", res);
  return 0;
}