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
#include <bits/stdc++.h>
#define int long long
#define pi pair<int, int>

using namespace std;

// segment tree from my team's (me, Mikołaj Kołek, Mikołaj Zabłocki) AMPPZ/UZI
// library.
// Andrzej Pezarski teaches this way to write segment trees, so there's
// a chance many other ppl from UJ/V LO have similar structure
struct Value {
  int v = 0;
};

Value operator+(const Value &lhs, const Value &rhs) { return {lhs.v + rhs.v}; }

Value operator*(const Value &lhs, const Value &rhs) { return {lhs.v + rhs.v}; }

struct Tree {
  int L = 1;
  vector<Value> T;

  Tree(const int n) {
    while (L <= n)
      L *= 2;
    T = vector<Value>(2 * L);
  }

  Tree(const vector<int> &V) {
    while (L <= V.size())
      L *= 2;
    T = vector<Value>(2 * L);
    for (int i = 0; i < V.size(); i++)
      T[i + L] = {V[i]};
  }

  void update(int p, int q, const Value &x) {
    if ((p += L) >= (q += L))
      return;
    T[p] = T[p] + x;
    while (p / 2 != q / 2) {
      if (p % 2 == 0)
        T[p + 1] = T[p + 1] + x;
      if (q % 2 == 1)
        T[q - 1] = T[q - 1] + x;
      p /= 2, q /= 2;
    }
  }

  Value query(int i) {
    i += L;
    auto res = T[i];
    while (i /= 2)
      res = res * T[i];
    return res;
  }
};

signed main() {
  //ios_base::sync_with_stdio(0);
//  cin.tie(0);
  int n;
  cin >> n;
  vector<int> A(n);
  int sum = 0;
  for (auto &a : A) {
    cin >> a;
    sum += a;
  }


  vector<int> divisors;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      if (i <= n)
        divisors.push_back(i);
      if (sum / i <= n)
        divisors.push_back(sum / i);
    }
  }

  sort(divisors.begin(), divisors.end());
  vector<int> primes;
  vector<pi> number_and_div;
  vector<int> work(n+1);
  for(auto d : divisors){
    if(d==1) continue;
    [&](){
      for(auto p : primes){
        if(p>d) break;
        if(d%p == 0){
          number_and_div.push_back({d, p});
          return;
        }
      }
      number_and_div.push_back({d, d});
      primes.push_back(d);
    }();
  }

  Tree T(A);
  work[1] = 1;
  for (auto [l, p] : number_and_div) {
    [&](){
      if(l != p && (!work[p] || !work[l/p])){
        work[l] = 0;
        return;
      }
      //cerr << l << '\n';
      auto copy = T;
      bool wrk = true;
      for (int i = 0; i < n - l + 1; i++) {
        int val = copy.query(i).v;
        if(val == 0) 
          continue;
        if (val < 0)
         return;
        copy.update(i, i + l, {-val});
      }
      for (int i = n-l; i < n; i++)
        wrk &= (copy.query(i).v == 0);
      if (wrk) {
        work[l]=1;
      } else work[l]=0;
    }();
  }

    for(int i=n; i>0; i--)
      if(work[i]==1){
        cout << i << '\n';
        return 0;
      }
}