#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<ll, ll>
#define f first
#define s second
const int mxn = 300001;
int n, k;
ll a[mxn];
pi dp[mxn];
pi sol(ll x){
pi p = dp[0];
for(int i = 1; i <= n; i++){
dp[i] = dp[i - 1];
if(p.f + a[i] - x > dp[i].f) dp[i] = {p.f + a[i] - x, p.s + 1};
if(dp[i].f - a[i] > p.f) p = {dp[i].f - a[i], dp[i].s};
}
return dp[n];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1];
ll l = -1, r = 0x3f3f3f3f;
while(r - l > 1){
ll mid = (l + r) / 2;
if(sol(mid).s >= k) l = mid;
else r = mid;
}
pi p = sol(l);
cout << (p.f + k * l) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
7136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
7188 KB |
Output is correct |
2 |
Correct |
43 ms |
8476 KB |
Output is correct |
3 |
Correct |
48 ms |
8284 KB |
Output is correct |
4 |
Incorrect |
42 ms |
8260 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
94 ms |
7268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
7136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |