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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <bits/stdc++.h>
 
#define f first
#define s second
#define pb push_back
#define mp make_pair
 
#define ins insert
#define er erase
 
#define siz size()
#define siz1 size()-1
 
#define ll long long
#define ull unsigned long long
#define all(v) v.begin(), v.end()
 
#define FORN(i, n) for(int i=1; i<=(int)n; i++)
#define FOR(i,p,k) for(int i=(int)p; i<=(int)k; i++)
#define FORD(i, k, p) for(int i=(int)k; i>=(int)p; i--)
#define FOR0(i, n) for(int i=(int)n; i>=0; i--)
#define FOR1(i, n) for(int i=(int)n; i>=1; i--)
#define FORV(i, vec) for(auto i = vec.begin(); i != vec.end(); ++i)

#define TURBO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define END cerr<<"TIME: "<<1.0 * clock() / CLOCKS_PER_SEC<<endl;
#define VI vector <int>
#define VP vector <pair<int, int>>
#define VIP vector <pair<int, pair<int, int>>>
#define VPP vector <pair<pair<int, int>, pair<int, int>>>
 
#define MII map<int, int>
#define MLL map<ll, ll>
 
#define PII pair<int, int>
#define PLL pair<ll, ll>
 
#define MAX 1e9 + 10
#define MIN -1e9 + 10

using namespace std;

const int TREE_SIZE = 1050000 + 5;

ll bt[TREE_SIZE][2]; 
int zakresy[TREE_SIZE][2];

int max_pot = 1;
int n;

VP non_zero_points;

void DROP_VALUES(int w) 
{
	bt[w*2][0] += bt[w][0];
	bt[w*2][1] += bt[w][0];

	bt[w*2 + 1][0] += bt[w][0];
	bt[w*2 + 1][1] += bt[w][0];
	
	bt[w][0] = 0LL;
}

int FIND_MAX_NON_NEGATIVE_IDX(int w, int L, int R)
{
	if(zakresy[w][0] > R || zakresy[w][1] < L)
		return -1;

	if (bt[w][1] < 0)
		return -1;

	if (w  >= max_pot)
		return w-max_pot;

	DROP_VALUES(w);
	
	int right_son_result = FIND_MAX_NON_NEGATIVE_IDX(w*2+1, L, R);
	if (right_son_result != -1) 
		return right_son_result;
	
	return FIND_MAX_NON_NEGATIVE_IDX(w*2, L, R);
}

ll GET_VALUE(int LR, int w)
{
    if(zakresy[w][0] == LR && zakresy[w][1] == LR) 
		return  bt[w][1];

	DROP_VALUES(w);

	if (zakresy[w*2][0] <= LR && LR <= zakresy[w*2][1]) 
		return GET_VALUE(LR, w*2);

	return GET_VALUE(LR, w*2+1);
}

int main()
{
	TURBO;

	cin>>n;
	ll energy;

	ll sum = 0;
	FORN(i, n)
	{
		cin>>energy;
		sum += energy;
		if (energy != 0)
			non_zero_points.pb({i, energy}); 
	}

	if (sum < 0)
	{
		cout<<-1;
		return 0;
	}

	if (non_zero_points.size() == 0)
	{
		cout<<0;
		return 0;
	}

	while(max_pot < n)
        max_pot *= 2;

	FOR(i, max_pot, 2*max_pot-1)
    {
		zakresy[i][0] = zakresy[i][1] = i-max_pot;
		bt[i][1] = LLONG_MIN / 2;
	}

 	bt[max_pot][1] = 0;

    FORD(i, max_pot-1, 1)
    {
        zakresy[i][0] = zakresy[2*i][0];
        zakresy[i][1] = zakresy[2*i+1][1];
		bt[i][1] = max(bt[i*2][1],bt[i*2+1][1]);
    }

	int new_cost = non_zero_points[0].f - 1;
	ll energy_to_add;

	for(int i=0; i<non_zero_points.size(); i++)
	{
		if (i != 0)
			new_cost = non_zero_points[i].f - non_zero_points[i-1].f;

		energy_to_add = non_zero_points[i].s;

		if (bt[1][1] < 0LL)
		{
			bt[1][0] += energy_to_add;
			bt[1][1] += energy_to_add;
		}

		else
		{
			int K1 = FIND_MAX_NON_NEGATIVE_IDX(1, 0, n-1); ///na pewno jest ok

			bt[1][0] += energy_to_add;
			bt[1][1] += energy_to_add;
			
			ll K1_ENERGY = GET_VALUE(K1 + new_cost, 1); // przy okazji mam gwarancje, że nie ma żadnego niezerowego bt[w][0] po drodze od 1 do K1
			
			if (energy_to_add > K1_ENERGY)
			{
				int w = max_pot + K1 + new_cost;
				
				bt[w][1] = energy_to_add;
				w /= 2;

				while (w > 0)
				{
					bt[w][1] = max(bt[w*2][1], bt[w*2+1][1]);
					w /= 2;
				}
			}
		}
	}
	
	int trailing_zeros_num = n - non_zero_points[non_zero_points.size() - 1].f;
	cout<<n-1-FIND_MAX_NON_NEGATIVE_IDX(1, 0, n-1) - trailing_zeros_num;

	return 0;
}