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

struct Town {
	int dist;
	long long val;
	
	Town(int dist0, long long val0) {
		dist = dist0;
		val = val0;
	}
};

struct Candidate {
	int length;
	unsigned start;
	unsigned end;
	long long excess;
	
	Candidate(int length0, int start0, int end0, long long excess0) {
		length = length0;
		start = start0;
		end = end0;
		excess = excess0;
	}
};

bool byEnds(const Candidate& X, const Candidate& Y) {
	return X.end < Y.end || (X.end == Y.end && X.excess > Y.excess);
}

bool byStarts(const Candidate& X, const Candidate& Y) {
	return X.start < Y.start || (X.start == Y.start && X.excess < Y.excess);
}

vector<Town> towns;
vector<Candidate> candidates;
vector<Candidate> newCandidates;

bool isPossible() {
	long long sum = 0;
	for (unsigned i = 0; i < towns.size(); ++i) {
		sum += towns[i].val;
	}
	
	return sum >= 0;
}

long long dist(int posA, int posB) {
	return towns[posB].dist - towns[posA].dist;
}

void prepareNewCandidates(unsigned pos) {
	long long startSum = towns[pos].val;
	long long sum;
	
	unsigned start = pos;
	unsigned end = pos;
	unsigned start2;
	
	for (int i = pos - 1; i >= 0 && towns[i].val >= 0; --i) {
		startSum += towns[i].val;
		start = i;
	}
	
	while (true) {
		sum = startSum;
		start2 = start;
		
		if (sum >= 0) {
			while (sum - towns[start2].val >= 0 && start2 < pos) {
				sum -= towns[start2].val;
				++start2;
			}
			
			newCandidates.push_back(Candidate(dist(start2, end), start2, end, sum));
		}
		
		++end;
		if (end == towns.size()) {
			break;
		}
		startSum += towns[end].val;
	}
	
	sort(newCandidates.begin(), newCandidates.end(), byStarts);
	
	
	
	/*cout << "CANDIDATES " << pos << endl;
	for (int i = 0; i < candidates.size(); ++i) {
		cout << candidates[i].start << "-" << candidates[i].end << " " << candidates[i].length << " " << candidates[i].excess << endl;
	}
	cout << "NEW CANDIDATES " << pos << endl;
	for (int i = 0; i < newCandidates.size(); ++i) {
		cout << newCandidates[i].start << "-" << newCandidates[i].end << " " << newCandidates[i].length << " " << newCandidates[i].excess << endl;
	}*/
	
	
	
	if (!candidates.empty()) {
		unsigned currPos = 0;
		int shortestPos = 0;
		
		for (unsigned i = 0; i < newCandidates.size(); ++i) {
			while (candidates[currPos].end < newCandidates[i].start
					|| (candidates[currPos].end == newCandidates[i].start
						&& candidates[currPos].excess + newCandidates[i].excess >= towns[newCandidates[i].start].val)) {
				if (candidates[shortestPos].length > candidates[currPos].length) {
					shortestPos = currPos;
				}
				++currPos;
				if (currPos == candidates.size()) {
					break;
				}				
			}
			
			if (currPos <= 0) {
				newCandidates.erase(newCandidates.begin() + i);
				--i;
				continue;
			}
			--currPos;
//			cout << "qwe " << currPos << " " << i << " " << candidates[currPos].end << " " << newCandidates[i].start << " " << candidates[currPos].excess << " " << newCandidates[i].excess << " " << towns[newCandidates[i].start].val << " " << shortestPos << endl;
			if (candidates[currPos].end < newCandidates[i].start
						|| (candidates[currPos].end == newCandidates[i].start
								&& candidates[currPos].excess + newCandidates[i].excess >= towns[newCandidates[i].start].val)) {
				if (candidates[shortestPos].end == newCandidates[i].start) {
					newCandidates[i].excess = candidates[shortestPos].excess + newCandidates[i].excess - towns[newCandidates[i].start].val;
				}
				newCandidates[i].length += candidates[shortestPos].length;
				newCandidates[i].start = candidates[shortestPos].start;
			}
			
			++currPos;
		}
		
		for (unsigned i = 0; i < candidates.size(); ++i) {
			if (candidates[i].end >= pos) {
				newCandidates.push_back(candidates[i]);
			}
		}
	}
	
	swap(candidates, newCandidates);
	newCandidates.clear();
	sort(candidates.begin(), candidates.end(), byEnds);
}		

int main() {
	ios_base::sync_with_stdio(0);
	int n, a;
	
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a;
		if (a != 0) {
			towns.push_back(Town(i, a));
		}
	}
	
	if (!isPossible()) {
		cout << -1 << endl;
		return 0;
	}
	
	for (unsigned i = 0; i < towns.size(); ++i) {
		if (towns[i].val < 0) {
			prepareNewCandidates(i);
		}
	}
	
	int bestResult = 0;
	for (unsigned i = 0; i < candidates.size(); ++i) {
		if (bestResult == 0 || bestResult > candidates[i].length) {
			bestResult = candidates[i].length;
		}
	}
	
	
	cout << bestResult << endl;
	

	
	return 0;
}