#include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, k, q, a, b; int tab[300007]; long long prefix_sums[300007], dp[300007], res = 0; cin >> n >> k >> q; for (int i = 1; i <= n; i++) { cin >> tab[i]; dp[i] = 0; } prefix_sums[0] = 0; for (auto i = 1; i <= n; i++) { prefix_sums[i] = prefix_sums[i - 1] + tab[i]; } for (auto l = 0; l < q; l++) { cin >> a >> b; res = 0; for(auto i = a + k - 1; i <= b; i ++) { dp[i] = max(dp[i - 1], dp[i - k] + prefix_sums[i] - prefix_sums[i - k]); res = max(res, dp[i]); } cout << res << endl; for(auto i = a + k - 1; i <= b; i ++) { dp[i] = 0; } } 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 | #include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, k, q, a, b; int tab[300007]; long long prefix_sums[300007], dp[300007], res = 0; cin >> n >> k >> q; for (int i = 1; i <= n; i++) { cin >> tab[i]; dp[i] = 0; } prefix_sums[0] = 0; for (auto i = 1; i <= n; i++) { prefix_sums[i] = prefix_sums[i - 1] + tab[i]; } for (auto l = 0; l < q; l++) { cin >> a >> b; res = 0; for(auto i = a + k - 1; i <= b; i ++) { dp[i] = max(dp[i - 1], dp[i - k] + prefix_sums[i] - prefix_sums[i - k]); res = max(res, dp[i]); } cout << res << endl; for(auto i = a + k - 1; i <= b; i ++) { dp[i] = 0; } } return 0; } |