#include <bits/stdc++.h> using namespace std; int n, k, q; long long query(int l, int r, vector <long long>& prefsums){ vector <long long> dp(n+1, 0); for(int i = l+k-1; i<=r; ++i){ dp[i] = max(dp[i-1], dp[i-k]+prefsums[i]-prefsums[i-k]); } return dp[r]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> q; int x; vector <long long> prefsums(n+1); prefsums[0] = 0; for(int i = 1; i<=n; ++i){ cin >> x; prefsums[i] = prefsums[i-1]+x; } int l, r; for(int i = 0; i<q; ++i){ cin >> l >> r; cout << query(l, r, prefsums) << "\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; int n, k, q; long long query(int l, int r, vector <long long>& prefsums){ vector <long long> dp(n+1, 0); for(int i = l+k-1; i<=r; ++i){ dp[i] = max(dp[i-1], dp[i-k]+prefsums[i]-prefsums[i-k]); } return dp[r]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> q; int x; vector <long long> prefsums(n+1); prefsums[0] = 0; for(int i = 1; i<=n; ++i){ cin >> x; prefsums[i] = prefsums[i-1]+x; } int l, r; for(int i = 0; i<q; ++i){ cin >> l >> r; cout << query(l, r, prefsums) << "\n"; } } |