#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define p_b push_back
#define m_p make_pair
#define fi first
#define se second
const int maxn = 100001;
ll mod = 1000000007;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, k, q; cin>>n>>k>>q;
vector<ll> t1(n+1), pref(n+1);
for(int i = 1; i<=n; i++){
cin>>t1[i];
pref[i] = pref[i-1]+t1[i];
}
while(q--){
int l, r; cin>>l>>r;
vector<ll> dp(n+1);
ll md = 0;
for(int i = l+k-1; i<=r; i++){
if(i-k>=l) md = max(md, dp[i-k]);
if(i-k+1>=l)
dp[i] = max(dp[i-1], md+pref[i]-pref[i-k]);
}
cout<<dp[r]<<"\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 35 36 | #include <bits/stdc++.h> using namespace std; #define ll long long #define p_b push_back #define m_p make_pair #define fi first #define se second const int maxn = 100001; ll mod = 1000000007; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, q; cin>>n>>k>>q; vector<ll> t1(n+1), pref(n+1); for(int i = 1; i<=n; i++){ cin>>t1[i]; pref[i] = pref[i-1]+t1[i]; } while(q--){ int l, r; cin>>l>>r; vector<ll> dp(n+1); ll md = 0; for(int i = l+k-1; i<=r; i++){ if(i-k>=l) md = max(md, dp[i-k]); if(i-k+1>=l) dp[i] = max(dp[i-1], md+pref[i]-pref[i-k]); } cout<<dp[r]<<"\n"; } } |
English