#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 = 3e14;
int final = 0;
while (l <= r){
int mid = (l + r) >> 1;
auto res = f(mid);
if (res.second <= k){
r = mid - 1;
final = max(final, res.first + mid * res.second);
}
else l = mid + 1;
}
cout << final << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
14092 KB |
Output is correct |
2 |
Correct |
230 ms |
17188 KB |
Output is correct |
3 |
Correct |
247 ms |
17484 KB |
Output is correct |
4 |
Correct |
235 ms |
17280 KB |
Output is correct |
5 |
Correct |
233 ms |
17204 KB |
Output is correct |
6 |
Correct |
222 ms |
16916 KB |
Output is correct |
7 |
Correct |
227 ms |
16896 KB |
Output is correct |
8 |
Correct |
227 ms |
17256 KB |
Output is correct |
9 |
Correct |
239 ms |
17016 KB |
Output is correct |
10 |
Correct |
241 ms |
17096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
224 ms |
14184 KB |
Output is correct |
2 |
Correct |
242 ms |
14536 KB |
Output is correct |
3 |
Correct |
249 ms |
14140 KB |
Output is correct |
4 |
Correct |
228 ms |
14288 KB |
Output is correct |
5 |
Correct |
242 ms |
16928 KB |
Output is correct |
6 |
Correct |
228 ms |
15280 KB |
Output is correct |
7 |
Correct |
251 ms |
15660 KB |
Output is correct |
8 |
Correct |
223 ms |
17336 KB |
Output is correct |
9 |
Correct |
221 ms |
16892 KB |
Output is correct |
10 |
Correct |
226 ms |
15572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
14340 KB |
Output is correct |
2 |
Correct |
231 ms |
14184 KB |
Output is correct |
3 |
Correct |
252 ms |
14356 KB |
Output is correct |
4 |
Correct |
231 ms |
14272 KB |
Output is correct |
5 |
Correct |
241 ms |
14292 KB |
Output is correct |
6 |
Correct |
241 ms |
14576 KB |
Output is correct |
7 |
Correct |
230 ms |
14408 KB |
Output is correct |
8 |
Correct |
219 ms |
14340 KB |
Output is correct |
9 |
Correct |
245 ms |
14604 KB |
Output is correct |
10 |
Correct |
268 ms |
14456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
356 KB |
Output is correct |
24 |
Correct |
2 ms |
340 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 |
420 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
1 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
14092 KB |
Output is correct |
2 |
Correct |
230 ms |
17188 KB |
Output is correct |
3 |
Correct |
247 ms |
17484 KB |
Output is correct |
4 |
Correct |
235 ms |
17280 KB |
Output is correct |
5 |
Correct |
233 ms |
17204 KB |
Output is correct |
6 |
Correct |
222 ms |
16916 KB |
Output is correct |
7 |
Correct |
227 ms |
16896 KB |
Output is correct |
8 |
Correct |
227 ms |
17256 KB |
Output is correct |
9 |
Correct |
239 ms |
17016 KB |
Output is correct |
10 |
Correct |
241 ms |
17096 KB |
Output is correct |
11 |
Correct |
224 ms |
14184 KB |
Output is correct |
12 |
Correct |
242 ms |
14536 KB |
Output is correct |
13 |
Correct |
249 ms |
14140 KB |
Output is correct |
14 |
Correct |
228 ms |
14288 KB |
Output is correct |
15 |
Correct |
242 ms |
16928 KB |
Output is correct |
16 |
Correct |
228 ms |
15280 KB |
Output is correct |
17 |
Correct |
251 ms |
15660 KB |
Output is correct |
18 |
Correct |
223 ms |
17336 KB |
Output is correct |
19 |
Correct |
221 ms |
16892 KB |
Output is correct |
20 |
Correct |
226 ms |
15572 KB |
Output is correct |
21 |
Correct |
255 ms |
14340 KB |
Output is correct |
22 |
Correct |
231 ms |
14184 KB |
Output is correct |
23 |
Correct |
252 ms |
14356 KB |
Output is correct |
24 |
Correct |
231 ms |
14272 KB |
Output is correct |
25 |
Correct |
241 ms |
14292 KB |
Output is correct |
26 |
Correct |
241 ms |
14576 KB |
Output is correct |
27 |
Correct |
230 ms |
14408 KB |
Output is correct |
28 |
Correct |
219 ms |
14340 KB |
Output is correct |
29 |
Correct |
245 ms |
14604 KB |
Output is correct |
30 |
Correct |
268 ms |
14456 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
324 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
324 KB |
Output is correct |
41 |
Correct |
1 ms |
324 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
316 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
324 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
320 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
2 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
356 KB |
Output is correct |
54 |
Correct |
2 ms |
340 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 |
420 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
328 KB |
Output is correct |
60 |
Correct |
1 ms |
356 KB |
Output is correct |
61 |
Correct |
221 ms |
17180 KB |
Output is correct |
62 |
Correct |
238 ms |
17540 KB |
Output is correct |
63 |
Correct |
238 ms |
17196 KB |
Output is correct |
64 |
Correct |
241 ms |
17460 KB |
Output is correct |
65 |
Correct |
247 ms |
17504 KB |
Output is correct |
66 |
Correct |
256 ms |
17412 KB |
Output is correct |
67 |
Correct |
254 ms |
17084 KB |
Output is correct |
68 |
Correct |
268 ms |
17548 KB |
Output is correct |
69 |
Correct |
253 ms |
17172 KB |
Output is correct |
70 |
Correct |
229 ms |
17076 KB |
Output is correct |