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
#include <stdio.h>
#include <algorithm>

int main()
{
    int n;
    int A[5000];
    
    scanf("%d", &n);
    
    if (n > 5000)
    {
        printf("-1\n");
        return 0;
    }
    
    for (int i = 0; i < n; i++)
        scanf("%d", &A[i]);
    
    int T[2][5000];
    
    for (int i = n-1; i >= 0; i--)
    {
        long long S = 0;
        for (int j = i; j >= 0; j--)
        {
            S += A[j];
            
            if (i == n-1)
            {
                if (S >= 0) T[i%2][j] = 0;
                else        T[i%2][j] = 1000000;
            }
            else
            {
                T[i%2][j] = T[(i+1)%2][j] + 1;
                if (S >= 0)
                    T[i%2][j] = std::min(T[i%2][j], T[(i+1)%2][i+1]);
            }
        }
    }
    
    if (T[0][0] > 5000)
        printf("-1\n");
    else
        printf("%d\n", T[0][0]);
    
    return 0;
}