#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), dp_open(n);
dp_closed[0] = {0, 0};
dp_open[0] = {max(0LL, -pfx[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-1].first - pfx[i], dp_closed[i-1].second});
}
return dp_closed[n-1];
}
void main_program(){
cin >> n >> k;
v.resize(n);
for (int i = 0; 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 |
178 ms |
14108 KB |
Output is correct |
2 |
Correct |
181 ms |
14508 KB |
Output is correct |
3 |
Correct |
183 ms |
14544 KB |
Output is correct |
4 |
Correct |
182 ms |
14476 KB |
Output is correct |
5 |
Correct |
192 ms |
14348 KB |
Output is correct |
6 |
Correct |
185 ms |
14132 KB |
Output is correct |
7 |
Correct |
173 ms |
14132 KB |
Output is correct |
8 |
Correct |
183 ms |
14368 KB |
Output is correct |
9 |
Correct |
185 ms |
14360 KB |
Output is correct |
10 |
Correct |
214 ms |
14312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
14264 KB |
Output is correct |
2 |
Correct |
170 ms |
15688 KB |
Output is correct |
3 |
Correct |
163 ms |
15296 KB |
Output is correct |
4 |
Correct |
174 ms |
15380 KB |
Output is correct |
5 |
Correct |
179 ms |
16916 KB |
Output is correct |
6 |
Correct |
179 ms |
15412 KB |
Output is correct |
7 |
Correct |
171 ms |
15716 KB |
Output is correct |
8 |
Correct |
205 ms |
17252 KB |
Output is correct |
9 |
Correct |
175 ms |
16840 KB |
Output is correct |
10 |
Correct |
167 ms |
15688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
189 ms |
14412 KB |
Output is correct |
2 |
Correct |
177 ms |
17148 KB |
Output is correct |
3 |
Correct |
197 ms |
17376 KB |
Output is correct |
4 |
Correct |
169 ms |
17136 KB |
Output is correct |
5 |
Correct |
173 ms |
17332 KB |
Output is correct |
6 |
Correct |
190 ms |
17400 KB |
Output is correct |
7 |
Correct |
183 ms |
17416 KB |
Output is correct |
8 |
Correct |
213 ms |
17240 KB |
Output is correct |
9 |
Correct |
187 ms |
17496 KB |
Output is correct |
10 |
Correct |
203 ms |
17408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
1 ms |
264 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
1 ms |
264 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
1 ms |
264 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
14108 KB |
Output is correct |
2 |
Correct |
181 ms |
14508 KB |
Output is correct |
3 |
Correct |
183 ms |
14544 KB |
Output is correct |
4 |
Correct |
182 ms |
14476 KB |
Output is correct |
5 |
Correct |
192 ms |
14348 KB |
Output is correct |
6 |
Correct |
185 ms |
14132 KB |
Output is correct |
7 |
Correct |
173 ms |
14132 KB |
Output is correct |
8 |
Correct |
183 ms |
14368 KB |
Output is correct |
9 |
Correct |
185 ms |
14360 KB |
Output is correct |
10 |
Correct |
214 ms |
14312 KB |
Output is correct |
11 |
Correct |
182 ms |
14264 KB |
Output is correct |
12 |
Correct |
170 ms |
15688 KB |
Output is correct |
13 |
Correct |
163 ms |
15296 KB |
Output is correct |
14 |
Correct |
174 ms |
15380 KB |
Output is correct |
15 |
Correct |
179 ms |
16916 KB |
Output is correct |
16 |
Correct |
179 ms |
15412 KB |
Output is correct |
17 |
Correct |
171 ms |
15716 KB |
Output is correct |
18 |
Correct |
205 ms |
17252 KB |
Output is correct |
19 |
Correct |
175 ms |
16840 KB |
Output is correct |
20 |
Correct |
167 ms |
15688 KB |
Output is correct |
21 |
Correct |
189 ms |
14412 KB |
Output is correct |
22 |
Correct |
177 ms |
17148 KB |
Output is correct |
23 |
Correct |
197 ms |
17376 KB |
Output is correct |
24 |
Correct |
169 ms |
17136 KB |
Output is correct |
25 |
Correct |
173 ms |
17332 KB |
Output is correct |
26 |
Correct |
190 ms |
17400 KB |
Output is correct |
27 |
Correct |
183 ms |
17416 KB |
Output is correct |
28 |
Correct |
213 ms |
17240 KB |
Output is correct |
29 |
Correct |
187 ms |
17496 KB |
Output is correct |
30 |
Correct |
203 ms |
17408 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
320 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 |
1 ms |
264 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
320 KB |
Output is correct |
39 |
Correct |
0 ms |
324 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |