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
#include <bits/stdc++.h>
using namespace std;


const int N = 5e5 + 5;


int n, dp[N], b[N], c[N];
long long pref[N];

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		int x;
		cin >> x;
		b[i] = b[i - 1] + (x < 0);
		if (x < 0) c[i] = i;
		else c[i] = c[i - 1];
		pref[i] = pref[i - 1] + x;
	}
	for(int i = 1; i <= n; i++){
		dp[i] = 1e9;
		for(int j = 1; j <= i; j++){
			if(pref[i] - pref[j - 1] >= 0){
				dp[i] = min(dp[i], dp[j - 1] + (i - j) * (b[i] - b[j - 1] > 0));
			}
		}
	}
	cout << dp[n];
}