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
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, int> PILL;
typedef pair<ll, ll> PLL;

const int MAX_N = 5e5+5;
const int M = 1e6;
const ll INF = (ll)(1e18);
const int inf = 2e9;
const ll MOD = 1000000007LL;

int n;
ll a[MAX_N], pref[MAX_N];
vector<ll> com;
map<ll, int> prefToPos;
int dp[MAX_N][2];
bool canCut[MAX_N];

// Segment tree

int treeSize = 0;
int t[4*MAX_N];

void update(int v, int l, int r, int pos, int val) {
	if (l > pos || r < pos) return;
	if (l == pos && r == pos) {
		t[v] = max(t[v], val);
		return;
	}
	
	int mid = (l+r) / 2;
	update(2*v, l, mid, pos, val);
	update(2*v+1, mid+1, r, pos, val);
	
	t[v] = max(t[2*v], t[2*v+1]);
}

int query(int v, int l, int r, int qL, int qR) {
	if (qL <= l && r <= qR) return t[v];
	if (l > qR || r < qL) return 0;
	int mid = (l+r) / 2;
	
	return max(query(2*v, l, mid, qL, qR), query(2*v+1, mid+1, r, qL, qR));
}

void brute() {
	for (int i = 1; i <= n; i++) {
		pref[i] = pref[i-1] + a[i];
	}
	
	canCut[0] = true;
	
	for (int i = 1; i < n; i++) {
		if (pref[n] - pref[i] < 0) {
			dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
			dp[i][1] = 0LL;
			continue;
		}
		
		dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
		for (int j = i-1; j >= 0; j--) {
			
			if (!canCut[j] || pref[j] > pref[i]) continue;
			
			canCut[i] = true;
			dp[i][1] = max(dp[i][1], dp[j][1] + 1);
		}
	}
	
	int ans = 0;
	for (int i = 0; i < n; i++) {
		//cout << i << ": " << dp[i][0] << ' ' << dp[i][1] << '\n';
		ans = max(ans, dp[i][0]);
		ans = max(ans, dp[i][1]);
	}
	
	cout << n - 1 - ans << '\n';
	
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
	
	cin >> n;
	ll total = 0LL;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		total += a[i];
	}
	
	if (total < 0) {
		cout << "-1\n";
		return 0;
	}
	
	if (n <= 5000) {
		brute();
		return 0;
	}
	
	com.push_back(0LL);		// empty prefix
	for (int i = 1; i <= n; i++) {
		pref[i] = pref[i-1] + a[i];
		com.push_back(pref[i]);
	}
	
	// Recalculate prefix vals
	sort(com.begin(), com.end());
	auto it = unique(com.begin(), com.end());
	com.resize(distance(com.begin(), it));
	for (int i = 0; i < (int)com.size(); i++) {
		prefToPos[com[i]] = i+1;
	}
	treeSize = (int)com.size();
	
	/*for (auto it2: prefToPos) {
		cout << it2.first << ": " << it2.second << '\n';
	}
	cout << "\n\n";*/
	
	for (int i = 1; i < n; i++) {
		if (pref[n] - pref[i] < 0 || pref[i] < 0) {
			dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
			dp[i][1] = 0LL;
			continue;
		}
		
		dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
		
		int r = prefToPos[pref[i]];
		int bestPrv = query(1, 1, treeSize, 1, r);
		//cout << i << ": " << r << " " << bestPrv << '\n';
		
		dp[i][1] = bestPrv + 1;
		update(1, 1, treeSize, r, dp[i][1]);
	}
	
	//cout << "\n\n";
	int ans = 0;
	for (int i = 0; i < n; i++) {
		//cout << i << ": " << dp[i][0] << ' ' << dp[i][1] << '\n';
		ans = max(ans, dp[i][0]);
		ans = max(ans, dp[i][1]);
	}
	
	cout << n - 1 - ans << '\n';
	
	
    return 0;
}