#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define pb push_back
#define mod 1000000007
#define INF 200000000000000000
#define INF2 2000000000
#define fi first
#define se second
using namespace std;
double const EPS = 1e-10;
const int P = 1007;
typedef long long ll;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // find_by_order, order_of_key
const int N = 3e5 + 5;
ll arr[N];
pair<ll,ll> dp[N][2];
int n, k;
pair<ll,ll> sol(ll mid) {
for(int i = 0; i <= n; i++) {
dp[i][0].fi = 0;
dp[i][1].fi = 0;
dp[i][0].se = 0;
dp[i][1].se = 0;
}
// vector<int> pos;
dp[0][1].fi -= mid;
dp[0][1].se += 1;
for(int i = 1; i <= n; i++) {
if(dp[i-1][0].fi > dp[i-1][1].fi) dp[i][0].se = dp[i-1][0].se;
else if(dp[i-1][0].fi < dp[i-1][1].fi) dp[i][0].se = dp[i-1][1].se;
else dp[i][0].se = max(dp[i-1][0].se,dp[i-1][1].se);
dp[i][0].fi = max(dp[i-1][0].fi,dp[i-1][1].fi);
if(dp[i-1][1].fi+arr[i] < dp[i-1][0].fi+arr[i]-mid) {
dp[i][1].se = dp[i-1][0].se+1;
}
else if(dp[i-1][1].fi+arr[i] > dp[i-1][0].fi+arr[i]-mid) {
dp[i][1].se = dp[i-1][1].se;
}
else {
dp[i][1].se = max(dp[i-1][1].se,dp[i-1][0].se+1);
}
dp[i][1].fi = max(dp[i-1][1].fi+arr[i],dp[i-1][0].fi+arr[i]-mid);
}
if(dp[n][0].fi > dp[n][1].fi) return {dp[n][0].fi,dp[n][0].se};
else if(dp[n][0].fi < dp[n][1].fi) return {dp[n][1].fi,dp[n][1].se};
else return {dp[n][1].fi,max(dp[n][0].se,dp[n][1].se)};
}
int main(){
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> arr[i];
ll l = 0, r = INF;
ll mx = 0;
while(l <= r) {
ll mid = (l+r)/2;
pair<ll,ll> cal = sol(mid);
if(cal.se <= k) {
/*if(cal.se == k) {
for(int i = 0; i <= n; i++) {
cout << i << ' ' << 'i' << endl;
cout << dp[i][0].fi << ' ' << dp[i][0].se << 'a' << endl;
cout << dp[i][1].fi << ' ' << dp[i][1].se << 'b' << endl;
}
}*/
mx = max(cal.fi + (cal.se*mid),mx);
r = mid-1;
}
else {
l = mid+1;
}
}
cout << mx << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
11612 KB |
Output is correct |
2 |
Correct |
71 ms |
11868 KB |
Output is correct |
3 |
Correct |
67 ms |
12172 KB |
Output is correct |
4 |
Correct |
72 ms |
11868 KB |
Output is correct |
5 |
Correct |
68 ms |
12016 KB |
Output is correct |
6 |
Correct |
70 ms |
11824 KB |
Output is correct |
7 |
Correct |
76 ms |
11600 KB |
Output is correct |
8 |
Correct |
69 ms |
11860 KB |
Output is correct |
9 |
Correct |
68 ms |
11852 KB |
Output is correct |
10 |
Correct |
67 ms |
11968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
11868 KB |
Output is correct |
2 |
Correct |
60 ms |
12124 KB |
Output is correct |
3 |
Correct |
59 ms |
11864 KB |
Output is correct |
4 |
Correct |
76 ms |
11984 KB |
Output is correct |
5 |
Correct |
68 ms |
11604 KB |
Output is correct |
6 |
Correct |
63 ms |
11868 KB |
Output is correct |
7 |
Correct |
59 ms |
12064 KB |
Output is correct |
8 |
Correct |
70 ms |
11860 KB |
Output is correct |
9 |
Correct |
66 ms |
11612 KB |
Output is correct |
10 |
Correct |
57 ms |
12012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
11856 KB |
Output is correct |
2 |
Correct |
85 ms |
14684 KB |
Output is correct |
3 |
Correct |
87 ms |
15196 KB |
Output is correct |
4 |
Correct |
84 ms |
14732 KB |
Output is correct |
5 |
Correct |
82 ms |
14924 KB |
Output is correct |
6 |
Correct |
83 ms |
14932 KB |
Output is correct |
7 |
Correct |
87 ms |
15164 KB |
Output is correct |
8 |
Correct |
89 ms |
14932 KB |
Output is correct |
9 |
Correct |
85 ms |
15184 KB |
Output is correct |
10 |
Correct |
97 ms |
15172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
464 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
344 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 |
464 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
348 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 |
344 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 |
464 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
348 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 |
348 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
348 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 |
72 ms |
11612 KB |
Output is correct |
2 |
Correct |
71 ms |
11868 KB |
Output is correct |
3 |
Correct |
67 ms |
12172 KB |
Output is correct |
4 |
Correct |
72 ms |
11868 KB |
Output is correct |
5 |
Correct |
68 ms |
12016 KB |
Output is correct |
6 |
Correct |
70 ms |
11824 KB |
Output is correct |
7 |
Correct |
76 ms |
11600 KB |
Output is correct |
8 |
Correct |
69 ms |
11860 KB |
Output is correct |
9 |
Correct |
68 ms |
11852 KB |
Output is correct |
10 |
Correct |
67 ms |
11968 KB |
Output is correct |
11 |
Correct |
53 ms |
11868 KB |
Output is correct |
12 |
Correct |
60 ms |
12124 KB |
Output is correct |
13 |
Correct |
59 ms |
11864 KB |
Output is correct |
14 |
Correct |
76 ms |
11984 KB |
Output is correct |
15 |
Correct |
68 ms |
11604 KB |
Output is correct |
16 |
Correct |
63 ms |
11868 KB |
Output is correct |
17 |
Correct |
59 ms |
12064 KB |
Output is correct |
18 |
Correct |
70 ms |
11860 KB |
Output is correct |
19 |
Correct |
66 ms |
11612 KB |
Output is correct |
20 |
Correct |
57 ms |
12012 KB |
Output is correct |
21 |
Correct |
83 ms |
11856 KB |
Output is correct |
22 |
Correct |
85 ms |
14684 KB |
Output is correct |
23 |
Correct |
87 ms |
15196 KB |
Output is correct |
24 |
Correct |
84 ms |
14732 KB |
Output is correct |
25 |
Correct |
82 ms |
14924 KB |
Output is correct |
26 |
Correct |
83 ms |
14932 KB |
Output is correct |
27 |
Correct |
87 ms |
15164 KB |
Output is correct |
28 |
Correct |
89 ms |
14932 KB |
Output is correct |
29 |
Correct |
85 ms |
15184 KB |
Output is correct |
30 |
Correct |
97 ms |
15172 KB |
Output is correct |
31 |
Correct |
0 ms |
344 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 |
464 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 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 |
348 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 |
348 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 |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
348 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 |
139 ms |
14928 KB |
Output is correct |
62 |
Correct |
110 ms |
15228 KB |
Output is correct |
63 |
Correct |
97 ms |
14580 KB |
Output is correct |
64 |
Correct |
116 ms |
14928 KB |
Output is correct |
65 |
Correct |
112 ms |
14928 KB |
Output is correct |
66 |
Correct |
117 ms |
15064 KB |
Output is correct |
67 |
Correct |
111 ms |
14804 KB |
Output is correct |
68 |
Correct |
83 ms |
14940 KB |
Output is correct |
69 |
Correct |
86 ms |
14684 KB |
Output is correct |
70 |
Correct |
90 ms |
14672 KB |
Output is correct |