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
#include<cstdio>
#include<algorithm>

using namespace std;

const int SIZE = 500*1001;

int in[SIZE];
long long partial[SIZE];

int res[SIZE];

int main() {
    int n;
    scanf("%i", &n);
    for (int i = 0; i < n; ++i)
        scanf("%i", &in[i]);
    
    partial[0] = 0;
    for (int i = 0; i < n; ++i)
        partial[i+1] = partial[i] + in[i];
    
    long long s = partial[n];
    res[n] = 0;
    
    if (in[n-1] >= 0) {
        res[n-1] = 0;
    } else {
        res[n-1] = -1;
    }
    
    for (int i = n-2; i >= 0; --i) {
        if (s - partial[i] < 0) {
            res[i] = -1;
        } else {
            res[i] = 0;
            
            if (in[i] >= 0 and res[i+1] != -1)
                res[i] = res[i+1] + 1;
            
            for (int j = 1; i+j < n-1; ++j) {
                if ( (partial[i+j+1] - partial[i] >= 0) and (res[i+j+1] != -1))
                    res[i] = max(res[i], 1+res[i+j+1]);
            }
        }
    }
    
    if (res[0] == -1) {
        printf("-1\n");
    } else {
        printf("%i\n", n-1-res[0]);
    }
    
    return 0;
}