#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O2") #define ll long long #define pii pair<int, int> int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n, k, q; cin>>n>>k>>q; ll pref[n+1]; pref[0] = 0; ll a[n+1]; for (int i=1; i<=n; ++i) cin>>a[i]; for (int i=1; i<=n; ++i) pref[i] = pref[i-1]+a[i]; ll ans[q]; vector<pii> que[n+1]; for (int i=0; i<q; ++i) { int l, r; cin>>l>>r; que[l].push_back({r, i}); } for (int i=1; i<=n; ++i) { ll dp[n+1]; dp[i-1] = 0; for (int j=i; j<=n; ++j) { dp[j] = dp[j-1]; if (j-k >= i-1) { dp[j] = max(dp[j-k]+(pref[j]-pref[j-k]), dp[j]); } } int x=0; for (int x=0; x<(int)que[i].size(); ++x) { ans[que[i][x].second] = dp[que[i][x].first]; } } for (int i=0; i<q; ++i) { cout<<ans[i]<<endl; } }
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 | #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O2") #define ll long long #define pii pair<int, int> int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n, k, q; cin>>n>>k>>q; ll pref[n+1]; pref[0] = 0; ll a[n+1]; for (int i=1; i<=n; ++i) cin>>a[i]; for (int i=1; i<=n; ++i) pref[i] = pref[i-1]+a[i]; ll ans[q]; vector<pii> que[n+1]; for (int i=0; i<q; ++i) { int l, r; cin>>l>>r; que[l].push_back({r, i}); } for (int i=1; i<=n; ++i) { ll dp[n+1]; dp[i-1] = 0; for (int j=i; j<=n; ++j) { dp[j] = dp[j-1]; if (j-k >= i-1) { dp[j] = max(dp[j-k]+(pref[j]-pref[j-k]), dp[j]); } } int x=0; for (int x=0; x<(int)que[i].size(); ++x) { ans[que[i][x].second] = dp[que[i][x].first]; } } for (int i=0; i<q; ++i) { cout<<ans[i]<<endl; } } |