#include<bits/stdc++.h> using namespace std; typedef long long ll; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n,k,q; cin >> n >> k >> q; vector<ll> tab(n), ps(n+1); for(int i=0;i<n;i++) { cin >> tab[i]; ps[i+1] = ps[i] + tab[i]; if(i+1-k < 0) tab[i] = 0; else tab[i] = ps[i+1] - ps[i+1-k]; } while(q--) { int L,R; cin >> L >> R; L--; R--; ll mx = 0; vector<ll> dp(R-L+2); for(int j=L+k-1, s=k; j<=R; j++,s++) { mx = max(mx, dp[s-k]); dp[s] = max(dp[s-1], mx + tab[j]); } cout<<dp.back()<<"\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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n,k,q; cin >> n >> k >> q; vector<ll> tab(n), ps(n+1); for(int i=0;i<n;i++) { cin >> tab[i]; ps[i+1] = ps[i] + tab[i]; if(i+1-k < 0) tab[i] = 0; else tab[i] = ps[i+1] - ps[i+1-k]; } while(q--) { int L,R; cin >> L >> R; L--; R--; ll mx = 0; vector<ll> dp(R-L+2); for(int j=L+k-1, s=k; j<=R; j++,s++) { mx = max(mx, dp[s-k]); dp[s] = max(dp[s-1], mx + tab[j]); } cout<<dp.back()<<"\n"; } } |