#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);
}
// 1-> (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, 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 = 1LL << 42, res = 0;
while (l <= r) {
long long m = (l + r) / 2;
auto [x, y] = get(m);
if (-y <= k) {
res = x + (-y) * m;
r = m - 1;
} else l = m + 1;
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
48800 KB |
Output is correct |
2 |
Correct |
367 ms |
49680 KB |
Output is correct |
3 |
Correct |
372 ms |
50256 KB |
Output is correct |
4 |
Correct |
378 ms |
49880 KB |
Output is correct |
5 |
Correct |
368 ms |
49844 KB |
Output is correct |
6 |
Correct |
368 ms |
49120 KB |
Output is correct |
7 |
Correct |
419 ms |
48872 KB |
Output is correct |
8 |
Correct |
409 ms |
49748 KB |
Output is correct |
9 |
Correct |
400 ms |
48980 KB |
Output is correct |
10 |
Correct |
423 ms |
49588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
47444 KB |
Output is correct |
2 |
Correct |
430 ms |
48212 KB |
Output is correct |
3 |
Correct |
390 ms |
47396 KB |
Output is correct |
4 |
Correct |
404 ms |
47800 KB |
Output is correct |
5 |
Correct |
389 ms |
48980 KB |
Output is correct |
6 |
Correct |
410 ms |
47568 KB |
Output is correct |
7 |
Correct |
404 ms |
48468 KB |
Output is correct |
8 |
Correct |
397 ms |
49992 KB |
Output is correct |
9 |
Correct |
360 ms |
48820 KB |
Output is correct |
10 |
Correct |
370 ms |
48272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
399 ms |
50008 KB |
Output is correct |
2 |
Correct |
397 ms |
49488 KB |
Output is correct |
3 |
Correct |
394 ms |
49748 KB |
Output is correct |
4 |
Correct |
432 ms |
49540 KB |
Output is correct |
5 |
Correct |
454 ms |
49892 KB |
Output is correct |
6 |
Correct |
448 ms |
50000 KB |
Output is correct |
7 |
Correct |
444 ms |
50256 KB |
Output is correct |
8 |
Correct |
437 ms |
49748 KB |
Output is correct |
9 |
Correct |
449 ms |
50376 KB |
Output is correct |
10 |
Correct |
452 ms |
50064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4700 KB |
Output is correct |
2 |
Correct |
3 ms |
2784 KB |
Output is correct |
3 |
Correct |
3 ms |
2824 KB |
Output is correct |
4 |
Correct |
3 ms |
2824 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
4 ms |
2652 KB |
Output is correct |
10 |
Correct |
4 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4700 KB |
Output is correct |
2 |
Correct |
3 ms |
2784 KB |
Output is correct |
3 |
Correct |
3 ms |
2824 KB |
Output is correct |
4 |
Correct |
3 ms |
2824 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
4 ms |
2652 KB |
Output is correct |
10 |
Correct |
4 ms |
4700 KB |
Output is correct |
11 |
Correct |
4 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
2652 KB |
Output is correct |
13 |
Correct |
4 ms |
2652 KB |
Output is correct |
14 |
Correct |
4 ms |
2652 KB |
Output is correct |
15 |
Correct |
4 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
4 ms |
2652 KB |
Output is correct |
19 |
Correct |
4 ms |
2652 KB |
Output is correct |
20 |
Correct |
4 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4700 KB |
Output is correct |
2 |
Correct |
3 ms |
2784 KB |
Output is correct |
3 |
Correct |
3 ms |
2824 KB |
Output is correct |
4 |
Correct |
3 ms |
2824 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
4 ms |
2652 KB |
Output is correct |
10 |
Correct |
4 ms |
4700 KB |
Output is correct |
11 |
Correct |
4 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
2652 KB |
Output is correct |
13 |
Correct |
4 ms |
2652 KB |
Output is correct |
14 |
Correct |
4 ms |
2652 KB |
Output is correct |
15 |
Correct |
4 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
4 ms |
2652 KB |
Output is correct |
19 |
Correct |
4 ms |
2652 KB |
Output is correct |
20 |
Correct |
4 ms |
2652 KB |
Output is correct |
21 |
Correct |
5 ms |
2908 KB |
Output is correct |
22 |
Correct |
5 ms |
3120 KB |
Output is correct |
23 |
Correct |
7 ms |
2908 KB |
Output is correct |
24 |
Correct |
7 ms |
2908 KB |
Output is correct |
25 |
Correct |
6 ms |
4956 KB |
Output is correct |
26 |
Correct |
6 ms |
2904 KB |
Output is correct |
27 |
Correct |
7 ms |
5164 KB |
Output is correct |
28 |
Correct |
6 ms |
3128 KB |
Output is correct |
29 |
Correct |
5 ms |
2904 KB |
Output is correct |
30 |
Correct |
6 ms |
3120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
48800 KB |
Output is correct |
2 |
Correct |
367 ms |
49680 KB |
Output is correct |
3 |
Correct |
372 ms |
50256 KB |
Output is correct |
4 |
Correct |
378 ms |
49880 KB |
Output is correct |
5 |
Correct |
368 ms |
49844 KB |
Output is correct |
6 |
Correct |
368 ms |
49120 KB |
Output is correct |
7 |
Correct |
419 ms |
48872 KB |
Output is correct |
8 |
Correct |
409 ms |
49748 KB |
Output is correct |
9 |
Correct |
400 ms |
48980 KB |
Output is correct |
10 |
Correct |
423 ms |
49588 KB |
Output is correct |
11 |
Correct |
411 ms |
47444 KB |
Output is correct |
12 |
Correct |
430 ms |
48212 KB |
Output is correct |
13 |
Correct |
390 ms |
47396 KB |
Output is correct |
14 |
Correct |
404 ms |
47800 KB |
Output is correct |
15 |
Correct |
389 ms |
48980 KB |
Output is correct |
16 |
Correct |
410 ms |
47568 KB |
Output is correct |
17 |
Correct |
404 ms |
48468 KB |
Output is correct |
18 |
Correct |
397 ms |
49992 KB |
Output is correct |
19 |
Correct |
360 ms |
48820 KB |
Output is correct |
20 |
Correct |
370 ms |
48272 KB |
Output is correct |
21 |
Correct |
399 ms |
50008 KB |
Output is correct |
22 |
Correct |
397 ms |
49488 KB |
Output is correct |
23 |
Correct |
394 ms |
49748 KB |
Output is correct |
24 |
Correct |
432 ms |
49540 KB |
Output is correct |
25 |
Correct |
454 ms |
49892 KB |
Output is correct |
26 |
Correct |
448 ms |
50000 KB |
Output is correct |
27 |
Correct |
444 ms |
50256 KB |
Output is correct |
28 |
Correct |
437 ms |
49748 KB |
Output is correct |
29 |
Correct |
449 ms |
50376 KB |
Output is correct |
30 |
Correct |
452 ms |
50064 KB |
Output is correct |
31 |
Correct |
4 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
2784 KB |
Output is correct |
33 |
Correct |
3 ms |
2824 KB |
Output is correct |
34 |
Correct |
3 ms |
2824 KB |
Output is correct |
35 |
Correct |
3 ms |
2652 KB |
Output is correct |
36 |
Correct |
4 ms |
2652 KB |
Output is correct |
37 |
Correct |
3 ms |
2652 KB |
Output is correct |
38 |
Correct |
4 ms |
4700 KB |
Output is correct |
39 |
Correct |
4 ms |
2652 KB |
Output is correct |
40 |
Correct |
4 ms |
4700 KB |
Output is correct |
41 |
Correct |
4 ms |
2652 KB |
Output is correct |
42 |
Correct |
3 ms |
2652 KB |
Output is correct |
43 |
Correct |
4 ms |
2652 KB |
Output is correct |
44 |
Correct |
4 ms |
2652 KB |
Output is correct |
45 |
Correct |
4 ms |
2652 KB |
Output is correct |
46 |
Correct |
3 ms |
2652 KB |
Output is correct |
47 |
Correct |
3 ms |
2652 KB |
Output is correct |
48 |
Correct |
4 ms |
2652 KB |
Output is correct |
49 |
Correct |
4 ms |
2652 KB |
Output is correct |
50 |
Correct |
4 ms |
2652 KB |
Output is correct |
51 |
Correct |
5 ms |
2908 KB |
Output is correct |
52 |
Correct |
5 ms |
3120 KB |
Output is correct |
53 |
Correct |
7 ms |
2908 KB |
Output is correct |
54 |
Correct |
7 ms |
2908 KB |
Output is correct |
55 |
Correct |
6 ms |
4956 KB |
Output is correct |
56 |
Correct |
6 ms |
2904 KB |
Output is correct |
57 |
Correct |
7 ms |
5164 KB |
Output is correct |
58 |
Correct |
6 ms |
3128 KB |
Output is correct |
59 |
Correct |
5 ms |
2904 KB |
Output is correct |
60 |
Correct |
6 ms |
3120 KB |
Output is correct |
61 |
Correct |
495 ms |
49236 KB |
Output is correct |
62 |
Correct |
477 ms |
50364 KB |
Output is correct |
63 |
Correct |
423 ms |
49040 KB |
Output is correct |
64 |
Correct |
495 ms |
50000 KB |
Output is correct |
65 |
Correct |
478 ms |
50004 KB |
Output is correct |
66 |
Correct |
470 ms |
49744 KB |
Output is correct |
67 |
Correct |
447 ms |
49184 KB |
Output is correct |
68 |
Correct |
398 ms |
50256 KB |
Output is correct |
69 |
Correct |
401 ms |
49208 KB |
Output is correct |
70 |
Correct |
384 ms |
49204 KB |
Output is correct |