#include<bits/stdc++.h>
#ifdef LOCAL
#include "D:\debug.h"
#else
#define cebug(...) "Orz_chUoNgnn_khanhtb_0x2ee08"
#endif
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define cd complex<double>
const ll mod = 1e9 + 7;
const ll INF = 1e18L + 5;
const double PI = acos(-1);
const int block = 320;
const int N = 3e5;
int tc, tt = 1;
int n, k;
int a[N + 5];
ll dp[N + 5][2], cnt[N + 5][2];
void solve() {
cin>>n>>k;
for(int i=1; i<=n; i++) cin>>a[i];
int l = 0, r = 1e12;
ll ans = 0;
while(l <= r) {
int mid = (l + r)/2;
dp[1][0] = 0;
dp[1][1] = a[1] - mid;
cnt[1][0] = 0;
cnt[1][1] = 1;
for(int i=2; i<=n; i++) {
ll v1 = dp[i - 1][0] + a[i] - mid;
ll v2 = dp[i - 1][1] + a[i];
dp[i][1] = max(v1, v2);
if(v1 == v2) cnt[i][1] = max(cnt[i - 1][0] + 1, cnt[i - 1][1]);
else {
if(dp[i][1] == v1) cnt[i][1] = cnt[i - 1][0] + 1;
else cnt[i][1] = cnt[i - 1][1];
}
v1 = dp[i - 1][0];
v2 = dp[i - 1][1];
dp[i][0] = max(v1, v2);
if(v1 == v2) cnt[i][0] = max(cnt[i - 1][0], cnt[i - 1][1]);
else {
if(dp[i][0] == v1) cnt[i][0] = cnt[i - 1][0];
else cnt[i][0] = cnt[i - 1][1];
}
}
ll seg = (dp[n][0] > dp[n][1] ? cnt[n][0] : cnt[n][1]);
if(dp[n][0] == dp[n][1]) seg = max(cnt[n][0], cnt[n][1]);
if(seg > k) l = mid + 1;
else {
if(seg <= k)
ans = max(ans, max(dp[n][0], dp[n][1]) + mid * seg);
r = mid - 1;
}
}
cout<<ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen(".INP", "r", stdin);
// freopen(".OUT", "w", stdout);
for(tc=1; tc<=tt; tc++) solve();
cerr<<"\nTime elapsed: "<<1000.0*clock()/CLOCKS_PER_SEC<<" ms.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
11612 KB |
Output is correct |
2 |
Correct |
41 ms |
11860 KB |
Output is correct |
3 |
Correct |
44 ms |
12124 KB |
Output is correct |
4 |
Correct |
48 ms |
11856 KB |
Output is correct |
5 |
Correct |
42 ms |
11816 KB |
Output is correct |
6 |
Correct |
42 ms |
11828 KB |
Output is correct |
7 |
Correct |
41 ms |
11840 KB |
Output is correct |
8 |
Correct |
42 ms |
11860 KB |
Output is correct |
9 |
Correct |
40 ms |
11844 KB |
Output is correct |
10 |
Correct |
41 ms |
11856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
11868 KB |
Output is correct |
2 |
Correct |
37 ms |
12120 KB |
Output is correct |
3 |
Correct |
36 ms |
11868 KB |
Output is correct |
4 |
Correct |
36 ms |
11864 KB |
Output is correct |
5 |
Correct |
41 ms |
11608 KB |
Output is correct |
6 |
Correct |
50 ms |
11868 KB |
Output is correct |
7 |
Correct |
36 ms |
12124 KB |
Output is correct |
8 |
Correct |
41 ms |
11912 KB |
Output is correct |
9 |
Correct |
40 ms |
11724 KB |
Output is correct |
10 |
Correct |
41 ms |
11864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
11860 KB |
Output is correct |
2 |
Correct |
63 ms |
11864 KB |
Output is correct |
3 |
Correct |
66 ms |
11860 KB |
Output is correct |
4 |
Correct |
61 ms |
11868 KB |
Output is correct |
5 |
Correct |
69 ms |
11876 KB |
Output is correct |
6 |
Correct |
61 ms |
11868 KB |
Output is correct |
7 |
Correct |
61 ms |
12120 KB |
Output is correct |
8 |
Correct |
60 ms |
11856 KB |
Output is correct |
9 |
Correct |
62 ms |
12116 KB |
Output is correct |
10 |
Correct |
61 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
432 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
11612 KB |
Output is correct |
2 |
Correct |
41 ms |
11860 KB |
Output is correct |
3 |
Correct |
44 ms |
12124 KB |
Output is correct |
4 |
Correct |
48 ms |
11856 KB |
Output is correct |
5 |
Correct |
42 ms |
11816 KB |
Output is correct |
6 |
Correct |
42 ms |
11828 KB |
Output is correct |
7 |
Correct |
41 ms |
11840 KB |
Output is correct |
8 |
Correct |
42 ms |
11860 KB |
Output is correct |
9 |
Correct |
40 ms |
11844 KB |
Output is correct |
10 |
Correct |
41 ms |
11856 KB |
Output is correct |
11 |
Correct |
39 ms |
11868 KB |
Output is correct |
12 |
Correct |
37 ms |
12120 KB |
Output is correct |
13 |
Correct |
36 ms |
11868 KB |
Output is correct |
14 |
Correct |
36 ms |
11864 KB |
Output is correct |
15 |
Correct |
41 ms |
11608 KB |
Output is correct |
16 |
Correct |
50 ms |
11868 KB |
Output is correct |
17 |
Correct |
36 ms |
12124 KB |
Output is correct |
18 |
Correct |
41 ms |
11912 KB |
Output is correct |
19 |
Correct |
40 ms |
11724 KB |
Output is correct |
20 |
Correct |
41 ms |
11864 KB |
Output is correct |
21 |
Correct |
70 ms |
11860 KB |
Output is correct |
22 |
Correct |
63 ms |
11864 KB |
Output is correct |
23 |
Correct |
66 ms |
11860 KB |
Output is correct |
24 |
Correct |
61 ms |
11868 KB |
Output is correct |
25 |
Correct |
69 ms |
11876 KB |
Output is correct |
26 |
Correct |
61 ms |
11868 KB |
Output is correct |
27 |
Correct |
61 ms |
12120 KB |
Output is correct |
28 |
Correct |
60 ms |
11856 KB |
Output is correct |
29 |
Correct |
62 ms |
12116 KB |
Output is correct |
30 |
Correct |
61 ms |
12116 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
600 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
432 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
344 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
99 ms |
11864 KB |
Output is correct |
62 |
Correct |
106 ms |
11928 KB |
Output is correct |
63 |
Correct |
74 ms |
11864 KB |
Output is correct |
64 |
Correct |
101 ms |
12088 KB |
Output is correct |
65 |
Correct |
105 ms |
12076 KB |
Output is correct |
66 |
Correct |
100 ms |
11856 KB |
Output is correct |
67 |
Correct |
104 ms |
11868 KB |
Output is correct |
68 |
Correct |
79 ms |
11864 KB |
Output is correct |
69 |
Correct |
61 ms |
11868 KB |
Output is correct |
70 |
Correct |
60 ms |
11600 KB |
Output is correct |