#include<bits/stdc++.h> using namespace std; int main(){ int n,k,q,i,j,l,r; long long sum; scanf("%d%d%d",&n,&k,&q); int arr[n]; long long dp[n+1]; for(i=0;i<n;i++) scanf("%d",&arr[i]); for(j=0;j<q;j++){ scanf("%d%d",&l,&r); l--; r--; dp[l]=0; sum=0; for(i=l;i<=r;i++){ sum+=arr[i]; if(i-k>=l) sum-=arr[i-k]; dp[i+1]=dp[i]; if(i-k+1>=l) dp[i+1]=max(dp[i+1],dp[i-k+1]+sum); //printf(" dp: %lld\n",dp[i+1]); } printf("%lld\n",dp[r+1]); } 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 | #include<bits/stdc++.h> using namespace std; int main(){ int n,k,q,i,j,l,r; long long sum; scanf("%d%d%d",&n,&k,&q); int arr[n]; long long dp[n+1]; for(i=0;i<n;i++) scanf("%d",&arr[i]); for(j=0;j<q;j++){ scanf("%d%d",&l,&r); l--; r--; dp[l]=0; sum=0; for(i=l;i<=r;i++){ sum+=arr[i]; if(i-k>=l) sum-=arr[i-k]; dp[i+1]=dp[i]; if(i-k+1>=l) dp[i+1]=max(dp[i+1],dp[i-k+1]+sum); //printf(" dp: %lld\n",dp[i+1]); } printf("%lld\n",dp[r+1]); } return 0; } |