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
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

vector<int> tab;
vector<int> add;

bool check(int d, int n){
    add.assign(n, 0);
    int acc = 0;
    for(int i = 0; i < n; i++){
        acc += add[i];
        if(tab[i] + acc < 0) return false;
        if(tab[i] + acc > 0 && i + d > n) return false;
        if(i + d < n) add[i+d] += tab[i] + acc;
        acc -= tab[i] + acc;
    }
    return true;
}

int main(){
    cin.tie(0)->sync_with_stdio(0);

    int n;
    cin >> n;
    tab.resize(n);
    ll sum = 0;
    for(int i = 0; i < n; i++){
        cin >> tab[i];
        sum += tab[i];
    }

    for(int i = n; i >= 1; i--){
        if(sum % (ll)i != 0) continue;
        if(check(i, n)) {
            cout << i << '\n';
            return 0;
        }
    }

    return 0;
}