#include <iostream> using namespace std; int main() { int n,k,q; cin>>n>>k>>q; long long a[300000],s[300000],dp[300000]; cin>>a[0]; s[0] = a[0]; for(int i=1; i<n; ++i) { cin>>a[i]; s[i] = s[i-1] + a[i]; } for(int i=0; i<q; ++i) { int l,r; cin>>l>>r; l--; r--; if(k == 1) dp[l] = max(a[l],(long long)0); else dp[l] = 0; for(int j=l+1; j<=r; ++j) { if(j-l+1 > k) dp[j] = max(dp[j-1], s[j] - s[j-k] + dp[j-k]); else if(j-l+1 == k) dp[j] = max(dp[j-1], s[j] - s[l-1]); else dp[j] = 0; } cout<<dp[r]<<endl; } 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 | #include <iostream> using namespace std; int main() { int n,k,q; cin>>n>>k>>q; long long a[300000],s[300000],dp[300000]; cin>>a[0]; s[0] = a[0]; for(int i=1; i<n; ++i) { cin>>a[i]; s[i] = s[i-1] + a[i]; } for(int i=0; i<q; ++i) { int l,r; cin>>l>>r; l--; r--; if(k == 1) dp[l] = max(a[l],(long long)0); else dp[l] = 0; for(int j=l+1; j<=r; ++j) { if(j-l+1 > k) dp[j] = max(dp[j-1], s[j] - s[j-k] + dp[j-k]); else if(j-l+1 == k) dp[j] = max(dp[j-1], s[j] - s[l-1]); else dp[j] = 0; } cout<<dp[r]<<endl; } return 0; } |