#include <bits/stdc++.h>
#define st first
#define nd second
using lint = int64_t;
constexpr int mod = int(1e9) + 7;
constexpr int inf = 0x3f3f3f3f;
constexpr int ninf = 0xcfcfcfcf;
constexpr lint linf = 0x3f3f3f3f3f3f3f3f;
const long double pi = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;
using dbl = long double;
const lint INF = 3.1e14;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, k;
cin>>n>>k;
vector<int>a(n);
for(int i=0;i<n;i++) cin>>a[i];
dbl lo = 0, hi = INF;
for(int z=0;z<100;z++){
dbl mid = (lo + hi) / 2;
vector<array<pair<dbl, int>, 2>>dp(n+1);
dp[0][0] = {0, 0}, dp[0][1] = {-INF, 0};
for(int i=0;i<n;i++){
dp[i+1][1] = max(make_pair(dp[i][1].st + a[i], dp[i][1].nd),
make_pair(dp[i][0].st + a[i] - mid, dp[i][0].nd + 1));
dp[i+1][0] = max(dp[i][0], dp[i][1]);
}
if(max(dp[n][0], dp[n][1]).nd >= k) lo = mid;
else hi = mid;
}
dbl mid = hi;
vector<array<dbl, 2>>dp(n+1);
dp[0][0] = 0, dp[0][1] = -INF;
for(int i=0;i<n;i++){
dp[i+1][1] = max(dp[i][1] + a[i], dp[i][0] + a[i] - mid);
dp[i+1][0] = max(dp[i][0], dp[i][1]);
}
cout<< lint(round(max(dp[n][0], dp[n][1]) + mid * k))<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
508 ms |
19652 KB |
Output is correct |
2 |
Correct |
512 ms |
19964 KB |
Output is correct |
3 |
Correct |
564 ms |
20300 KB |
Output is correct |
4 |
Correct |
555 ms |
20180 KB |
Output is correct |
5 |
Correct |
520 ms |
19976 KB |
Output is correct |
6 |
Correct |
516 ms |
19668 KB |
Output is correct |
7 |
Correct |
521 ms |
19664 KB |
Output is correct |
8 |
Correct |
528 ms |
20052 KB |
Output is correct |
9 |
Correct |
515 ms |
19768 KB |
Output is correct |
10 |
Correct |
520 ms |
19952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
585 ms |
19776 KB |
Output is correct |
2 |
Correct |
553 ms |
20172 KB |
Output is correct |
3 |
Correct |
529 ms |
19704 KB |
Output is correct |
4 |
Correct |
570 ms |
19916 KB |
Output is correct |
5 |
Correct |
563 ms |
19676 KB |
Output is correct |
6 |
Correct |
530 ms |
19668 KB |
Output is correct |
7 |
Correct |
598 ms |
20240 KB |
Output is correct |
8 |
Correct |
558 ms |
20108 KB |
Output is correct |
9 |
Correct |
557 ms |
19552 KB |
Output is correct |
10 |
Correct |
525 ms |
20088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
652 ms |
19988 KB |
Output is correct |
2 |
Correct |
625 ms |
19704 KB |
Output is correct |
3 |
Correct |
631 ms |
19996 KB |
Output is correct |
4 |
Correct |
612 ms |
19792 KB |
Output is correct |
5 |
Correct |
595 ms |
20000 KB |
Output is correct |
6 |
Correct |
603 ms |
20040 KB |
Output is correct |
7 |
Correct |
638 ms |
20200 KB |
Output is correct |
8 |
Correct |
601 ms |
19992 KB |
Output is correct |
9 |
Correct |
591 ms |
20172 KB |
Output is correct |
10 |
Correct |
586 ms |
20196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
344 KB |
Output is correct |
22 |
Correct |
5 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
5 ms |
340 KB |
Output is correct |
25 |
Correct |
6 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
6 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
508 ms |
19652 KB |
Output is correct |
2 |
Correct |
512 ms |
19964 KB |
Output is correct |
3 |
Correct |
564 ms |
20300 KB |
Output is correct |
4 |
Correct |
555 ms |
20180 KB |
Output is correct |
5 |
Correct |
520 ms |
19976 KB |
Output is correct |
6 |
Correct |
516 ms |
19668 KB |
Output is correct |
7 |
Correct |
521 ms |
19664 KB |
Output is correct |
8 |
Correct |
528 ms |
20052 KB |
Output is correct |
9 |
Correct |
515 ms |
19768 KB |
Output is correct |
10 |
Correct |
520 ms |
19952 KB |
Output is correct |
11 |
Correct |
585 ms |
19776 KB |
Output is correct |
12 |
Correct |
553 ms |
20172 KB |
Output is correct |
13 |
Correct |
529 ms |
19704 KB |
Output is correct |
14 |
Correct |
570 ms |
19916 KB |
Output is correct |
15 |
Correct |
563 ms |
19676 KB |
Output is correct |
16 |
Correct |
530 ms |
19668 KB |
Output is correct |
17 |
Correct |
598 ms |
20240 KB |
Output is correct |
18 |
Correct |
558 ms |
20108 KB |
Output is correct |
19 |
Correct |
557 ms |
19552 KB |
Output is correct |
20 |
Correct |
525 ms |
20088 KB |
Output is correct |
21 |
Correct |
652 ms |
19988 KB |
Output is correct |
22 |
Correct |
625 ms |
19704 KB |
Output is correct |
23 |
Correct |
631 ms |
19996 KB |
Output is correct |
24 |
Correct |
612 ms |
19792 KB |
Output is correct |
25 |
Correct |
595 ms |
20000 KB |
Output is correct |
26 |
Correct |
603 ms |
20040 KB |
Output is correct |
27 |
Correct |
638 ms |
20200 KB |
Output is correct |
28 |
Correct |
601 ms |
19992 KB |
Output is correct |
29 |
Correct |
591 ms |
20172 KB |
Output is correct |
30 |
Correct |
586 ms |
20196 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
5 ms |
344 KB |
Output is correct |
52 |
Correct |
5 ms |
340 KB |
Output is correct |
53 |
Correct |
4 ms |
340 KB |
Output is correct |
54 |
Correct |
5 ms |
340 KB |
Output is correct |
55 |
Correct |
6 ms |
340 KB |
Output is correct |
56 |
Correct |
4 ms |
340 KB |
Output is correct |
57 |
Correct |
6 ms |
340 KB |
Output is correct |
58 |
Correct |
3 ms |
340 KB |
Output is correct |
59 |
Correct |
4 ms |
340 KB |
Output is correct |
60 |
Correct |
3 ms |
340 KB |
Output is correct |
61 |
Correct |
779 ms |
19692 KB |
Output is correct |
62 |
Correct |
805 ms |
23312 KB |
Output is correct |
63 |
Correct |
657 ms |
22644 KB |
Output is correct |
64 |
Correct |
791 ms |
23116 KB |
Output is correct |
65 |
Correct |
786 ms |
23096 KB |
Output is correct |
66 |
Correct |
777 ms |
23012 KB |
Output is correct |
67 |
Correct |
776 ms |
22772 KB |
Output is correct |
68 |
Correct |
630 ms |
23184 KB |
Output is correct |
69 |
Correct |
578 ms |
22668 KB |
Output is correct |
70 |
Correct |
589 ms |
22584 KB |
Output is correct |