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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, t[500005], res;
ll sum;
int32_t main() {
	ios_base::sync_with_stdio(0);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> t[i];
		sum += t[i];
	}
	if(sum < 0) {
		cout << "-1\n";
		return 0;
	}
	//ll istot = 0;
	ll DELTA = 0;
	set<pair<ll, int>> aktywne;
	aktywne.insert({0, 0});
	for(int i = 1; i <= n; i++) {
		auto it = aktywne.lower_bound({0 - DELTA, -1e9});
		DELTA += t[i];
		if(it == aktywne.end()) continue;
		else {
			pair<ll, int> key = {t[i] - DELTA, it->second - 1};
			aktywne.insert(key);
			auto it2 = aktywne.lower_bound(key);
			if(it2 == aktywne.begin()) continue;
			it2--;
			while(-it2->second < -key.second) {
				aktywne.erase(it2);
				it2 = aktywne.lower_bound(key);
				if(it2 == aktywne.begin()) break;
				it2--;
			}

		}
	}

	for(auto i : aktywne) {
		if(i.first + DELTA >= 0) res = max(res, -i.second);
	}
	//assert(istot <= n * 10);
	cout << n - res << "\n";
}