#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 10;
const long long oo = (long long)300000 * 1000000000;
const long double EPS = 1e-3;
int N, K;
int a[MAX];
pair<long double, int> dp[MAX][2];
bool OK(long double lambda) {
dp[0][0] = {0, 0}, dp[0][1] = {-oo, 0};
for (int i = 1; i <= N; ++i) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] =
max(make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second),
make_pair(dp[i - 1][0].first + a[i] - lambda,
dp[i - 1][0].second + 1));
}
return max(dp[N][0], dp[N][1]).second >= K;
};
int main(){
ios_base::sync_with_stdio(0);
cin >> N >> K;
for(int i=1; i<=N; i++){
cin >> a[i];
}
long double lower = 0, upper = oo;
while (lower + EPS < upper) {
long double mid = (lower + upper) / 2;
if (OK(mid)) {
lower = mid;
} else {
upper = mid;
}
}
long double lambda = lower;
OK(lambda);
cout << (long long)round(lambda * K + max(dp[N][0], dp[N][1]).first);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
21556 KB |
Output is correct |
2 |
Correct |
185 ms |
22000 KB |
Output is correct |
3 |
Correct |
196 ms |
22156 KB |
Output is correct |
4 |
Correct |
191 ms |
21984 KB |
Output is correct |
5 |
Correct |
186 ms |
21840 KB |
Output is correct |
6 |
Correct |
187 ms |
22004 KB |
Output is correct |
7 |
Correct |
191 ms |
21764 KB |
Output is correct |
8 |
Correct |
195 ms |
21836 KB |
Output is correct |
9 |
Correct |
196 ms |
21748 KB |
Output is correct |
10 |
Correct |
224 ms |
22032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
20988 KB |
Output is correct |
2 |
Correct |
180 ms |
21500 KB |
Output is correct |
3 |
Correct |
207 ms |
21328 KB |
Output is correct |
4 |
Correct |
187 ms |
21376 KB |
Output is correct |
5 |
Correct |
203 ms |
23036 KB |
Output is correct |
6 |
Correct |
196 ms |
21344 KB |
Output is correct |
7 |
Correct |
192 ms |
21368 KB |
Output is correct |
8 |
Correct |
201 ms |
23128 KB |
Output is correct |
9 |
Correct |
189 ms |
23020 KB |
Output is correct |
10 |
Correct |
198 ms |
21384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
22044 KB |
Output is correct |
2 |
Correct |
234 ms |
23224 KB |
Output is correct |
3 |
Correct |
246 ms |
23256 KB |
Output is correct |
4 |
Correct |
225 ms |
23216 KB |
Output is correct |
5 |
Correct |
242 ms |
23260 KB |
Output is correct |
6 |
Correct |
226 ms |
23276 KB |
Output is correct |
7 |
Correct |
235 ms |
23124 KB |
Output is correct |
8 |
Correct |
233 ms |
23260 KB |
Output is correct |
9 |
Correct |
224 ms |
23412 KB |
Output is correct |
10 |
Correct |
233 ms |
23356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
460 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 |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
460 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 |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
580 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
728 KB |
Output is correct |
28 |
Correct |
2 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
6 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
21556 KB |
Output is correct |
2 |
Correct |
185 ms |
22000 KB |
Output is correct |
3 |
Correct |
196 ms |
22156 KB |
Output is correct |
4 |
Correct |
191 ms |
21984 KB |
Output is correct |
5 |
Correct |
186 ms |
21840 KB |
Output is correct |
6 |
Correct |
187 ms |
22004 KB |
Output is correct |
7 |
Correct |
191 ms |
21764 KB |
Output is correct |
8 |
Correct |
195 ms |
21836 KB |
Output is correct |
9 |
Correct |
196 ms |
21748 KB |
Output is correct |
10 |
Correct |
224 ms |
22032 KB |
Output is correct |
11 |
Correct |
174 ms |
20988 KB |
Output is correct |
12 |
Correct |
180 ms |
21500 KB |
Output is correct |
13 |
Correct |
207 ms |
21328 KB |
Output is correct |
14 |
Correct |
187 ms |
21376 KB |
Output is correct |
15 |
Correct |
203 ms |
23036 KB |
Output is correct |
16 |
Correct |
196 ms |
21344 KB |
Output is correct |
17 |
Correct |
192 ms |
21368 KB |
Output is correct |
18 |
Correct |
201 ms |
23128 KB |
Output is correct |
19 |
Correct |
189 ms |
23020 KB |
Output is correct |
20 |
Correct |
198 ms |
21384 KB |
Output is correct |
21 |
Correct |
243 ms |
22044 KB |
Output is correct |
22 |
Correct |
234 ms |
23224 KB |
Output is correct |
23 |
Correct |
246 ms |
23256 KB |
Output is correct |
24 |
Correct |
225 ms |
23216 KB |
Output is correct |
25 |
Correct |
242 ms |
23260 KB |
Output is correct |
26 |
Correct |
226 ms |
23276 KB |
Output is correct |
27 |
Correct |
235 ms |
23124 KB |
Output is correct |
28 |
Correct |
233 ms |
23260 KB |
Output is correct |
29 |
Correct |
224 ms |
23412 KB |
Output is correct |
30 |
Correct |
233 ms |
23356 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
460 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 |
464 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 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 |
2 ms |
604 KB |
Output is correct |
52 |
Correct |
2 ms |
604 KB |
Output is correct |
53 |
Correct |
2 ms |
604 KB |
Output is correct |
54 |
Correct |
2 ms |
604 KB |
Output is correct |
55 |
Correct |
2 ms |
580 KB |
Output is correct |
56 |
Correct |
2 ms |
600 KB |
Output is correct |
57 |
Correct |
2 ms |
728 KB |
Output is correct |
58 |
Correct |
2 ms |
600 KB |
Output is correct |
59 |
Correct |
2 ms |
604 KB |
Output is correct |
60 |
Correct |
6 ms |
604 KB |
Output is correct |
61 |
Correct |
317 ms |
23216 KB |
Output is correct |
62 |
Correct |
297 ms |
23444 KB |
Output is correct |
63 |
Correct |
273 ms |
23208 KB |
Output is correct |
64 |
Correct |
306 ms |
23288 KB |
Output is correct |
65 |
Correct |
341 ms |
23284 KB |
Output is correct |
66 |
Correct |
335 ms |
23280 KB |
Output is correct |
67 |
Correct |
306 ms |
23216 KB |
Output is correct |
68 |
Correct |
231 ms |
23192 KB |
Output is correct |
69 |
Correct |
224 ms |
23028 KB |
Output is correct |
70 |
Correct |
226 ms |
23120 KB |
Output is correct |