#include <bits/stdc++.h> using namespace std; constexpr int maxN = 3e5+7; int n,k,q,t[maxN]; long long prefix[maxN],seg[maxN],dp[maxN]; int solve(int l, int r) { if(r-l+1 < k) return 0; /// l-1 = dp[0] for(int i=l;i<=r-k+1;i++) { dp[i] = dp[max(l-1, i-k)] + seg[i]; dp[i] = max(dp[i-1],dp[i]); } return dp[r-k+1]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>k>>q; for(int i=1;i<=n;i++) { cin>>t[i]; prefix[i] = prefix[i-1]+t[i]; } for(int i=1;i<=n-k+1;i++) seg[i] = prefix[i+k-1] - prefix[i-1]; while(q--) { int l,r; cin>>l>>r; cout<<solve(l,r)<<"\n"; for(int i=l;i<=r-k+1;i++) dp[i] = 0; } } /* 8 3 7 3 -1 10 0 10 -1 1 -1 1 8 3 5 6 8 1 2 1 7 2 8 1 6 */
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 | #include <bits/stdc++.h> using namespace std; constexpr int maxN = 3e5+7; int n,k,q,t[maxN]; long long prefix[maxN],seg[maxN],dp[maxN]; int solve(int l, int r) { if(r-l+1 < k) return 0; /// l-1 = dp[0] for(int i=l;i<=r-k+1;i++) { dp[i] = dp[max(l-1, i-k)] + seg[i]; dp[i] = max(dp[i-1],dp[i]); } return dp[r-k+1]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>k>>q; for(int i=1;i<=n;i++) { cin>>t[i]; prefix[i] = prefix[i-1]+t[i]; } for(int i=1;i<=n-k+1;i++) seg[i] = prefix[i+k-1] - prefix[i-1]; while(q--) { int l,r; cin>>l>>r; cout<<solve(l,r)<<"\n"; for(int i=l;i<=r-k+1;i++) dp[i] = 0; } } /* 8 3 7 3 -1 10 0 10 -1 1 -1 1 8 3 5 6 8 1 2 1 7 2 8 1 6 */ |