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
//Kazda fala wnosi k burszytnow, wiec k musi dzielic ich sume

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
typedef long long LL;
typedef vector<LL> VI;

bool check(const VI& a, int n, int k)
{
	VI delta(n + 2, 0);
	LL cur = 0;
	int lastStart = n - k + 1;

	for (int i = 1; i <= n; ++i)
	{
		cur += delta[i];
		LL needed = a[i] - cur;

		if (i <= lastStart)
		{
			if (needed < 0) return false;
			cur += needed;
			delta[i + k] -= needed;
		}
		else if (needed != 0) return false;
	}
	return true;
}

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

	int n; cin >> n;
	VI a(n + 1);
	for (int i = 1; i <= n; ++i) cin >> a[i];
	LL S = accumulate(a.begin() + 1, a.end(), 0LL);

	VI divisors;
	for (LL d = 1; d * d <= S; ++d)
	{
		if (S % d != 0) continue;
		if (d <= n) divisors.push_back(d);
		LL other = S / d;
		if (other != d && other <= n) divisors.push_back(other);
	}
	sort(divisors.begin(), divisors.end(), greater<int>());
	for (int k : divisors) { if (check(a, n, k)) { cout << k << endl; break; }}

	return 0;
}