/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
bool sprawdz(int k, int n2){
vector<long long> diff(n2 + 2, 0);
long long cur = 0;
for(int i = 1; i <= n2; i++){
cur += diff[i];
if(cur > a[i]){
return false;
}
long long po = 1LL * a[i] - cur;
if(po > 0){
if(i + k - 1 > n2){
return false;
}
cur += po;
diff[i + k] -= po;
}
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
a.resize(n + 1);
long long s = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
s += a[i];
}
vector<long long> dzielniki;
for(long long d = 1; d * d <= s; d++){
if(s % d == 0){
dzielniki.push_back(d);
if(d * d != s){
dzielniki.push_back(s / d);
}
}
}
sort(dzielniki.rbegin(), dzielniki.rend());
for(long long k : dzielniki){
if(k > n){
continue;
}
if(sprawdz(k, n)){
cout << k << endl;
return 0;
}
}
return 0;
}
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 | /****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; vector<int> a; bool sprawdz(int k, int n2){ vector<long long> diff(n2 + 2, 0); long long cur = 0; for(int i = 1; i <= n2; i++){ cur += diff[i]; if(cur > a[i]){ return false; } long long po = 1LL * a[i] - cur; if(po > 0){ if(i + k - 1 > n2){ return false; } cur += po; diff[i + k] -= po; } } return true; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; a.resize(n + 1); long long s = 0; for(int i = 1; i <= n; i++){ cin >> a[i]; s += a[i]; } vector<long long> dzielniki; for(long long d = 1; d * d <= s; d++){ if(s % d == 0){ dzielniki.push_back(d); if(d * d != s){ dzielniki.push_back(s / d); } } } sort(dzielniki.rbegin(), dzielniki.rend()); for(long long k : dzielniki){ if(k > n){ continue; } if(sprawdz(k, n)){ cout << k << endl; return 0; } } return 0; } |
English