이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 3e5 + 25;
ll a[MAXN], n, k;
pair <ll, ll> eval (ll mid) {
pair <ll, ll> dp[n + 1][2];
dp[1][0] = {0, 0};
dp[1][1] = {a[1] - mid, 1};
for (int i = 2; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
pair <ll, ll> g = {dp[i - 1][0].first + a[i] - mid, dp[i - 1][0].second + 1};
pair <ll, ll> h = {dp[i - 1][1].first + a[i], dp[i - 1][1].second};
dp[i][1] = max(g, h);
}
return max(dp[n][0], dp[n][1]);
}
void solve () {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ll l = 0, r = 1e18, ans = -1;
while (l <= r) {
ll mid = (l + r) / 2;
if (eval(mid).second >= k) {
l = mid + 1; ans = mid;
} else {
r = mid - 1;
}
}
cout << max(0ll, eval(ans).first + ans * k) << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |