#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
#define int long long
using ii = pair<int, int>;
int n, k;
vector<int> v, pfx;
ii better(ii a, ii b){
if (a.first != b.first){
return (a.first > b.first) ? a : b;
}
return (a.second < b.second) ? a : b;
}
ii f(int lambda){
vector<ii> dp_closed(n+1), dp_open(n+1);
dp_closed[0] = {0, 0};
dp_open[0] = {0, 0};
for (int i = 1; i <= n; i++){
dp_closed[i] = better(dp_closed[i-1],
{dp_open[i-1].first + pfx[i] - lambda, dp_open[i-1].second + 1});
dp_open[i] = better(dp_open[i-1],
{dp_closed[i].first - pfx[i], dp_closed[i].second});
}
return dp_closed[n];
}
void main_program(){
cin >> n >> k;
v.resize(n+1);
for (int i = 1; i <= n; i++) cin >> v[i];
pfx = v;
for (int i = 1; i <= n; i++) pfx[i] += pfx[i-1];
int l = 0, r = 1e12;
while (l < r){
int mid = (l + r) >> 1;
auto res = f(mid);
if (res.second <= k) r = mid;
else l = mid + 1;
}
auto res = f(l);
cout << res.first + l * res.second << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
14112 KB |
Output is correct |
2 |
Correct |
219 ms |
14348 KB |
Output is correct |
3 |
Correct |
208 ms |
14536 KB |
Output is correct |
4 |
Correct |
197 ms |
14448 KB |
Output is correct |
5 |
Correct |
199 ms |
14460 KB |
Output is correct |
6 |
Correct |
198 ms |
14068 KB |
Output is correct |
7 |
Correct |
192 ms |
14064 KB |
Output is correct |
8 |
Correct |
207 ms |
14360 KB |
Output is correct |
9 |
Correct |
194 ms |
14140 KB |
Output is correct |
10 |
Correct |
195 ms |
14284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
189 ms |
14168 KB |
Output is correct |
2 |
Correct |
199 ms |
14744 KB |
Output is correct |
3 |
Correct |
192 ms |
14200 KB |
Output is correct |
4 |
Correct |
194 ms |
14308 KB |
Output is correct |
5 |
Correct |
219 ms |
14212 KB |
Output is correct |
6 |
Correct |
193 ms |
14188 KB |
Output is correct |
7 |
Correct |
197 ms |
14480 KB |
Output is correct |
8 |
Correct |
205 ms |
14432 KB |
Output is correct |
9 |
Correct |
206 ms |
14040 KB |
Output is correct |
10 |
Correct |
202 ms |
14568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
14344 KB |
Output is correct |
2 |
Correct |
204 ms |
14164 KB |
Output is correct |
3 |
Correct |
198 ms |
14356 KB |
Output is correct |
4 |
Correct |
202 ms |
14180 KB |
Output is correct |
5 |
Correct |
224 ms |
14336 KB |
Output is correct |
6 |
Correct |
219 ms |
14432 KB |
Output is correct |
7 |
Correct |
211 ms |
14464 KB |
Output is correct |
8 |
Correct |
224 ms |
14488 KB |
Output is correct |
9 |
Correct |
225 ms |
14468 KB |
Output is correct |
10 |
Correct |
216 ms |
14432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
14112 KB |
Output is correct |
2 |
Correct |
219 ms |
14348 KB |
Output is correct |
3 |
Correct |
208 ms |
14536 KB |
Output is correct |
4 |
Correct |
197 ms |
14448 KB |
Output is correct |
5 |
Correct |
199 ms |
14460 KB |
Output is correct |
6 |
Correct |
198 ms |
14068 KB |
Output is correct |
7 |
Correct |
192 ms |
14064 KB |
Output is correct |
8 |
Correct |
207 ms |
14360 KB |
Output is correct |
9 |
Correct |
194 ms |
14140 KB |
Output is correct |
10 |
Correct |
195 ms |
14284 KB |
Output is correct |
11 |
Correct |
189 ms |
14168 KB |
Output is correct |
12 |
Correct |
199 ms |
14744 KB |
Output is correct |
13 |
Correct |
192 ms |
14200 KB |
Output is correct |
14 |
Correct |
194 ms |
14308 KB |
Output is correct |
15 |
Correct |
219 ms |
14212 KB |
Output is correct |
16 |
Correct |
193 ms |
14188 KB |
Output is correct |
17 |
Correct |
197 ms |
14480 KB |
Output is correct |
18 |
Correct |
205 ms |
14432 KB |
Output is correct |
19 |
Correct |
206 ms |
14040 KB |
Output is correct |
20 |
Correct |
202 ms |
14568 KB |
Output is correct |
21 |
Correct |
205 ms |
14344 KB |
Output is correct |
22 |
Correct |
204 ms |
14164 KB |
Output is correct |
23 |
Correct |
198 ms |
14356 KB |
Output is correct |
24 |
Correct |
202 ms |
14180 KB |
Output is correct |
25 |
Correct |
224 ms |
14336 KB |
Output is correct |
26 |
Correct |
219 ms |
14432 KB |
Output is correct |
27 |
Correct |
211 ms |
14464 KB |
Output is correct |
28 |
Correct |
224 ms |
14488 KB |
Output is correct |
29 |
Correct |
225 ms |
14468 KB |
Output is correct |
30 |
Correct |
216 ms |
14432 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
2 ms |
340 KB |
Output is correct |
60 |
Correct |
2 ms |
340 KB |
Output is correct |
61 |
Correct |
197 ms |
14192 KB |
Output is correct |
62 |
Correct |
205 ms |
14508 KB |
Output is correct |
63 |
Correct |
221 ms |
14120 KB |
Output is correct |
64 |
Correct |
193 ms |
14428 KB |
Output is correct |
65 |
Correct |
207 ms |
14356 KB |
Output is correct |
66 |
Correct |
222 ms |
14388 KB |
Output is correct |
67 |
Correct |
198 ms |
14168 KB |
Output is correct |
68 |
Correct |
196 ms |
14420 KB |
Output is correct |
69 |
Correct |
197 ms |
14136 KB |
Output is correct |
70 |
Correct |
230 ms |
14044 KB |
Output is correct |