#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 = 1e16, 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 |
46 ms |
8272 KB |
Output is correct |
2 |
Correct |
48 ms |
8532 KB |
Output is correct |
3 |
Correct |
46 ms |
8532 KB |
Output is correct |
4 |
Correct |
45 ms |
8532 KB |
Output is correct |
5 |
Correct |
46 ms |
8540 KB |
Output is correct |
6 |
Correct |
45 ms |
8280 KB |
Output is correct |
7 |
Correct |
44 ms |
8236 KB |
Output is correct |
8 |
Correct |
52 ms |
8532 KB |
Output is correct |
9 |
Correct |
44 ms |
8284 KB |
Output is correct |
10 |
Correct |
45 ms |
8512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1081 ms |
8284 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1059 ms |
8536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1087 ms |
348 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1087 ms |
348 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1087 ms |
348 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
8272 KB |
Output is correct |
2 |
Correct |
48 ms |
8532 KB |
Output is correct |
3 |
Correct |
46 ms |
8532 KB |
Output is correct |
4 |
Correct |
45 ms |
8532 KB |
Output is correct |
5 |
Correct |
46 ms |
8540 KB |
Output is correct |
6 |
Correct |
45 ms |
8280 KB |
Output is correct |
7 |
Correct |
44 ms |
8236 KB |
Output is correct |
8 |
Correct |
52 ms |
8532 KB |
Output is correct |
9 |
Correct |
44 ms |
8284 KB |
Output is correct |
10 |
Correct |
45 ms |
8512 KB |
Output is correct |
11 |
Execution timed out |
1081 ms |
8284 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |