#include <bits/stdc++.h> using namespace std; long long result = 0LL; void rec(int l, int r, const int &k, const vector<long long> &a, vector<long long> &ps, const long long res) { if((r - l + 1) < k) { return; } long long s = 0; for(int i = l; i < l + k; i++) { s += a[i]; } result = max(result, res + s); rec(l + k, r, k, a, ps, res + s); for(int i = l + 1; i <= r; i++) { if((r - i + 1) >= k) { rec(i, r, k, a, ps, res); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, q; cin >> n >> k >> q; vector<long long> a(n + 1); vector<long long> ps(n + 1); for(int i = 1; i <= n; i++) { cin >> a[i]; ps[i] = ps[i - 1] + a[i]; } while(q--) { int l, r; cin >> l >> r; result = 0; rec(l, r, k, a, ps, 0LL); cout << result << "\n"; } 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 | #include <bits/stdc++.h> using namespace std; long long result = 0LL; void rec(int l, int r, const int &k, const vector<long long> &a, vector<long long> &ps, const long long res) { if((r - l + 1) < k) { return; } long long s = 0; for(int i = l; i < l + k; i++) { s += a[i]; } result = max(result, res + s); rec(l + k, r, k, a, ps, res + s); for(int i = l + 1; i <= r; i++) { if((r - i + 1) >= k) { rec(i, r, k, a, ps, res); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, q; cin >> n >> k >> q; vector<long long> a(n + 1); vector<long long> ps(n + 1); for(int i = 1; i <= n; i++) { cin >> a[i]; ps[i] = ps[i - 1] + a[i]; } while(q--) { int l, r; cin >> l >> r; result = 0; rec(l, r, k, a, ps, 0LL); cout << result << "\n"; } return 0; } |