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
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <time.h>
#include <queue>

using namespace std;

int main () {
	int n;
	scanf("%d", &n);
	vector<int64_t> ambers(n, 0);
	int64_t total = 0;
	for (int i = 0; i < n; ++i) {
		scanf("%lld", &ambers[i]);
		total += ambers[i];
	}
	set<int64_t> divisors;
	int64_t i = 1;
	while (i * i <= total) {
		if (total % i == 0) {
			divisors.insert(i);
			divisors.insert(total / i);
		}
		++i;
	}
	for (set<int64_t>::reverse_iterator it = divisors.rbegin(); it != divisors.rend(); it++) {
		int64_t wavelength = *it;
//		printf("WAVELENGTH: %lld\n", wavelength);
		if (wavelength > n) {
			continue;
		}
		int64_t waves = 0;
		vector<int64_t> decrease(n + 1, 0);
		bool ok = true;
		for (int i = 0; i < n; ++i) {
//			printf("PART %d END WAVES %d\n", i, decrease[i]);
			waves -= decrease[i];
			if (ambers[i] > waves) {
				if (i + wavelength > n) {
					ok = false;
					break;
				}
//				printf("START WAVES %d\n", ambers[i] - waves);
				decrease[i + wavelength] += ambers[i] - waves;
				waves += ambers[i] - waves;
			}
			if (ambers[i] < waves) {
				ok = false;
//				printf("BREAK 2\n");
				break;
			}
		}
		if (ok) {
			printf("%lld\n", wavelength);
			return 0;
		}
	}
}