#include <iostream> #include <vector> using namespace std; long long getRes(const vector<long long>& strength, int start, int end, int k, int n) { if(end - start + 1 < k) { return 0; } long long sum = 0; vector<pair<long long, long long> > dp(n); for(int i = start; i < start + k; ++i) { sum += strength[i]; } dp[start + k - 1].first = sum; dp[start + k - 1].second = 0; sum -= strength[start]; for(int i = start + k; i <= end; ++i) { sum += strength[i]; dp[i].first = max(dp[i - k].first, dp[i - k].second) + sum; dp[i].second = max(dp[i - 1].first, dp[i - 1].second); sum -= strength[i - k + 1]; } return max(dp[end].first, dp[end].second); } int main() { ios_base::sync_with_stdio(0); int n, k, q; cin >> n >> k >> q; vector<long long> strength(n); for(int i = 0; i < n; ++i) { cin >> strength[i]; } for(int i = 0; i < q; ++i) { int a, b; cin >> a >> b; cout << getRes(strength, a - 1, b - 1, k, n) << "\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 42 43 44 45 46 47 48 49 | #include <iostream> #include <vector> using namespace std; long long getRes(const vector<long long>& strength, int start, int end, int k, int n) { if(end - start + 1 < k) { return 0; } long long sum = 0; vector<pair<long long, long long> > dp(n); for(int i = start; i < start + k; ++i) { sum += strength[i]; } dp[start + k - 1].first = sum; dp[start + k - 1].second = 0; sum -= strength[start]; for(int i = start + k; i <= end; ++i) { sum += strength[i]; dp[i].first = max(dp[i - k].first, dp[i - k].second) + sum; dp[i].second = max(dp[i - 1].first, dp[i - 1].second); sum -= strength[i - k + 1]; } return max(dp[end].first, dp[end].second); } int main() { ios_base::sync_with_stdio(0); int n, k, q; cin >> n >> k >> q; vector<long long> strength(n); for(int i = 0; i < n; ++i) { cin >> strength[i]; } for(int i = 0; i < q; ++i) { int a, b; cin >> a >> b; cout << getRes(strength, a - 1, b - 1, k, n) << "\n"; } } |