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