이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e18;
const int N = 3e5 + 7;
int n, k, a[N];
pair<int, int> dp[N][2];
pair<int, int> calc(int pen) {
for(int i = 1; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][1].fi + a[i], dp[i - 1][1].se),
make_pair(dp[i - 1][0].fi + a[i] - pen, dp[i - 1][0].se + 1));
}
return max(dp[n][0], dp[n][1]);
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
dp[0][0] = {0, 0};
dp[0][1] = {-inf, -inf};
int l = 0, r = 1e18;
int ans = 0;
while(l <= r) {
int m = (l + r) >> 1;
if(calc(m).se >= k) {
ans = m;
l = m + 1;
} else r = m - 1;
}
cout << calc(ans).fi + k *ans;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
# | 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... |