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

using namespace std;


vector<long long> divisors(long long n) {
    vector<long long> divs;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            divs.push_back(i);
            if (i * i != n) {
                divs.push_back(n/i);
            }
        }
    }
    return divs;
}

bool check_waves(vector<long long> ambers, long long wave_size) {
    vector<long long> counter(ambers.size() + 1, 0);
    long long amber_counter = 0;
    for (long long i = 0; i < ambers.size(); i++) {
        long long curr_counter = amber_counter + counter[i];
        if (curr_counter > ambers[i]) {
            return false;
        }
        if (curr_counter == ambers[i]) {
            amber_counter = curr_counter;
            continue;
        }
        if (i + wave_size > ambers.size()) {
            return false;
        }
        long long diff = ambers[i] - curr_counter;
        counter[i] += diff;
        counter[i + wave_size] -= diff;
        amber_counter = ambers[i];
    }
    return true;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<long long> ambers(n);
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> ambers[i];
        sum += ambers[i];
    }

    vector<long long> divs = divisors(sum);
    sort(divs.begin(), divs.end());
    // cout << "divs: " << divs.size() << endl;
    // for (int i = 0; i < divs.size(); i++) {
    //     cout << divs[i] << " ";
    // }
    // cout << endl;
        
    vector<long long> not_possible_waves;
    long long longest_wave = 0;
    for (long long d : divs) {
        bool possible = true;
        for (long long npw : not_possible_waves) {
            if (d % npw == 0) {
                possible = false;
                break;
            }
        }
        if (!possible) {
            continue;
        }
        if (check_waves(ambers, d)) {
            longest_wave = d;
        } else {
            not_possible_waves.push_back(d);
        }
    }
    cout << longest_wave << endl;

    return 0;
}