#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-v,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 = 0;
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(l);
// cout << l << " -= " << t.f << " " << t.s << '\n';
cout << t.f + k*l << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
1368 KB |
Output is correct |
2 |
Correct |
35 ms |
1372 KB |
Output is correct |
3 |
Correct |
36 ms |
1628 KB |
Output is correct |
4 |
Correct |
36 ms |
1368 KB |
Output is correct |
5 |
Correct |
34 ms |
1372 KB |
Output is correct |
6 |
Correct |
35 ms |
1372 KB |
Output is correct |
7 |
Correct |
34 ms |
1372 KB |
Output is correct |
8 |
Correct |
36 ms |
1372 KB |
Output is correct |
9 |
Correct |
35 ms |
1372 KB |
Output is correct |
10 |
Correct |
34 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
1372 KB |
Output is correct |
2 |
Correct |
48 ms |
1368 KB |
Output is correct |
3 |
Correct |
34 ms |
1368 KB |
Output is correct |
4 |
Correct |
30 ms |
2656 KB |
Output is correct |
5 |
Correct |
43 ms |
4324 KB |
Output is correct |
6 |
Correct |
37 ms |
2652 KB |
Output is correct |
7 |
Correct |
30 ms |
2652 KB |
Output is correct |
8 |
Correct |
46 ms |
4444 KB |
Output is correct |
9 |
Correct |
46 ms |
4180 KB |
Output is correct |
10 |
Correct |
38 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
1444 KB |
Output is correct |
2 |
Correct |
62 ms |
4608 KB |
Output is correct |
3 |
Correct |
62 ms |
4440 KB |
Output is correct |
4 |
Correct |
62 ms |
4432 KB |
Output is correct |
5 |
Correct |
76 ms |
4660 KB |
Output is correct |
6 |
Correct |
57 ms |
4440 KB |
Output is correct |
7 |
Correct |
61 ms |
4444 KB |
Output is correct |
8 |
Correct |
58 ms |
4564 KB |
Output is correct |
9 |
Correct |
60 ms |
4444 KB |
Output is correct |
10 |
Correct |
67 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
496 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
1368 KB |
Output is correct |
2 |
Correct |
35 ms |
1372 KB |
Output is correct |
3 |
Correct |
36 ms |
1628 KB |
Output is correct |
4 |
Correct |
36 ms |
1368 KB |
Output is correct |
5 |
Correct |
34 ms |
1372 KB |
Output is correct |
6 |
Correct |
35 ms |
1372 KB |
Output is correct |
7 |
Correct |
34 ms |
1372 KB |
Output is correct |
8 |
Correct |
36 ms |
1372 KB |
Output is correct |
9 |
Correct |
35 ms |
1372 KB |
Output is correct |
10 |
Correct |
34 ms |
1368 KB |
Output is correct |
11 |
Correct |
39 ms |
1372 KB |
Output is correct |
12 |
Correct |
48 ms |
1368 KB |
Output is correct |
13 |
Correct |
34 ms |
1368 KB |
Output is correct |
14 |
Correct |
30 ms |
2656 KB |
Output is correct |
15 |
Correct |
43 ms |
4324 KB |
Output is correct |
16 |
Correct |
37 ms |
2652 KB |
Output is correct |
17 |
Correct |
30 ms |
2652 KB |
Output is correct |
18 |
Correct |
46 ms |
4444 KB |
Output is correct |
19 |
Correct |
46 ms |
4180 KB |
Output is correct |
20 |
Correct |
38 ms |
2652 KB |
Output is correct |
21 |
Correct |
59 ms |
1444 KB |
Output is correct |
22 |
Correct |
62 ms |
4608 KB |
Output is correct |
23 |
Correct |
62 ms |
4440 KB |
Output is correct |
24 |
Correct |
62 ms |
4432 KB |
Output is correct |
25 |
Correct |
76 ms |
4660 KB |
Output is correct |
26 |
Correct |
57 ms |
4440 KB |
Output is correct |
27 |
Correct |
61 ms |
4444 KB |
Output is correct |
28 |
Correct |
58 ms |
4564 KB |
Output is correct |
29 |
Correct |
60 ms |
4444 KB |
Output is correct |
30 |
Correct |
67 ms |
4432 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
460 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
464 KB |
Output is correct |
51 |
Correct |
1 ms |
512 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
496 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
88 ms |
4436 KB |
Output is correct |
62 |
Correct |
85 ms |
4676 KB |
Output is correct |
63 |
Correct |
68 ms |
4440 KB |
Output is correct |
64 |
Correct |
86 ms |
4432 KB |
Output is correct |
65 |
Correct |
105 ms |
4444 KB |
Output is correct |
66 |
Correct |
87 ms |
4432 KB |
Output is correct |
67 |
Correct |
85 ms |
4436 KB |
Output is correct |
68 |
Correct |
62 ms |
4436 KB |
Output is correct |
69 |
Correct |
63 ms |
4440 KB |
Output is correct |
70 |
Correct |
58 ms |
4480 KB |
Output is correct |