#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX = 3e5+5; LL a[MAX]; LL dp[MAX]; LL sums[MAX]; int main() { ios_base::sync_with_stdio(false); int n, k, q; cin >> n >> k >> q; LL sum = 0; for(int i = 0; i < n; ++i) { cin >> a[i]; sums[i] = sum; sum += a[i]; } sums[n] = sum; while(q--) { int l, r; cin >> l >> r; l--; r--; for(int i = l; i < l+k; ++i) { dp[i] = 0; } dp[l+k-1] = max(0ll, sums[l+k]-sums[l]); for(int i = l+k; i <= r; ++i) { dp[i] = max(dp[i-k]+sums[i+1]-sums[i-k+1], dp[i-1]); } cout << dp[r] << '\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 37 38 39 40 41 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX = 3e5+5; LL a[MAX]; LL dp[MAX]; LL sums[MAX]; int main() { ios_base::sync_with_stdio(false); int n, k, q; cin >> n >> k >> q; LL sum = 0; for(int i = 0; i < n; ++i) { cin >> a[i]; sums[i] = sum; sum += a[i]; } sums[n] = sum; while(q--) { int l, r; cin >> l >> r; l--; r--; for(int i = l; i < l+k; ++i) { dp[i] = 0; } dp[l+k-1] = max(0ll, sums[l+k]-sums[l]); for(int i = l+k; i <= r; ++i) { dp[i] = max(dp[i-k]+sums[i+1]-sums[i-k+1], dp[i-1]); } cout << dp[r] << '\n'; } } |