#include <bits/stdc++.h>
using namespace std;
template<class A, class B> bool maximize(A& x, B y) {if (x < y) return x = y, true; else return false;}
template<class A, class B> bool minimize(A& x, B y) {if (x > y) return x = y, true; else return false;}
#define all(a) a.begin(), a.end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double db;
const int MAX_N = 3e5 + 5;
const db inf = 3e14;
const db eps = 1e-3;
int n, k;
int a[MAX_N];
pair<db, int> dp[2][2];
bool curr, nxt;
int check(db lamda) {
curr = 0, nxt = 1;
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
dp[i][j] = {-inf, -inf};
}
}
dp[curr][0] = {0, 0};
for (int i = 0; i < n; i++, swap(curr, nxt)) {
// 0
maximize(dp[nxt][0], dp[curr][0]);
maximize(dp[nxt][1], pair<db, int>(dp[curr][0].fi - lamda + (db) a[i + 1], dp[curr][0].se + 1));
// 1
maximize(dp[nxt][1], pair<db, int>(dp[curr][1].fi + (db) a[i + 1], dp[curr][1].se));
maximize(dp[nxt][0], dp[curr][1]);
for (int j = 0; j <= 1; j++) {
dp[curr][j] = {-inf, -inf};
}
}
return max(dp[curr][1], dp[curr][0]).se;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen("FEAS.inp", "r")) {
freopen("FEAS.inp", "r", stdin);
freopen("FEAS.out", "w", stdout);
}
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
db low = 0, high = inf;
while (low + eps < high) {
db mid = (low + high) / 2;
if (check(mid) >= k) {
low = mid;
}
else {
high = mid;
}
}
db ans = low;
check(ans);
cout << (ll) round(max(dp[curr][0], dp[curr][1]).fi + ans * k);
return 0;
}
/*
Saturday, 16 December 2023
14:53:03
*/
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | freopen("FEAS.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen("FEAS.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
2712 KB |
Output is correct |
2 |
Correct |
431 ms |
2760 KB |
Output is correct |
3 |
Correct |
410 ms |
2788 KB |
Output is correct |
4 |
Correct |
414 ms |
2896 KB |
Output is correct |
5 |
Correct |
412 ms |
2652 KB |
Output is correct |
6 |
Correct |
406 ms |
2716 KB |
Output is correct |
7 |
Correct |
404 ms |
2648 KB |
Output is correct |
8 |
Correct |
412 ms |
2764 KB |
Output is correct |
9 |
Correct |
413 ms |
2724 KB |
Output is correct |
10 |
Correct |
403 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
379 ms |
2736 KB |
Output is correct |
2 |
Correct |
381 ms |
2788 KB |
Output is correct |
3 |
Correct |
378 ms |
2724 KB |
Output is correct |
4 |
Correct |
391 ms |
2652 KB |
Output is correct |
5 |
Correct |
383 ms |
2900 KB |
Output is correct |
6 |
Correct |
373 ms |
2648 KB |
Output is correct |
7 |
Correct |
402 ms |
2780 KB |
Output is correct |
8 |
Correct |
387 ms |
2772 KB |
Output is correct |
9 |
Correct |
390 ms |
2944 KB |
Output is correct |
10 |
Correct |
393 ms |
2768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
420 ms |
2756 KB |
Output is correct |
2 |
Correct |
420 ms |
2728 KB |
Output is correct |
3 |
Correct |
417 ms |
2648 KB |
Output is correct |
4 |
Correct |
419 ms |
2728 KB |
Output is correct |
5 |
Correct |
418 ms |
2752 KB |
Output is correct |
6 |
Correct |
420 ms |
2900 KB |
Output is correct |
7 |
Correct |
420 ms |
2776 KB |
Output is correct |
8 |
Correct |
414 ms |
2752 KB |
Output is correct |
9 |
Correct |
421 ms |
2652 KB |
Output is correct |
10 |
Correct |
419 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
480 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
2712 KB |
Output is correct |
2 |
Correct |
431 ms |
2760 KB |
Output is correct |
3 |
Correct |
410 ms |
2788 KB |
Output is correct |
4 |
Correct |
414 ms |
2896 KB |
Output is correct |
5 |
Correct |
412 ms |
2652 KB |
Output is correct |
6 |
Correct |
406 ms |
2716 KB |
Output is correct |
7 |
Correct |
404 ms |
2648 KB |
Output is correct |
8 |
Correct |
412 ms |
2764 KB |
Output is correct |
9 |
Correct |
413 ms |
2724 KB |
Output is correct |
10 |
Correct |
403 ms |
2652 KB |
Output is correct |
11 |
Correct |
379 ms |
2736 KB |
Output is correct |
12 |
Correct |
381 ms |
2788 KB |
Output is correct |
13 |
Correct |
378 ms |
2724 KB |
Output is correct |
14 |
Correct |
391 ms |
2652 KB |
Output is correct |
15 |
Correct |
383 ms |
2900 KB |
Output is correct |
16 |
Correct |
373 ms |
2648 KB |
Output is correct |
17 |
Correct |
402 ms |
2780 KB |
Output is correct |
18 |
Correct |
387 ms |
2772 KB |
Output is correct |
19 |
Correct |
390 ms |
2944 KB |
Output is correct |
20 |
Correct |
393 ms |
2768 KB |
Output is correct |
21 |
Correct |
420 ms |
2756 KB |
Output is correct |
22 |
Correct |
420 ms |
2728 KB |
Output is correct |
23 |
Correct |
417 ms |
2648 KB |
Output is correct |
24 |
Correct |
419 ms |
2728 KB |
Output is correct |
25 |
Correct |
418 ms |
2752 KB |
Output is correct |
26 |
Correct |
420 ms |
2900 KB |
Output is correct |
27 |
Correct |
420 ms |
2776 KB |
Output is correct |
28 |
Correct |
414 ms |
2752 KB |
Output is correct |
29 |
Correct |
421 ms |
2652 KB |
Output is correct |
30 |
Correct |
419 ms |
2648 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
3 ms |
348 KB |
Output is correct |
52 |
Correct |
3 ms |
348 KB |
Output is correct |
53 |
Correct |
3 ms |
468 KB |
Output is correct |
54 |
Correct |
3 ms |
480 KB |
Output is correct |
55 |
Correct |
3 ms |
348 KB |
Output is correct |
56 |
Correct |
3 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
348 KB |
Output is correct |
58 |
Correct |
3 ms |
348 KB |
Output is correct |
59 |
Correct |
3 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
348 KB |
Output is correct |
61 |
Correct |
488 ms |
2728 KB |
Output is correct |
62 |
Correct |
481 ms |
5608 KB |
Output is correct |
63 |
Correct |
445 ms |
5696 KB |
Output is correct |
64 |
Correct |
501 ms |
5720 KB |
Output is correct |
65 |
Correct |
502 ms |
5724 KB |
Output is correct |
66 |
Correct |
494 ms |
5804 KB |
Output is correct |
67 |
Correct |
486 ms |
5460 KB |
Output is correct |
68 |
Correct |
426 ms |
5808 KB |
Output is correct |
69 |
Correct |
429 ms |
5712 KB |
Output is correct |
70 |
Correct |
440 ms |
5676 KB |
Output is correct |