#include <bits/stdc++.h>
using namespace std;
#define int int64_t
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n,k; cin >> n >> k;
vector<int> a(n); for(int& v : a) cin >> v;
partial_sum(a.begin(),a.end(),a.begin());
auto go = [&](int c){
vector<int> dp(n+1,-1e18),cnt(n+1);
dp[0] = 0;
pair<int,int> mx = {0,0};
pair<int,int> bst = {-1e18,0};
for(int i = 1; i <= n; i++){
dp[i] = dp[i-1]; cnt[i] = cnt[i-1];
auto[adj, cntj] = mx; cntj *= -1;
if(adj-c+a[i-1] > dp[i]){
dp[i] = adj-c+a[i-1];
cnt[i] = cntj+1;
}
mx = max(mx,{dp[i]-a[i-1],-cnt[i]});
bst = max(bst,{dp[i],-cnt[i]});
}
bst.second *= -1;
bst.first += c*bst.second;
return bst;
};
/*
for(int i = 0; i <= 10; i++){
auto[res,cnt] = go(i);
cout << i << ": " << cnt << " " << res << "\n";
}*/
int lo = 0, hi = 1e12;
while(lo < hi){
int mid = (lo+hi)/2;
auto[res,cnt] = go(mid);
if(cnt <= k) hi = mid;
else lo = mid+1;
}
cout << go(lo).first << "\n";
}
// dp[i] = max{mx[j]+psa[i]-psa[j]-c}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
7212 KB |
Output is correct |
2 |
Correct |
117 ms |
7372 KB |
Output is correct |
3 |
Correct |
118 ms |
7572 KB |
Output is correct |
4 |
Correct |
119 ms |
7412 KB |
Output is correct |
5 |
Correct |
137 ms |
7556 KB |
Output is correct |
6 |
Correct |
115 ms |
7236 KB |
Output is correct |
7 |
Correct |
115 ms |
7240 KB |
Output is correct |
8 |
Correct |
127 ms |
7380 KB |
Output is correct |
9 |
Correct |
128 ms |
7380 KB |
Output is correct |
10 |
Correct |
121 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
7296 KB |
Output is correct |
2 |
Correct |
114 ms |
7480 KB |
Output is correct |
3 |
Correct |
107 ms |
7252 KB |
Output is correct |
4 |
Correct |
111 ms |
7324 KB |
Output is correct |
5 |
Correct |
121 ms |
7264 KB |
Output is correct |
6 |
Correct |
105 ms |
7304 KB |
Output is correct |
7 |
Correct |
110 ms |
7524 KB |
Output is correct |
8 |
Correct |
131 ms |
7576 KB |
Output is correct |
9 |
Correct |
126 ms |
7472 KB |
Output is correct |
10 |
Correct |
113 ms |
7428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
7388 KB |
Output is correct |
2 |
Correct |
155 ms |
10304 KB |
Output is correct |
3 |
Correct |
156 ms |
10332 KB |
Output is correct |
4 |
Correct |
147 ms |
10236 KB |
Output is correct |
5 |
Correct |
140 ms |
10332 KB |
Output is correct |
6 |
Correct |
147 ms |
10412 KB |
Output is correct |
7 |
Correct |
166 ms |
10408 KB |
Output is correct |
8 |
Correct |
139 ms |
10396 KB |
Output is correct |
9 |
Correct |
141 ms |
10504 KB |
Output is correct |
10 |
Correct |
140 ms |
10452 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
7212 KB |
Output is correct |
2 |
Correct |
117 ms |
7372 KB |
Output is correct |
3 |
Correct |
118 ms |
7572 KB |
Output is correct |
4 |
Correct |
119 ms |
7412 KB |
Output is correct |
5 |
Correct |
137 ms |
7556 KB |
Output is correct |
6 |
Correct |
115 ms |
7236 KB |
Output is correct |
7 |
Correct |
115 ms |
7240 KB |
Output is correct |
8 |
Correct |
127 ms |
7380 KB |
Output is correct |
9 |
Correct |
128 ms |
7380 KB |
Output is correct |
10 |
Correct |
121 ms |
7416 KB |
Output is correct |
11 |
Correct |
115 ms |
7296 KB |
Output is correct |
12 |
Correct |
114 ms |
7480 KB |
Output is correct |
13 |
Correct |
107 ms |
7252 KB |
Output is correct |
14 |
Correct |
111 ms |
7324 KB |
Output is correct |
15 |
Correct |
121 ms |
7264 KB |
Output is correct |
16 |
Correct |
105 ms |
7304 KB |
Output is correct |
17 |
Correct |
110 ms |
7524 KB |
Output is correct |
18 |
Correct |
131 ms |
7576 KB |
Output is correct |
19 |
Correct |
126 ms |
7472 KB |
Output is correct |
20 |
Correct |
113 ms |
7428 KB |
Output is correct |
21 |
Correct |
144 ms |
7388 KB |
Output is correct |
22 |
Correct |
155 ms |
10304 KB |
Output is correct |
23 |
Correct |
156 ms |
10332 KB |
Output is correct |
24 |
Correct |
147 ms |
10236 KB |
Output is correct |
25 |
Correct |
140 ms |
10332 KB |
Output is correct |
26 |
Correct |
147 ms |
10412 KB |
Output is correct |
27 |
Correct |
166 ms |
10408 KB |
Output is correct |
28 |
Correct |
139 ms |
10396 KB |
Output is correct |
29 |
Correct |
141 ms |
10504 KB |
Output is correct |
30 |
Correct |
140 ms |
10452 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 |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
316 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
186 ms |
10276 KB |
Output is correct |
62 |
Correct |
199 ms |
10592 KB |
Output is correct |
63 |
Correct |
152 ms |
10200 KB |
Output is correct |
64 |
Correct |
184 ms |
10440 KB |
Output is correct |
65 |
Correct |
196 ms |
10368 KB |
Output is correct |
66 |
Correct |
183 ms |
10340 KB |
Output is correct |
67 |
Correct |
191 ms |
10368 KB |
Output is correct |
68 |
Correct |
172 ms |
10420 KB |
Output is correct |
69 |
Correct |
138 ms |
10228 KB |
Output is correct |
70 |
Correct |
136 ms |
10176 KB |
Output is correct |