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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <bits/stdc++.h>
using namespace std;

vector<pair<long long, int>> factorize(long long n) {
	vector<pair<long long, int>> factors;

	if (n % 2 == 0) {
		int cnt = 0;
		while (n % 2 == 0) {
			n /= 2;
			cnt++;
		}
		factors.push_back( { 2, cnt });
	}

	for (long long p = 3; p * p <= n; p += 2) {
		if (n % p == 0) {
			int cnt = 0;
			while (n % p == 0) {
				n /= p;
				cnt++;
			}
			factors.push_back( { p, cnt });
		}
	}

	if (n > 1)
		factors.push_back( { n, 1 });

	return factors;
}

// Pruned generation
void genDivsLimited(int i, long long current,
		const vector<pair<long long, int>> &factors,
		vector<long long> &divisors, long long maxVal) {

	if (current > maxVal)
		return;

	if (i == (int) factors.size()) {
		divisors.push_back(current);
		return;
	}

	long long p = factors[i].first;
	int exp = factors[i].second;

	for (int e = 0; e <= exp; ++e) {
		genDivsLimited(i + 1, current, factors, divisors, maxVal);

		// prevent overflow + useless work
		if (current > maxVal / p)
			break;
		current *= p;
	}
}

// Main function
vector<long long> getDivisors(long long n, long long maxVal) {
	vector<pair<long long, int>> factors = factorize(n);

	vector<long long> divisors;
	genDivsLimited(0, 1, factors, divisors, maxVal);

	// sort descending
	sort(divisors.begin(), divisors.end(), greater<long long>());

	return divisors;
}

bool checkIfMatching1(const long long int hLength,
		vector<long long int> amounts, const int start,
		long long int totalSum) {

	const int S = amounts.size();
	int firstNonZero = start;
	while (true) {
		if (firstNonZero + hLength > S) {
			return false;
		}
		long long int h = amounts[firstNonZero];
		for (int i = firstNonZero; i < firstNonZero + hLength; i++) {
			amounts[i] -= h;
			if (amounts[i] < 0) {
				return false;
			}
			totalSum -= h;
		}

		for (; firstNonZero < S; firstNonZero++) {
			if (amounts[firstNonZero] > 0) {
				break;
			}
		}

		if (firstNonZero >= S) {
			return totalSum == 0;
		}
	}
	return totalSum == 0;
}
bool checkIfMatching(const long long int hLength, vector<long long int> amounts,
		const int start, long long int totalSum) {

	const int S = amounts.size();
	int firstNonZero = start;
	deque<long long int> stairs;
	while (true) {
		if (firstNonZero + hLength > S) {
			return false;
		}
		stairs.clear();
		stairs.push_back(0);
		long long int h = amounts[firstNonZero];
		int i = firstNonZero;
		const int maxIExc = firstNonZero + hLength;
		for (; i < maxIExc; i++) {
			if (amounts[i] >= stairs.back()) {
				h = amounts[i];
				stairs.push_back(h);

				amounts[i] -= h;
				if (amounts[i] < 0) {
					return false;
				}
				totalSum -= h;
			} else {
				return false;
			}
		}
		stairs.pop_front();
		while (stairs.back() == h) {
			stairs.pop_back();
		}
		int STEPS = stairs.size();
		if ((i + (int) STEPS) > S) {
			return false;
		}

		for (int s = 0; s < STEPS; s++, i++) {
			long long int d = (h - stairs[s]);
			amounts[i] -= d;
			if (amounts[i] < 0) {
				return false;
			}
			totalSum -= d;
		}

		firstNonZero += hLength;
		for (; firstNonZero < S; firstNonZero++) {
			if (amounts[firstNonZero] > 0) {
				break;
			}
		}

		if (firstNonZero >= S) {
			return totalSum == 0;
		}
	}
	return totalSum == 0;
}
int findMaxWaveWidth(const vector<long long int> &numbers) {
	int maxWidth = 0;
	int currentWidth = 0;

	for (long long int num : numbers) {
		if (num != 0) {
			currentWidth++;              // extend current wave
			if (currentWidth > maxWidth) {
				maxWidth = currentWidth; // update max
			}
		} else {
			currentWidth = 0;            // reset on zero
		}
	}

	return maxWidth;
}
int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<long long int> amounts(n);
	long long int a;
	long long int totalSum = 0;

	for (int i = 0; i < n; i++) {
		cin >> a;
		totalSum += a;
		amounts[i] = a;
	}
	int start_ = -1;
	for (int i = 0; i < n; i++) {
		if (amounts[i] > 0) {
			start_ = i;
			break;
		}
	}
	const int start = start_;

	const int maxVal = findMaxWaveWidth(amounts);
	vector<long long int> divisors = getDivisors(totalSum, maxVal);
	for (unsigned int i = 0; i < divisors.size(); i++) {
		const long long int divisor = divisors[i];
		const bool match = checkIfMatching(divisor, amounts, start, totalSum);
		if (match) {
			cout << divisors[i] << endl;
			break;
		}
	}
}