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

int main() {
    int n; cin >> n;
    vector<int> a(n);
    for (int &x: a) cin >> x;

    auto check = [&](i64 d) {
        if (d > n) return false;
        int active = 0;
        vector<int> sub(n+1);
        for (int i = 0; i <= n; i++) {
            active -= sub[i];
            if (active < 0) return false;
            if (i == n) break;

            int diff = a[i] - active;
            if (diff < 0) return false;
            if (diff > 0) {
                if (i+d > n) return false;
                sub[i+d] += diff;
                active += diff;
            }
        }
        return true;
    };

    i64 s = accumulate(begin(a), end(a), 0LL);
    i64 res = 1;
    for (i64 d = 1; d*d <= s; d++) if (s%d == 0) {
        if (check(d)) res = max(res, d);
        if (d < s/d && check(s/d)) res = max(res, s/d);
    }
    cout << res << '\n';
}