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

using namespace std;

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(0);

	int n;
	cin >> n;

	vector<int64_t> data(n);
	int64_t sum{};

	for (auto& e : data)
	{
		cin >> e;
		sum += e;
	}

	//ograniczenia
	int64_t maxi = n;

	for (int i = n - 2; i >= 0; --i)
	{
		if (data[i] < data[i + 1])
		{
			maxi = n - i - 1;
			break;
		}
	}

	for (int i = 0; i < n - 1; ++i)
	{
		if (data[i] > data[i + 1])
		{
			maxi = min(maxi, (int64_t)i + 1);
			break;
		}
	}

	vector<int64_t> diff(n + 1);
	int64_t curr{};

	//dzielniki
	for (int i = maxi; i >= 1; --i)
	{
		if (sum % i != 0) continue;

		//dlugosc i
		curr = 0;
		bool isWin = true;

		for (int j = 0; j < n; ++j)
		{
			curr += diff[j];
			diff[j] = 0;
			if (!isWin) continue;

			if (curr < data[j])
			{
				if (j + i <= n)
				{
					auto new_f = data[j] - curr;
					diff[j + i] -= new_f;
					curr += new_f;
				}
				else
				{
					isWin = false;
				}
			}
			else if (curr > data[j]) isWin = false;
		}

		diff[n] = 0;

		if (isWin)
		{
			cout << i;
			return 0;
		}
	}
}