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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct ps{
	int64_t p;
	int s;
	bool operator <(const struct ps& par) {
		if(s==par.s)
			return p>par.p;
		return s<par.s;
	}
};

int main(int argc, char* argv[]) {
	ios_base::sync_with_stdio (false);
	int n, i, p, s, mi, j, ign;
	vector<ps> m(500001);
	mi = 1;
	m[0].p = 0;
	m[0].s = 0;
	s = 0;
	cin >> n;
	for (i = 0; i < n; ++i) {
		cin >> p;
		s++;
		if (p == 0)
			continue;

		ign = -1;
		for (j = 0; j < mi; ++j) {
			if (ign==-1 && m[j].p >= 0) {
				ign=m[j].s;
			}
			m[j].p += p;
			m[j].s += s;
		}
		if (ign >= 0) {
			m[mi++] = ps{p, ign};
			sort(m.begin(), m.begin()+mi);
		}
		s = 0;
	}

	for (j = 0; j < mi; ++j) {
		if (m[j].p >= 0) {
			cout << m[j].s << "\n";
			return 0;
		}
	}
	cout << -1 << "\n";
}