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
#include <iostream>

using namespace std;


int main() {
  int n;
  cin >> n;
  int xs[n];
  unsigned int C[n];

  for (int i = 0; i < n; ++i) {
    cin >> xs[i];
  }

  C[0] = (xs[0] > 0) ? 0 : -1;

  for (int i = 1; i < n; ++i ) {
    int p = 0;
    unsigned int c = -1;
    unsigned int l = 0;
    for (int j = i; j > 0; --j) {
      p += xs[j];
      if ((p >= 0) && (C[j - 1] != -1)) c = min(c, C[j - 1] + l);
      l++;
    }
    p += xs[0];
    if (p >= 0) c = min(c, l);

    C[i] = c;
  }

  cout << (int)C[n-1] << endl;
}