#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define fi first #define se second #define p_b push_back #define pll pair<ll,ll> #define pii pair<int,int> #define m_p make_pair #define all(x) x.begin(),x.end() #define sset ordered_set #define sqr(x) (x)*(x) #define pw(x) (1ll << (x)) #define sz(x) (int)x.size() #define fout(x) {cout << x << "\n"; return; } using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; const int MAXN = 1e5; const int M = pw(16); const long long mod = 998244353; const int N = 1e6 + 200; //const int N = 100; const int inf = 1e9; template <typename T> void vout(T s){cout << s << endl;exit(0);} template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll n, k, q; cin >> n >> k >> q; vector <ll> a(n + 1), pref(n + 1), dp(n + 1, inf); for(int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = pref[i - 1] + a[i]; } while(q--){ int l, r; cin >> l >> r; if(r - l + 1 < k){ cout << "0\n"; }else{ int m = r - l + 1; dp[0] = 0; for(int i = 1; i <= m; i++){ if(i < k) dp[i] = 0; else{ dp[i] = max(dp[i - 1], dp[i - k] + pref[l + i - 1] - pref[l + i - k - 1]); } } cout << dp[m] << "\n"; } } 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 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 61 62 63 64 65 66 67 68 69 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define fi first #define se second #define p_b push_back #define pll pair<ll,ll> #define pii pair<int,int> #define m_p make_pair #define all(x) x.begin(),x.end() #define sset ordered_set #define sqr(x) (x)*(x) #define pw(x) (1ll << (x)) #define sz(x) (int)x.size() #define fout(x) {cout << x << "\n"; return; } using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; const int MAXN = 1e5; const int M = pw(16); const long long mod = 998244353; const int N = 1e6 + 200; //const int N = 100; const int inf = 1e9; template <typename T> void vout(T s){cout << s << endl;exit(0);} template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll n, k, q; cin >> n >> k >> q; vector <ll> a(n + 1), pref(n + 1), dp(n + 1, inf); for(int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = pref[i - 1] + a[i]; } while(q--){ int l, r; cin >> l >> r; if(r - l + 1 < k){ cout << "0\n"; }else{ int m = r - l + 1; dp[0] = 0; for(int i = 1; i <= m; i++){ if(i < k) dp[i] = 0; else{ dp[i] = max(dp[i - 1], dp[i - k] + pref[l + i - 1] - pref[l + i - k - 1]); } } cout << dp[m] << "\n"; } } return 0; } |