#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
int n;
i64 sum = 0;
vector<int> arr;
bool check(int k) {
vector<int> deactivate(n + k + 1);
i64 active = 0;
for (int i = 0; i < n; i++) {
active -= deactivate[i];
if (arr[i] - active < 0) return false;
if (arr[i] - active > 0) {
if (i + k > n) return false;
deactivate[i + k] = arr[i] - active;
active += arr[i] - active;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
arr.resize(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
int ans = 0;
if (sum != 0) {
for (int k = n; k >= 1; k--) {
if (sum % k == 0 && check(k)) {
ans = k;
break;
}
}
}
cout << ans << "\n";
}
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 | #include <bits/stdc++.h> using namespace std; using i64 = int64_t; using u64 = uint64_t; int n; i64 sum = 0; vector<int> arr; bool check(int k) { vector<int> deactivate(n + k + 1); i64 active = 0; for (int i = 0; i < n; i++) { active -= deactivate[i]; if (arr[i] - active < 0) return false; if (arr[i] - active > 0) { if (i + k > n) return false; deactivate[i + k] = arr[i] - active; active += arr[i] - active; } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; arr.resize(n); for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } int ans = 0; if (sum != 0) { for (int k = n; k >= 1; k--) { if (sum % k == 0 && check(k)) { ans = k; break; } } } cout << ans << "\n"; } |
English