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

int n, res = INT_MAX;
long long req;
long long inp[500005];
bitset <32> B;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> inp[i];
		req += inp[i];
	}
	
	if (req < 0)
	{
		cout << "-1\n";
		return 0;
	}
	
	for (int i = 0; i < (1<<(n-1)); i++)
	{
		bool p = true;
		long long x = 0;
		B = i;
		
		for (int j = 0; j < n-1; j++)
		{
			if (B[j] || inp[j] < 0)
				x = inp[j];
			while (B[j] && j < n-1)
			{
				x += inp[j+1];
				j++;
			}
			
			if (x < 0)
			{
				p = false;
				break;
			}
		}
		
		if (!B[n-2] && inp[n-1] < 0)
			p = false;
		
		if (p)
			res = min(res, (int) B.count());
	}
	
	cout << res << '\n';
}