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>
using namespace std;
const int N = 3e5 + 2;
int n, vis[N][2];
long long pan, a[N];
pair<long long, int> mem[N][2];
pair<long long, int> operator + (pair<long long, int> x, pair<long long, int> y) {
return make_pair(x.first + y.first, x.second + y.second);
}
pair<long long, int> dp(int i, int f) {
if (i == n) return {0, 0};
if (vis[i][f]) return mem[i][f];
vis[i][f] = 1;
auto &ret = mem[i][f];
ret = dp(i + 1, 1);
ret = max(ret, dp(i + 1, 0) + make_pair(-pan * f + a[i], -1 * f));
return ret;
}
pair<long long, int> get(long long x) {
pan = x;
memset(vis, 0, sizeof(vis));
return dp(0, 1);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
// auto z = get(1);
// cout << z.first << endl;
// cout << z.second << endl;
long long l = 0, r = 1 << 30, res = 0;
while (l <= r) {
long long m = (l + r) / 2;
auto [x, y] = get(m);
if (-y <= k) {
res = x + k * m;
r = m - 1;
} else l = m + 1;
}
cout << res << '\n';
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... |