#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int a[300300], n;
ll qs[300300];
ll dp[300300];
int cnt[300300];
void cal(ll mid) {
pair<ll, int> mx(0ll, 0);
for (int i = 1;i <= n;i++) {
dp[i] = dp[i - 1];
cnt[i] = cnt[i - 1];
if (qs[i] + mx.first - mid > dp[i]) {
dp[i] = qs[i] + mx.first - mid;
cnt[i] = mx.second + 1;
}
mx = max(mx, { dp[i] - qs[i], cnt[i] });
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int k;
cin >> n >> k;
for (int i = 1;i <= n;i++) {
cin >> a[i];
qs[i] = qs[i - 1] + a[i];
}
ll l = -1e13, r = 1e13;
while (l < r) {
ll mid = l + (r - l + 1) / 2;
cal(mid);
if (cnt[n] >= k) l = mid;
else r = mid - 1;
}
cal(l);
cout << dp[n] + l * k;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
10068 KB |
Output is correct |
2 |
Correct |
52 ms |
10268 KB |
Output is correct |
3 |
Correct |
50 ms |
10140 KB |
Output is correct |
4 |
Correct |
53 ms |
10312 KB |
Output is correct |
5 |
Correct |
56 ms |
10068 KB |
Output is correct |
6 |
Correct |
70 ms |
10068 KB |
Output is correct |
7 |
Correct |
59 ms |
9964 KB |
Output is correct |
8 |
Correct |
57 ms |
10148 KB |
Output is correct |
9 |
Correct |
58 ms |
10172 KB |
Output is correct |
10 |
Correct |
62 ms |
10032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
8504 KB |
Output is correct |
2 |
Correct |
54 ms |
8452 KB |
Output is correct |
3 |
Correct |
46 ms |
8488 KB |
Output is correct |
4 |
Correct |
48 ms |
8528 KB |
Output is correct |
5 |
Correct |
59 ms |
10064 KB |
Output is correct |
6 |
Correct |
52 ms |
8280 KB |
Output is correct |
7 |
Correct |
47 ms |
8628 KB |
Output is correct |
8 |
Correct |
55 ms |
10332 KB |
Output is correct |
9 |
Correct |
58 ms |
10136 KB |
Output is correct |
10 |
Correct |
47 ms |
8612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
10464 KB |
Output is correct |
2 |
Correct |
75 ms |
10372 KB |
Output is correct |
3 |
Correct |
69 ms |
10336 KB |
Output is correct |
4 |
Correct |
70 ms |
10368 KB |
Output is correct |
5 |
Correct |
71 ms |
10320 KB |
Output is correct |
6 |
Correct |
69 ms |
10488 KB |
Output is correct |
7 |
Correct |
80 ms |
10324 KB |
Output is correct |
8 |
Correct |
73 ms |
10288 KB |
Output is correct |
9 |
Correct |
71 ms |
10324 KB |
Output is correct |
10 |
Correct |
75 ms |
10308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
10068 KB |
Output is correct |
2 |
Correct |
52 ms |
10268 KB |
Output is correct |
3 |
Correct |
50 ms |
10140 KB |
Output is correct |
4 |
Correct |
53 ms |
10312 KB |
Output is correct |
5 |
Correct |
56 ms |
10068 KB |
Output is correct |
6 |
Correct |
70 ms |
10068 KB |
Output is correct |
7 |
Correct |
59 ms |
9964 KB |
Output is correct |
8 |
Correct |
57 ms |
10148 KB |
Output is correct |
9 |
Correct |
58 ms |
10172 KB |
Output is correct |
10 |
Correct |
62 ms |
10032 KB |
Output is correct |
11 |
Correct |
46 ms |
8504 KB |
Output is correct |
12 |
Correct |
54 ms |
8452 KB |
Output is correct |
13 |
Correct |
46 ms |
8488 KB |
Output is correct |
14 |
Correct |
48 ms |
8528 KB |
Output is correct |
15 |
Correct |
59 ms |
10064 KB |
Output is correct |
16 |
Correct |
52 ms |
8280 KB |
Output is correct |
17 |
Correct |
47 ms |
8628 KB |
Output is correct |
18 |
Correct |
55 ms |
10332 KB |
Output is correct |
19 |
Correct |
58 ms |
10136 KB |
Output is correct |
20 |
Correct |
47 ms |
8612 KB |
Output is correct |
21 |
Correct |
72 ms |
10464 KB |
Output is correct |
22 |
Correct |
75 ms |
10372 KB |
Output is correct |
23 |
Correct |
69 ms |
10336 KB |
Output is correct |
24 |
Correct |
70 ms |
10368 KB |
Output is correct |
25 |
Correct |
71 ms |
10320 KB |
Output is correct |
26 |
Correct |
69 ms |
10488 KB |
Output is correct |
27 |
Correct |
80 ms |
10324 KB |
Output is correct |
28 |
Correct |
73 ms |
10288 KB |
Output is correct |
29 |
Correct |
71 ms |
10324 KB |
Output is correct |
30 |
Correct |
75 ms |
10308 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |