This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 3e5 + 7;
int n, k, a[N];
void input() {
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
}
pair<long long, int> dp[N][2];
pair<long long, long long> solveLambda(long long x) {
dp[1][0] = make_pair(0, 0);
dp[1][1] = make_pair(a[1] - x, 1);
for(int i = 2; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][0].fi + a[i] - x, dp[i - 1][0].se + 1),
make_pair(dp[i - 1][1].fi + a[i], dp[i - 1][1].se));
}
return max(dp[n][0], dp[n][1]);
}
void solve() {
long long l = 0, r = 1e18;
while(r - l > 1) {
long long mid = (l + r) >> 1;
pair<long long, long long> cur = solveLambda(mid);
if(cur.se >= k) l = mid;
else r = mid;
}
cout << solveLambda(l).fi + l * k;
}
int main() {
cin.tie(NULL)->ios_base::sync_with_stdio(NULL);
//freopen(" .inp", "r", stdin);
//freopen(" .out", "w", stdout);
input();
solve();
return 0;
}
# | 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... |