Skip to content

Commit 2bc2014

Browse files
authored
[20251204] BOJ / P3 / Mowing the Lawn / 권혁준
1 parent 0306ab1 commit 2bc2014

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
```

0 commit comments

Comments
 (0)