#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int n,k; cin >> n >> k;
int a[n+1];
for (int i = 1; i <= n; cin >> a[i++]) {}
long long l = 0, r = 5e13, ans;
while (r > l) {
long long lambda = (l+r)/2;
long long dp[n+1][2];
int cnt[n+1][2];
dp[0][0] = 0;
cnt[0][0] = 0;
dp[0][1] = -1e18;
for (int i = 1; i <= n; i++) {
if (dp[i-1][0] > dp[i-1][1]) {
dp[i][0] = dp[i-1][0];
cnt[i][0] = cnt[i-1][0];
} else {
dp[i][0]= dp[i-1][1];
cnt[i][0] = cnt[i-1][1];
}
if (dp[i-1][0] - lambda > dp[i-1][1]) {
dp[i][1] = dp[i-1][0] - lambda + a[i];
cnt[i][1] = cnt[i-1][0] + 1;
} else {
dp[i][1] = dp[i-1][1] + a[i];
cnt[i][1] = cnt[i-1][1];
}
}
double friends;
if (dp[n][0] > dp[n][1]) {
ans = dp[n][0] + lambda*cnt[n][0];
friends = cnt[n][0];
} else {
ans = dp[n][1] + lambda*cnt[n][1];
friends = cnt[n][1];
}
if (friends == k) {
break;
}
if (friends > k) l = lambda+1;
else r = lambda-1;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8404 KB |
Output is correct |
2 |
Correct |
41 ms |
8528 KB |
Output is correct |
3 |
Correct |
50 ms |
8532 KB |
Output is correct |
4 |
Correct |
42 ms |
8528 KB |
Output is correct |
5 |
Correct |
52 ms |
8516 KB |
Output is correct |
6 |
Correct |
48 ms |
8272 KB |
Output is correct |
7 |
Correct |
41 ms |
8408 KB |
Output is correct |
8 |
Correct |
45 ms |
8556 KB |
Output is correct |
9 |
Correct |
41 ms |
8276 KB |
Output is correct |
10 |
Correct |
42 ms |
8280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
8284 KB |
Output is correct |
2 |
Correct |
24 ms |
8540 KB |
Output is correct |
3 |
Correct |
24 ms |
8280 KB |
Output is correct |
4 |
Correct |
58 ms |
8284 KB |
Output is correct |
5 |
Correct |
22 ms |
8280 KB |
Output is correct |
6 |
Correct |
23 ms |
8536 KB |
Output is correct |
7 |
Correct |
59 ms |
8644 KB |
Output is correct |
8 |
Correct |
23 ms |
8540 KB |
Output is correct |
9 |
Correct |
42 ms |
8232 KB |
Output is correct |
10 |
Correct |
35 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
8508 KB |
Output is correct |
2 |
Correct |
41 ms |
8284 KB |
Output is correct |
3 |
Correct |
34 ms |
8272 KB |
Output is correct |
4 |
Correct |
36 ms |
8276 KB |
Output is correct |
5 |
Correct |
35 ms |
8540 KB |
Output is correct |
6 |
Correct |
37 ms |
8516 KB |
Output is correct |
7 |
Correct |
37 ms |
8540 KB |
Output is correct |
8 |
Correct |
37 ms |
8508 KB |
Output is correct |
9 |
Correct |
35 ms |
8540 KB |
Output is correct |
10 |
Correct |
47 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8404 KB |
Output is correct |
2 |
Correct |
41 ms |
8528 KB |
Output is correct |
3 |
Correct |
50 ms |
8532 KB |
Output is correct |
4 |
Correct |
42 ms |
8528 KB |
Output is correct |
5 |
Correct |
52 ms |
8516 KB |
Output is correct |
6 |
Correct |
48 ms |
8272 KB |
Output is correct |
7 |
Correct |
41 ms |
8408 KB |
Output is correct |
8 |
Correct |
45 ms |
8556 KB |
Output is correct |
9 |
Correct |
41 ms |
8276 KB |
Output is correct |
10 |
Correct |
42 ms |
8280 KB |
Output is correct |
11 |
Correct |
36 ms |
8284 KB |
Output is correct |
12 |
Correct |
24 ms |
8540 KB |
Output is correct |
13 |
Correct |
24 ms |
8280 KB |
Output is correct |
14 |
Correct |
58 ms |
8284 KB |
Output is correct |
15 |
Correct |
22 ms |
8280 KB |
Output is correct |
16 |
Correct |
23 ms |
8536 KB |
Output is correct |
17 |
Correct |
59 ms |
8644 KB |
Output is correct |
18 |
Correct |
23 ms |
8540 KB |
Output is correct |
19 |
Correct |
42 ms |
8232 KB |
Output is correct |
20 |
Correct |
35 ms |
8540 KB |
Output is correct |
21 |
Correct |
34 ms |
8508 KB |
Output is correct |
22 |
Correct |
41 ms |
8284 KB |
Output is correct |
23 |
Correct |
34 ms |
8272 KB |
Output is correct |
24 |
Correct |
36 ms |
8276 KB |
Output is correct |
25 |
Correct |
35 ms |
8540 KB |
Output is correct |
26 |
Correct |
37 ms |
8516 KB |
Output is correct |
27 |
Correct |
37 ms |
8540 KB |
Output is correct |
28 |
Correct |
37 ms |
8508 KB |
Output is correct |
29 |
Correct |
35 ms |
8540 KB |
Output is correct |
30 |
Correct |
47 ms |
8540 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
344 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
71 ms |
8532 KB |
Output is correct |
62 |
Correct |
84 ms |
8540 KB |
Output is correct |
63 |
Correct |
55 ms |
8240 KB |
Output is correct |
64 |
Correct |
75 ms |
8520 KB |
Output is correct |
65 |
Correct |
63 ms |
8536 KB |
Output is correct |
66 |
Correct |
70 ms |
8516 KB |
Output is correct |
67 |
Correct |
87 ms |
8240 KB |
Output is correct |
68 |
Correct |
42 ms |
8528 KB |
Output is correct |
69 |
Correct |
42 ms |
8284 KB |
Output is correct |
70 |
Correct |
58 ms |
8228 KB |
Output is correct |