#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 10;
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] = make_pair(0.0, 0);
dp[0][1] = make_pair(-1.18, 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].second, 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 lo = 0, hi = (long double)1e15;
while(lo + EPS < hi){
long double mid = (lo + hi) / 2;
if(OK(mid)){
lo = mid;
}
else{
hi = mid;
}
}
OK(lo);
cout << (long long)round((long long)lo * K + (long long)max(dp[N][0], dp[N][1]).first) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
20228 KB |
Output is correct |
2 |
Correct |
200 ms |
20308 KB |
Output is correct |
3 |
Correct |
188 ms |
20312 KB |
Output is correct |
4 |
Correct |
187 ms |
20056 KB |
Output is correct |
5 |
Correct |
196 ms |
20384 KB |
Output is correct |
6 |
Correct |
189 ms |
20228 KB |
Output is correct |
7 |
Correct |
184 ms |
20060 KB |
Output is correct |
8 |
Correct |
195 ms |
20268 KB |
Output is correct |
9 |
Correct |
190 ms |
20212 KB |
Output is correct |
10 |
Correct |
188 ms |
20248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
183 ms |
20060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
190 ms |
20256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
20228 KB |
Output is correct |
2 |
Correct |
200 ms |
20308 KB |
Output is correct |
3 |
Correct |
188 ms |
20312 KB |
Output is correct |
4 |
Correct |
187 ms |
20056 KB |
Output is correct |
5 |
Correct |
196 ms |
20384 KB |
Output is correct |
6 |
Correct |
189 ms |
20228 KB |
Output is correct |
7 |
Correct |
184 ms |
20060 KB |
Output is correct |
8 |
Correct |
195 ms |
20268 KB |
Output is correct |
9 |
Correct |
190 ms |
20212 KB |
Output is correct |
10 |
Correct |
188 ms |
20248 KB |
Output is correct |
11 |
Incorrect |
183 ms |
20060 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |