#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);
}
// 0-> (i - 1) not taken vice verca
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, 0);
ret = max(ret, dp(i + 1, 1) + make_pair(a[i] - pan * !f, -1 * !f));
return ret;
}
pair<long long, int> get(long long x) {
pan = x;
memset(vis, 0, sizeof(vis));
return dp(0, 0);
}
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 = 1LL << 42, 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
427 ms |
46000 KB |
Output is correct |
2 |
Correct |
444 ms |
46676 KB |
Output is correct |
3 |
Correct |
448 ms |
47184 KB |
Output is correct |
4 |
Correct |
461 ms |
47028 KB |
Output is correct |
5 |
Correct |
417 ms |
46936 KB |
Output is correct |
6 |
Correct |
405 ms |
45908 KB |
Output is correct |
7 |
Correct |
409 ms |
45928 KB |
Output is correct |
8 |
Correct |
417 ms |
46676 KB |
Output is correct |
9 |
Correct |
426 ms |
45904 KB |
Output is correct |
10 |
Correct |
411 ms |
46504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
402 ms |
46160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
482 ms |
46932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
427 ms |
46000 KB |
Output is correct |
2 |
Correct |
444 ms |
46676 KB |
Output is correct |
3 |
Correct |
448 ms |
47184 KB |
Output is correct |
4 |
Correct |
461 ms |
47028 KB |
Output is correct |
5 |
Correct |
417 ms |
46936 KB |
Output is correct |
6 |
Correct |
405 ms |
45908 KB |
Output is correct |
7 |
Correct |
409 ms |
45928 KB |
Output is correct |
8 |
Correct |
417 ms |
46676 KB |
Output is correct |
9 |
Correct |
426 ms |
45904 KB |
Output is correct |
10 |
Correct |
411 ms |
46504 KB |
Output is correct |
11 |
Incorrect |
402 ms |
46160 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |