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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
int pot;
int tree[1100010];
vector<pair<long long,int>> t;
inline void update(int x,int c)
{
	x+=pot-1;
	tree[x]=c;
	for(x/=2;x>0;x/=2)
		tree[x]=max(tree[2*x],tree[2*x+1]);
	return;
}
inline int read(int l,int r)
{
	int ans=0;
	for(l+=pot-1,r+=pot-1;l<=r;l/=2,r/=2)
	{
		if(l%2==1)
		{
			ans=max(ans,tree[l]);
			l++;
		}
		if(r%2==0)
		{
			ans=max(ans,tree[r]);
			r--;
		}
	}
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,i;
	long long p=0,x;
	cin>>n;
	for(pot=1;pot<n;pot*=2);
	t.resize(n);
	for(i=1;i<=n;i++)
	{
		cin>>x;
		p+=x;
		t[i-1]={p,i};
	}
	if(p<0)
	{
		cout<<"-1\n";
		return 0;
	}
	sort(t.begin(),t.end());
	for(auto v:t)
	{
		if(v.fi>=0)
			update(v.se,read(1,v.se)+1);
	}
	cout<<n-tree[n+pot-1]<<"\n";
	return 0;
}