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
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

int n;
long long prefix[500005];
vector<long long> poss;
vector<long long> LIS[500005];

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		long long x;
		scanf("%lld", &x);
		prefix[i] = prefix[i-1] + x;
	}
	if(prefix[n] < 0){
		printf("-1");
		return 0;
	}
	for(int i = 1; i <= n; i++){
		if(prefix[i] >= 0 and prefix[i] <= prefix[n]) poss.push_back(prefix[i]);
	}
//	for(int i = 0; i < poss.size(); i++){
//		printf("%lld ", poss[i]);
//	}
	int largest = 0;
	for(int i = 0; i < poss.size(); i++){
		int L = 0;
		int R = largest + 1;
		while(R - L != 1){
			int sr = (R + L)/2;
			if(poss[i] >= LIS[sr][LIS[sr].size() - 1]) L = sr;
			else R = sr;
		}
		LIS[R].push_back(poss[i]);
		if(R == largest + 1) largest++;
	}
	printf("%d", n - largest);
	return 0;
}