File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` cpp
2+ #include < bits/stdc++.h>
3+ using namespace std ;
4+ using ll = long long ;
5+
6+ int N, K;
7+ ll dp[100001 ]{}, s[100001 ]{};
8+ deque<pair<ll, int >> d;
9+
10+ int main () {
11+ cin.tie(0)->sync_with_stdio(0);
12+
13+ cin >> N >> K;
14+ for (int i = 1, a; i <= N; i++) {
15+ cin >> a;
16+ s[i] = s[i - 1] + a;
17+ }
18+
19+ ll ans = 0;
20+ d.emplace_back(s[N], -1);
21+ d.emplace_back(s[N] - s[1], 0);
22+ for (int i = 1; i <= N; i++) {
23+ while (!d.empty() && i - d.front().second > K + 1) d.pop_front();
24+ int idx = d.front().second;
25+ dp[i] = (idx >= 0 ? dp[idx] : 0) + s[i] - s[idx + 1];
26+
27+ ans = max(ans, dp[i]);
28+ if (i < N) {
29+ ll val = dp[i] + s[N] - s[i + 1];
30+ while (!d.empty() && d.back().first <= val) d.pop_back();
31+ d.emplace_back(val, i);
32+ }
33+ }
34+ cout << ans;
35+
36+ }
37+ ```
You can’t perform that action at this time.
0 commit comments