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
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define repeat(i, x) for (int i = 0; i < (x); ++i)
using ll =  long long;
template<typename A, typename B> inline A& mini(A& a, B b) {return a = min(a, b); }

ll a[int(5e5)+5];
ll sum[int(5e5)+5];

inline ll seg(int b, int e)
{
	if (b == 0)
		return sum[e];
	else
		return sum[e] - sum[b-1];
}
    	
vector<vector<ll>> d;

int minimal(int b, int e)
{
	if (b == e)
	{
		if (a[b] < 0)
			return -1;
		else
			return 0;
	}
	
	if (seg(b, e) < 0)
		return -1;
		
	if (d[b][e] != -2)
		return d[b][e];
		
	int mn = e - b;
	
	for (int i = b; i < e; ++i)
	{
		int x = minimal(b, i);
		int y = minimal(i+1, e);
		
		if (x!=-1 && y!=-1)
			mini(mn, x+y);
	}
	
	d[b][e] = mn;
	return mn;
}



int32_t main()
{
    fastIO;

    int n;
    cin >> n;
    
    ll pref = 0;
    
    repeat(i, n)
    {
    	cin >> a[i];
    	pref += a[i];
    	sum[i] = pref;
    }
    
	d = vector<vector<ll>>(n, vector<ll>(n, -2));
	
	cout << minimal(0, n-1);
	
	

    return 0;
}