From 2bc20149e477dd5b6f7ed2ac4ec9f6c022bac876 Mon Sep 17 00:00:00 2001 From: oncsr Date: Thu, 4 Dec 2025 13:54:02 +0900 Subject: [PATCH] =?UTF-8?q?[20251204]=20BOJ=20/=20P3=20/=20Mowing=20the=20?= =?UTF-8?q?Lawn=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- khj20006/202512/04 BOJ P3 Mowing the Lawn.md | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 khj20006/202512/04 BOJ P3 Mowing the Lawn.md diff --git a/khj20006/202512/04 BOJ P3 Mowing the Lawn.md b/khj20006/202512/04 BOJ P3 Mowing the Lawn.md new file mode 100644 index 00000000..bfe25b84 --- /dev/null +++ b/khj20006/202512/04 BOJ P3 Mowing the Lawn.md @@ -0,0 +1,37 @@ +```cpp +#include +using namespace std; +using ll = long long; + +int N, K; +ll dp[100001]{}, s[100001]{}; +deque> d; + +int main() { + cin.tie(0)->sync_with_stdio(0); + + cin >> N >> K; + for (int i = 1, a; i <= N; i++) { + cin >> a; + s[i] = s[i - 1] + a; + } + + ll ans = 0; + d.emplace_back(s[N], -1); + d.emplace_back(s[N] - s[1], 0); + for (int i = 1; i <= N; i++) { + while (!d.empty() && i - d.front().second > K + 1) d.pop_front(); + int idx = d.front().second; + dp[i] = (idx >= 0 ? dp[idx] : 0) + s[i] - s[idx + 1]; + + ans = max(ans, dp[i]); + if (i < N) { + ll val = dp[i] + s[N] - s[i + 1]; + while (!d.empty() && d.back().first <= val) d.pop_back(); + d.emplace_back(val, i); + } + } + cout << ans; + +} +```