#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 3e5 + 11;
int a[MAXN], dp[MAXN], dp2[MAXN], c[MAXN], c2[MAXN];
int32_t main(){
int n, k; cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> a[i];
}
auto test = [&n](int C) {
fill(dp, dp + MAXN, -(1LL << 60));
fill(dp2, dp2 + MAXN, -(1LL << 60));
fill(c, c + MAXN, 0);
fill(c2, c2 + MAXN, 0);
dp[0] = -(1LL << 60), dp2[0] = 0;
for(int j = 0; j < n; j++){
if(dp2[j - 1] + a[j] - C >= dp[j] + a[j]){
dp[j + 1] = dp2[j - 1] + a[j] - C;
c[j + 1] = c2[j - 1] + 1;
}else{
dp[j + 1] = dp[j] + a[j];
c[j + 1] = c[j];
}
if(dp2[j] >= dp[j + 1]){
dp2[j + 1] = dp2[j];
c2[j + 1] = c2[j];
}else{
dp2[j + 1] = dp[j + 1];
c2[j + 1] = c[j + 1];
}
}
};
int l = 0, r = (1LL << 60);
while(l + 1 != r){
int mid = (l + r) / 2;
test(mid);
if(c2[n] > k){
l = mid;
}else{
r = mid;
}
}
test(l); int v1 = dp2[n] + l * c2[n], k1 = c2[n];
test(r); int v2 = dp2[n] + r * c2[n], k2 = c2[n];
cout << v1 + (k1 != k2 ? (v2 - v1) / (k2 - k1) * (k - k1) : 0) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
11948 KB |
Output is correct |
2 |
Correct |
212 ms |
11932 KB |
Output is correct |
3 |
Correct |
205 ms |
12020 KB |
Output is correct |
4 |
Correct |
199 ms |
12004 KB |
Output is correct |
5 |
Correct |
222 ms |
11964 KB |
Output is correct |
6 |
Correct |
201 ms |
11920 KB |
Output is correct |
7 |
Correct |
201 ms |
11868 KB |
Output is correct |
8 |
Correct |
208 ms |
12004 KB |
Output is correct |
9 |
Correct |
199 ms |
11884 KB |
Output is correct |
10 |
Correct |
214 ms |
12012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
12044 KB |
Output is correct |
2 |
Correct |
191 ms |
12012 KB |
Output is correct |
3 |
Correct |
145 ms |
11876 KB |
Output is correct |
4 |
Correct |
159 ms |
11984 KB |
Output is correct |
5 |
Correct |
202 ms |
11952 KB |
Output is correct |
6 |
Correct |
154 ms |
11856 KB |
Output is correct |
7 |
Correct |
159 ms |
12036 KB |
Output is correct |
8 |
Correct |
230 ms |
12016 KB |
Output is correct |
9 |
Correct |
202 ms |
11860 KB |
Output is correct |
10 |
Correct |
161 ms |
11900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
230 ms |
11996 KB |
Output is correct |
2 |
Correct |
249 ms |
14868 KB |
Output is correct |
3 |
Correct |
231 ms |
15032 KB |
Output is correct |
4 |
Correct |
228 ms |
15052 KB |
Output is correct |
5 |
Correct |
224 ms |
14988 KB |
Output is correct |
6 |
Correct |
229 ms |
15052 KB |
Output is correct |
7 |
Correct |
238 ms |
15156 KB |
Output is correct |
8 |
Correct |
240 ms |
14924 KB |
Output is correct |
9 |
Correct |
245 ms |
15100 KB |
Output is correct |
10 |
Correct |
235 ms |
15056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
9684 KB |
Output is correct |
2 |
Correct |
81 ms |
9684 KB |
Output is correct |
3 |
Correct |
65 ms |
9684 KB |
Output is correct |
4 |
Correct |
58 ms |
9684 KB |
Output is correct |
5 |
Correct |
69 ms |
9684 KB |
Output is correct |
6 |
Correct |
64 ms |
9684 KB |
Output is correct |
7 |
Correct |
60 ms |
9688 KB |
Output is correct |
8 |
Correct |
69 ms |
9688 KB |
Output is correct |
9 |
Correct |
67 ms |
9684 KB |
Output is correct |
10 |
Correct |
63 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
9684 KB |
Output is correct |
2 |
Correct |
81 ms |
9684 KB |
Output is correct |
3 |
Correct |
65 ms |
9684 KB |
Output is correct |
4 |
Correct |
58 ms |
9684 KB |
Output is correct |
5 |
Correct |
69 ms |
9684 KB |
Output is correct |
6 |
Correct |
64 ms |
9684 KB |
Output is correct |
7 |
Correct |
60 ms |
9688 KB |
Output is correct |
8 |
Correct |
69 ms |
9688 KB |
Output is correct |
9 |
Correct |
67 ms |
9684 KB |
Output is correct |
10 |
Correct |
63 ms |
9684 KB |
Output is correct |
11 |
Correct |
56 ms |
9688 KB |
Output is correct |
12 |
Correct |
57 ms |
9680 KB |
Output is correct |
13 |
Correct |
55 ms |
9680 KB |
Output is correct |
14 |
Correct |
67 ms |
9684 KB |
Output is correct |
15 |
Correct |
54 ms |
9684 KB |
Output is correct |
16 |
Correct |
57 ms |
9684 KB |
Output is correct |
17 |
Correct |
52 ms |
9684 KB |
Output is correct |
18 |
Correct |
57 ms |
9684 KB |
Output is correct |
19 |
Correct |
62 ms |
9684 KB |
Output is correct |
20 |
Correct |
66 ms |
9692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
9684 KB |
Output is correct |
2 |
Correct |
81 ms |
9684 KB |
Output is correct |
3 |
Correct |
65 ms |
9684 KB |
Output is correct |
4 |
Correct |
58 ms |
9684 KB |
Output is correct |
5 |
Correct |
69 ms |
9684 KB |
Output is correct |
6 |
Correct |
64 ms |
9684 KB |
Output is correct |
7 |
Correct |
60 ms |
9688 KB |
Output is correct |
8 |
Correct |
69 ms |
9688 KB |
Output is correct |
9 |
Correct |
67 ms |
9684 KB |
Output is correct |
10 |
Correct |
63 ms |
9684 KB |
Output is correct |
11 |
Correct |
56 ms |
9688 KB |
Output is correct |
12 |
Correct |
57 ms |
9680 KB |
Output is correct |
13 |
Correct |
55 ms |
9680 KB |
Output is correct |
14 |
Correct |
67 ms |
9684 KB |
Output is correct |
15 |
Correct |
54 ms |
9684 KB |
Output is correct |
16 |
Correct |
57 ms |
9684 KB |
Output is correct |
17 |
Correct |
52 ms |
9684 KB |
Output is correct |
18 |
Correct |
57 ms |
9684 KB |
Output is correct |
19 |
Correct |
62 ms |
9684 KB |
Output is correct |
20 |
Correct |
66 ms |
9692 KB |
Output is correct |
21 |
Correct |
56 ms |
9712 KB |
Output is correct |
22 |
Correct |
57 ms |
9684 KB |
Output is correct |
23 |
Correct |
64 ms |
9716 KB |
Output is correct |
24 |
Correct |
61 ms |
9712 KB |
Output is correct |
25 |
Correct |
63 ms |
9732 KB |
Output is correct |
26 |
Correct |
54 ms |
9684 KB |
Output is correct |
27 |
Correct |
66 ms |
9804 KB |
Output is correct |
28 |
Correct |
58 ms |
9684 KB |
Output is correct |
29 |
Correct |
53 ms |
9660 KB |
Output is correct |
30 |
Correct |
56 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
11948 KB |
Output is correct |
2 |
Correct |
212 ms |
11932 KB |
Output is correct |
3 |
Correct |
205 ms |
12020 KB |
Output is correct |
4 |
Correct |
199 ms |
12004 KB |
Output is correct |
5 |
Correct |
222 ms |
11964 KB |
Output is correct |
6 |
Correct |
201 ms |
11920 KB |
Output is correct |
7 |
Correct |
201 ms |
11868 KB |
Output is correct |
8 |
Correct |
208 ms |
12004 KB |
Output is correct |
9 |
Correct |
199 ms |
11884 KB |
Output is correct |
10 |
Correct |
214 ms |
12012 KB |
Output is correct |
11 |
Correct |
148 ms |
12044 KB |
Output is correct |
12 |
Correct |
191 ms |
12012 KB |
Output is correct |
13 |
Correct |
145 ms |
11876 KB |
Output is correct |
14 |
Correct |
159 ms |
11984 KB |
Output is correct |
15 |
Correct |
202 ms |
11952 KB |
Output is correct |
16 |
Correct |
154 ms |
11856 KB |
Output is correct |
17 |
Correct |
159 ms |
12036 KB |
Output is correct |
18 |
Correct |
230 ms |
12016 KB |
Output is correct |
19 |
Correct |
202 ms |
11860 KB |
Output is correct |
20 |
Correct |
161 ms |
11900 KB |
Output is correct |
21 |
Correct |
230 ms |
11996 KB |
Output is correct |
22 |
Correct |
249 ms |
14868 KB |
Output is correct |
23 |
Correct |
231 ms |
15032 KB |
Output is correct |
24 |
Correct |
228 ms |
15052 KB |
Output is correct |
25 |
Correct |
224 ms |
14988 KB |
Output is correct |
26 |
Correct |
229 ms |
15052 KB |
Output is correct |
27 |
Correct |
238 ms |
15156 KB |
Output is correct |
28 |
Correct |
240 ms |
14924 KB |
Output is correct |
29 |
Correct |
245 ms |
15100 KB |
Output is correct |
30 |
Correct |
235 ms |
15056 KB |
Output is correct |
31 |
Correct |
57 ms |
9684 KB |
Output is correct |
32 |
Correct |
81 ms |
9684 KB |
Output is correct |
33 |
Correct |
65 ms |
9684 KB |
Output is correct |
34 |
Correct |
58 ms |
9684 KB |
Output is correct |
35 |
Correct |
69 ms |
9684 KB |
Output is correct |
36 |
Correct |
64 ms |
9684 KB |
Output is correct |
37 |
Correct |
60 ms |
9688 KB |
Output is correct |
38 |
Correct |
69 ms |
9688 KB |
Output is correct |
39 |
Correct |
67 ms |
9684 KB |
Output is correct |
40 |
Correct |
63 ms |
9684 KB |
Output is correct |
41 |
Correct |
56 ms |
9688 KB |
Output is correct |
42 |
Correct |
57 ms |
9680 KB |
Output is correct |
43 |
Correct |
55 ms |
9680 KB |
Output is correct |
44 |
Correct |
67 ms |
9684 KB |
Output is correct |
45 |
Correct |
54 ms |
9684 KB |
Output is correct |
46 |
Correct |
57 ms |
9684 KB |
Output is correct |
47 |
Correct |
52 ms |
9684 KB |
Output is correct |
48 |
Correct |
57 ms |
9684 KB |
Output is correct |
49 |
Correct |
62 ms |
9684 KB |
Output is correct |
50 |
Correct |
66 ms |
9692 KB |
Output is correct |
51 |
Correct |
56 ms |
9712 KB |
Output is correct |
52 |
Correct |
57 ms |
9684 KB |
Output is correct |
53 |
Correct |
64 ms |
9716 KB |
Output is correct |
54 |
Correct |
61 ms |
9712 KB |
Output is correct |
55 |
Correct |
63 ms |
9732 KB |
Output is correct |
56 |
Correct |
54 ms |
9684 KB |
Output is correct |
57 |
Correct |
66 ms |
9804 KB |
Output is correct |
58 |
Correct |
58 ms |
9684 KB |
Output is correct |
59 |
Correct |
53 ms |
9660 KB |
Output is correct |
60 |
Correct |
56 ms |
9708 KB |
Output is correct |
61 |
Correct |
276 ms |
14924 KB |
Output is correct |
62 |
Correct |
274 ms |
15044 KB |
Output is correct |
63 |
Correct |
250 ms |
14936 KB |
Output is correct |
64 |
Correct |
282 ms |
15108 KB |
Output is correct |
65 |
Correct |
273 ms |
15028 KB |
Output is correct |
66 |
Correct |
265 ms |
14928 KB |
Output is correct |
67 |
Correct |
288 ms |
14876 KB |
Output is correct |
68 |
Correct |
228 ms |
15040 KB |
Output is correct |
69 |
Correct |
226 ms |
14932 KB |
Output is correct |
70 |
Correct |
246 ms |
14908 KB |
Output is correct |