#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 - l) / 2;
if(sol(mid).s <= k) r = mid;
else l = mid;
}
pi p = sol(r);
cout << (p.f + k * r) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
7120 KB |
Output is correct |
2 |
Correct |
57 ms |
7200 KB |
Output is correct |
3 |
Correct |
44 ms |
7244 KB |
Output is correct |
4 |
Correct |
43 ms |
7204 KB |
Output is correct |
5 |
Correct |
48 ms |
7248 KB |
Output is correct |
6 |
Correct |
42 ms |
7128 KB |
Output is correct |
7 |
Correct |
63 ms |
7028 KB |
Output is correct |
8 |
Correct |
44 ms |
7276 KB |
Output is correct |
9 |
Correct |
46 ms |
7144 KB |
Output is correct |
10 |
Correct |
43 ms |
7124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
7132 KB |
Output is correct |
2 |
Correct |
39 ms |
7336 KB |
Output is correct |
3 |
Correct |
36 ms |
7188 KB |
Output is correct |
4 |
Correct |
38 ms |
7284 KB |
Output is correct |
5 |
Correct |
43 ms |
7072 KB |
Output is correct |
6 |
Correct |
37 ms |
7168 KB |
Output is correct |
7 |
Correct |
38 ms |
7252 KB |
Output is correct |
8 |
Correct |
43 ms |
7252 KB |
Output is correct |
9 |
Correct |
43 ms |
7116 KB |
Output is correct |
10 |
Correct |
35 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
101 ms |
7300 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 |
Correct |
60 ms |
7120 KB |
Output is correct |
2 |
Correct |
57 ms |
7200 KB |
Output is correct |
3 |
Correct |
44 ms |
7244 KB |
Output is correct |
4 |
Correct |
43 ms |
7204 KB |
Output is correct |
5 |
Correct |
48 ms |
7248 KB |
Output is correct |
6 |
Correct |
42 ms |
7128 KB |
Output is correct |
7 |
Correct |
63 ms |
7028 KB |
Output is correct |
8 |
Correct |
44 ms |
7276 KB |
Output is correct |
9 |
Correct |
46 ms |
7144 KB |
Output is correct |
10 |
Correct |
43 ms |
7124 KB |
Output is correct |
11 |
Correct |
42 ms |
7132 KB |
Output is correct |
12 |
Correct |
39 ms |
7336 KB |
Output is correct |
13 |
Correct |
36 ms |
7188 KB |
Output is correct |
14 |
Correct |
38 ms |
7284 KB |
Output is correct |
15 |
Correct |
43 ms |
7072 KB |
Output is correct |
16 |
Correct |
37 ms |
7168 KB |
Output is correct |
17 |
Correct |
38 ms |
7252 KB |
Output is correct |
18 |
Correct |
43 ms |
7252 KB |
Output is correct |
19 |
Correct |
43 ms |
7116 KB |
Output is correct |
20 |
Correct |
35 ms |
7252 KB |
Output is correct |
21 |
Incorrect |
101 ms |
7300 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |