#include<bits/stdc++.h> //#define int unsigned short int //#define int long long //popraw intmina #define ll long long #include <time.h> #include <chrono> #include <iomanip> #define BE(x) x.begin(),x.end() #define EB(x) x.end(),x.begin() #define st first #define nd second using namespace std; using namespace std::chrono; #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma unroll int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int n,k,q;cin>>n>>k>>q; vector<int> V(n); for(auto&x:V)cin>>x; vector<int>suf(n,V[n-1]); for(int i=n-2;i>=0;i--) suf[i]=suf[i+1]+V[i]; vector<int>ksuf(n,0);ksuf[n-k]=suf[n-k]; for(int i=n-k-1;i>=0;i--) ksuf[i]=suf[i]-suf[i+k]; // for(auto x:ksuf)cout<<x<<" "; // cout<<endl<<endl; while(q--){ int k1,k2;cin>>k1>>k2;k1--;k2; vector<int> dp(k2-k1+1,0); for(int i=1;i<dp.size();i++){ // cout << ksuf[k2-i]<<" "; if(i<k) dp[i]=0; else dp[i]=max(dp[i-1],dp[i-k]+ksuf[k2-i]); } cout<<dp.back()<<endl; } }
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 57 58 59 60 | #include<bits/stdc++.h> //#define int unsigned short int //#define int long long //popraw intmina #define ll long long #include <time.h> #include <chrono> #include <iomanip> #define BE(x) x.begin(),x.end() #define EB(x) x.end(),x.begin() #define st first #define nd second using namespace std; using namespace std::chrono; #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma unroll int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int n,k,q;cin>>n>>k>>q; vector<int> V(n); for(auto&x:V)cin>>x; vector<int>suf(n,V[n-1]); for(int i=n-2;i>=0;i--) suf[i]=suf[i+1]+V[i]; vector<int>ksuf(n,0);ksuf[n-k]=suf[n-k]; for(int i=n-k-1;i>=0;i--) ksuf[i]=suf[i]-suf[i+k]; // for(auto x:ksuf)cout<<x<<" "; // cout<<endl<<endl; while(q--){ int k1,k2;cin>>k1>>k2;k1--;k2; vector<int> dp(k2-k1+1,0); for(int i=1;i<dp.size();i++){ // cout << ksuf[k2-i]<<" "; if(i<k) dp[i]=0; else dp[i]=max(dp[i-1],dp[i-k]+ksuf[k2-i]); } cout<<dp.back()<<endl; } } |