#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
#define rep(i, n) for(int i = 0; i<(int)n; ++i)
const int mxN = 3e5+5, M = 1e9+7;
int n, k;
ll a[mxN];
pair<long double, int> dp[2][mxN];
ll f(long double lambda){
dp[0][0] = {0, 0};
dp[1][0] = {a[0]-lambda, 1};
for(int i = 1; i<n; ++i){
dp[0][i] = max(dp[0][i-1], dp[1][i-1]);
dp[1][i] = max(make_pair(dp[0][i-1].first+a[i]-lambda, dp[0][i-1].second+1), make_pair(dp[1][i-1].first+a[i], dp[1][i-1].second));
}
return max(dp[0][n-1], dp[1][n-1]).second;
}
void solve(){
cin >> n >> k;
rep(i, n) cin >> a[i];
long double l = 0, r = 1e17;
rep(_, 100){
long double m = (l+r+1)/2;
if(f(m) < k){
r = m;
}else{
l = m;
}
}
long double lambda = l;
f(lambda);
cout << (ll)round(lambda * k + max(dp[0][n-1], dp[1][n-1]).first);
}
int main(){
#ifdef _DEBUG
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
20684 KB |
Output is correct |
2 |
Correct |
315 ms |
21176 KB |
Output is correct |
3 |
Correct |
323 ms |
21416 KB |
Output is correct |
4 |
Correct |
314 ms |
21280 KB |
Output is correct |
5 |
Correct |
328 ms |
21068 KB |
Output is correct |
6 |
Correct |
302 ms |
20812 KB |
Output is correct |
7 |
Correct |
378 ms |
20800 KB |
Output is correct |
8 |
Correct |
335 ms |
21220 KB |
Output is correct |
9 |
Correct |
366 ms |
20940 KB |
Output is correct |
10 |
Correct |
325 ms |
21068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
351 ms |
20940 KB |
Output is correct |
2 |
Correct |
320 ms |
21404 KB |
Output is correct |
3 |
Correct |
323 ms |
20820 KB |
Output is correct |
4 |
Correct |
347 ms |
21096 KB |
Output is correct |
5 |
Correct |
336 ms |
20788 KB |
Output is correct |
6 |
Correct |
364 ms |
20948 KB |
Output is correct |
7 |
Correct |
346 ms |
21436 KB |
Output is correct |
8 |
Correct |
394 ms |
21312 KB |
Output is correct |
9 |
Correct |
399 ms |
20760 KB |
Output is correct |
10 |
Correct |
378 ms |
21296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
21216 KB |
Output is correct |
2 |
Correct |
472 ms |
23932 KB |
Output is correct |
3 |
Correct |
446 ms |
24160 KB |
Output is correct |
4 |
Correct |
404 ms |
23884 KB |
Output is correct |
5 |
Correct |
436 ms |
24256 KB |
Output is correct |
6 |
Correct |
427 ms |
24264 KB |
Output is correct |
7 |
Correct |
418 ms |
24388 KB |
Output is correct |
8 |
Correct |
401 ms |
24140 KB |
Output is correct |
9 |
Correct |
410 ms |
24544 KB |
Output is correct |
10 |
Correct |
400 ms |
24368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
4 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
4 ms |
472 KB |
Output is correct |
26 |
Correct |
4 ms |
472 KB |
Output is correct |
27 |
Correct |
4 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
4 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
20684 KB |
Output is correct |
2 |
Correct |
315 ms |
21176 KB |
Output is correct |
3 |
Correct |
323 ms |
21416 KB |
Output is correct |
4 |
Correct |
314 ms |
21280 KB |
Output is correct |
5 |
Correct |
328 ms |
21068 KB |
Output is correct |
6 |
Correct |
302 ms |
20812 KB |
Output is correct |
7 |
Correct |
378 ms |
20800 KB |
Output is correct |
8 |
Correct |
335 ms |
21220 KB |
Output is correct |
9 |
Correct |
366 ms |
20940 KB |
Output is correct |
10 |
Correct |
325 ms |
21068 KB |
Output is correct |
11 |
Correct |
351 ms |
20940 KB |
Output is correct |
12 |
Correct |
320 ms |
21404 KB |
Output is correct |
13 |
Correct |
323 ms |
20820 KB |
Output is correct |
14 |
Correct |
347 ms |
21096 KB |
Output is correct |
15 |
Correct |
336 ms |
20788 KB |
Output is correct |
16 |
Correct |
364 ms |
20948 KB |
Output is correct |
17 |
Correct |
346 ms |
21436 KB |
Output is correct |
18 |
Correct |
394 ms |
21312 KB |
Output is correct |
19 |
Correct |
399 ms |
20760 KB |
Output is correct |
20 |
Correct |
378 ms |
21296 KB |
Output is correct |
21 |
Correct |
425 ms |
21216 KB |
Output is correct |
22 |
Correct |
472 ms |
23932 KB |
Output is correct |
23 |
Correct |
446 ms |
24160 KB |
Output is correct |
24 |
Correct |
404 ms |
23884 KB |
Output is correct |
25 |
Correct |
436 ms |
24256 KB |
Output is correct |
26 |
Correct |
427 ms |
24264 KB |
Output is correct |
27 |
Correct |
418 ms |
24388 KB |
Output is correct |
28 |
Correct |
401 ms |
24140 KB |
Output is correct |
29 |
Correct |
410 ms |
24544 KB |
Output is correct |
30 |
Correct |
400 ms |
24368 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
336 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
3 ms |
468 KB |
Output is correct |
52 |
Correct |
4 ms |
468 KB |
Output is correct |
53 |
Correct |
3 ms |
468 KB |
Output is correct |
54 |
Correct |
4 ms |
468 KB |
Output is correct |
55 |
Correct |
4 ms |
472 KB |
Output is correct |
56 |
Correct |
4 ms |
472 KB |
Output is correct |
57 |
Correct |
4 ms |
468 KB |
Output is correct |
58 |
Correct |
3 ms |
468 KB |
Output is correct |
59 |
Correct |
4 ms |
504 KB |
Output is correct |
60 |
Correct |
3 ms |
468 KB |
Output is correct |
61 |
Correct |
551 ms |
23900 KB |
Output is correct |
62 |
Correct |
543 ms |
24488 KB |
Output is correct |
63 |
Correct |
491 ms |
23832 KB |
Output is correct |
64 |
Correct |
591 ms |
24312 KB |
Output is correct |
65 |
Correct |
603 ms |
24192 KB |
Output is correct |
66 |
Correct |
607 ms |
24232 KB |
Output is correct |
67 |
Correct |
618 ms |
23868 KB |
Output is correct |
68 |
Correct |
497 ms |
24328 KB |
Output is correct |
69 |
Correct |
429 ms |
23852 KB |
Output is correct |
70 |
Correct |
421 ms |
23844 KB |
Output is correct |