#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<ll,ll>
#define f first
#define s second
const int mxN = 3e5;
int n,k;
int a[mxN];
pii test(ll v){
pii dp[2];
pii dp2[2];
dp[0] = {0,0};
dp[1] = {0,0};
for (int i = 0; i < n; i++){
dp2[0] = max(dp[0],(pii){dp[1].f-v,dp[1].s+1});
dp2[1] = max((pii) {dp[0].f+a[i],dp[0].s}, (pii) {dp[1].f+a[i],dp[1].s});
swap(dp2,dp);
}
dp[1] = (pii) {dp[1].f,dp[1].s+1};
return max(dp[0],dp[1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
ll l = -1;
ll r = 1e15;
ll c;
while (r-l>1){
c = (l+r)/2;
if(test(c).s<=k) r = c;
else l = c;
}
pii t = test(r);
// cout << r << " -= " << t.f << " " << t.s << '\n';
cout << t.f - (t.s-k)*r << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4304 KB |
Output is correct |
2 |
Correct |
37 ms |
4432 KB |
Output is correct |
3 |
Correct |
41 ms |
4444 KB |
Output is correct |
4 |
Correct |
38 ms |
4444 KB |
Output is correct |
5 |
Correct |
37 ms |
4444 KB |
Output is correct |
6 |
Correct |
35 ms |
4380 KB |
Output is correct |
7 |
Correct |
39 ms |
4184 KB |
Output is correct |
8 |
Correct |
38 ms |
4432 KB |
Output is correct |
9 |
Correct |
36 ms |
4184 KB |
Output is correct |
10 |
Correct |
35 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
2652 KB |
Output is correct |
2 |
Correct |
33 ms |
2648 KB |
Output is correct |
3 |
Incorrect |
32 ms |
2648 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
4528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4304 KB |
Output is correct |
2 |
Correct |
37 ms |
4432 KB |
Output is correct |
3 |
Correct |
41 ms |
4444 KB |
Output is correct |
4 |
Correct |
38 ms |
4444 KB |
Output is correct |
5 |
Correct |
37 ms |
4444 KB |
Output is correct |
6 |
Correct |
35 ms |
4380 KB |
Output is correct |
7 |
Correct |
39 ms |
4184 KB |
Output is correct |
8 |
Correct |
38 ms |
4432 KB |
Output is correct |
9 |
Correct |
36 ms |
4184 KB |
Output is correct |
10 |
Correct |
35 ms |
4444 KB |
Output is correct |
11 |
Correct |
38 ms |
2652 KB |
Output is correct |
12 |
Correct |
33 ms |
2648 KB |
Output is correct |
13 |
Incorrect |
32 ms |
2648 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |