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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <bits/stdc++.h>

using namespace std;

long long n;
long long a[600000];
long long DP[600000];
long long pref;

const long long M = 100000000000000000;

set < pair < long long, long long > > S;
set < pair < long long, long long > > S_min;
set < pair < long long, long long > > S_min_rev;
set < pair < long long, long long > > S_rev;

void ins(long long x, long long y){
	//cout << "insert " << x << " " << y << endl;
	S.insert(make_pair(x, y));
	S_min.insert(make_pair(-x, -y));
	S_min_rev.insert(make_pair(-y, -x));
	S_rev.insert(make_pair(y, x));
}

void er(long long x, long long y){
	//cout << "erase " << x << " " << y << endl;
	S.erase(S.find(make_pair(x, y)));
	S_min.erase(S_min.find(make_pair(-x, -y)));
	S_rev.erase(S_rev.find(make_pair(y, x)));
	S_min_rev.erase(S_min_rev.find(make_pair(-y, -x)));
}

void insert(long long x, long long y){
	// najpierw ogarniam ten sam x
	if(S.lower_bound(make_pair(x, y)) != S.end()){
		pair < long long, long long > p = *(S.lower_bound(make_pair(x, y)));
		if(p.first == x){
			if(p.second >= y){
				return;
			}
			else{
				er(p.first, p.second);
			}
		}
	}
	//cout << "A" << endl;
	if(S_min.lower_bound(make_pair(-x, -y)) != S_min.end()){
		pair < long long, long long > p = *(S_min.lower_bound(make_pair(-x, -y)));
		if(-p.first == x){
			if(-p.second >= y){
				return;
			}
			else{
				er(-p.first, -p.second);
			}
		}
	}

	// Najpierw 1 mniejszy niż (x, y)
	if(S_min.lower_bound(make_pair(-x, -y)) != S_min.end()){
		pair < long long, long long > p = *(S_min.lower_bound(make_pair(-x, -y)));
		p.first *= -1;
		p.second *= -1;
		if(p.second >= y){
			//cout << "C";
			return;
		}
	}

	// Lecimy po większych niż rozpatrywana para
	while(S.upper_bound(make_pair(x, y)) != S.end()){
		pair < long long, long long > p = *(S.upper_bound(make_pair(x, y)));
		if(p.second <= y){
			er(p.first, p.second);
		}
		else{
			break;
		}
	}

	ins(x, y);
}

long long bound(long long x){
	if(S_rev.lower_bound(make_pair(x, -M)) == S_rev.end()){
		return -M;
	}

	pair < long long, long long > p = *(S_rev.lower_bound(make_pair(x, -M)));
	return p.second;
}

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(false);

	/*while(true){
		char c;
		cin >> c;
		if(c == 'i'){
			int x, y;
			cin >> x >> y;
			insert(x, y);
		}
		if(c == 'b'){
			int x;
			cin >> x;
			cout << bound(x) << endl;
		}
		if(c == 'q'){
			return 0;
		}
	}*/

	//return 0;

	
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}

	// liczymy rekurencyjnie
	for (int i = 0; i < n; ++i)
	{
		DP[i] = M;
		pref += a[i];
		if(pref >= 0){
			DP[i] = i;
		}
		//cout << "i = " << i << "; bound = " << bound(-pref) << "; -pref = " << - pref << " " << endl; 
		long long l = bound(-pref);
		if(l != -M){
			DP[i] = l + i - 1;
			//cout << "DP[" << i << "] = " << DP[i] << endl;
		}
		if(DP[i] != M){
			insert(DP[i] - i, -pref);
		}
		//cout << "DP[" << i << "] = " << DP[i] << endl;
	}

	if(DP[n - 1] == M){
		cout << -1 << endl;
	}
	else{
		cout << DP[n - 1] << endl;
	}

}