#include<bits/stdc++.h>
using namespace std;
const long long maxn=300000+10;
long long n,k,all[maxn];
pair<long long ,long long>check(long long mid){
vector<pair<long long ,long long>>dp0(n),dp1(n);
pair<long long,long long>ret;
for(long long i=0;i<n;i++){
dp1[i]=make_pair(all[i]-mid,1);
dp0[i]=make_pair(0,0);
if(i==0){
continue;
}
dp1[i]=max(dp1[i],make_pair(dp0[i-1].first-mid+all[i],dp0[i-1].second+1));
dp1[i]=max(dp1[i],make_pair(dp1[i-1].first+all[i],dp1[i-1].second));
dp0[i]=max(dp0[i],dp0[i-1]);
dp0[i]=max(dp0[i],dp1[i-1]);
}
ret.second=max(dp0[n-1],dp1[n-1]).second;
for(long long i=0;i<n;i++){
dp1[i]=make_pair(all[i]-mid,-1);
dp0[i]=make_pair(0,0);
if(i==0){
continue;
}
dp1[i]=max(dp1[i],make_pair(dp0[i-1].first-mid+all[i],dp0[i-1].second-1));
dp1[i]=max(dp1[i],make_pair(dp1[i-1].first+all[i],dp1[i-1].second));
dp0[i]=max(dp0[i],dp0[i-1]);
dp0[i]=max(dp0[i],dp1[i-1]);
}
ret.first=max(dp0[n-1],dp1[n-1]).second*-1;
return ret;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(long long i=0;i<n;i++){
cin>>all[i];
}
long long opt=0;
long long high=1e15,low=-1,mid;
while(high-low>1){
mid=(high+low)>>1;
pair<long long ,long long>stf=check(mid);
if(stf.first<=k&&stf.second>=k){
opt=mid;
break;
}
if(stf.second<k){
high=mid;
}else{
low=mid;
}
}
mid=opt;
vector<pair<long long ,long long>>dp0(n),dp1(n);
pair<long long,long long>ret;
for(long long i=0;i<n;i++){
dp1[i]=make_pair(all[i]-mid,1);
dp0[i]=make_pair(0,0);
if(i==0){
continue;
}
dp1[i]=max(dp1[i],make_pair(dp0[i-1].first-mid+all[i],dp0[i-1].second+1));
dp1[i]=max(dp1[i],make_pair(dp1[i-1].first+all[i],dp1[i-1].second));
dp0[i]=max(dp0[i],dp0[i-1]);
dp0[i]=max(dp0[i],dp1[i-1]);
}
cout<<max(dp1[n-1],dp0[n-1]).first+k*opt<<endl;
//cout<<mid<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
11788 KB |
Output is correct |
2 |
Correct |
199 ms |
12100 KB |
Output is correct |
3 |
Correct |
214 ms |
12212 KB |
Output is correct |
4 |
Correct |
238 ms |
12136 KB |
Output is correct |
5 |
Correct |
173 ms |
12064 KB |
Output is correct |
6 |
Correct |
221 ms |
13796 KB |
Output is correct |
7 |
Correct |
243 ms |
11856 KB |
Output is correct |
8 |
Correct |
232 ms |
12116 KB |
Output is correct |
9 |
Correct |
193 ms |
11924 KB |
Output is correct |
10 |
Correct |
202 ms |
12048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
11924 KB |
Output is correct |
2 |
Correct |
113 ms |
12196 KB |
Output is correct |
3 |
Correct |
97 ms |
11932 KB |
Output is correct |
4 |
Correct |
214 ms |
12056 KB |
Output is correct |
5 |
Correct |
37 ms |
13768 KB |
Output is correct |
6 |
Correct |
92 ms |
11988 KB |
Output is correct |
7 |
Correct |
199 ms |
12260 KB |
Output is correct |
8 |
Correct |
34 ms |
12124 KB |
Output is correct |
9 |
Correct |
37 ms |
11788 KB |
Output is correct |
10 |
Correct |
184 ms |
12144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
14028 KB |
Output is correct |
2 |
Correct |
118 ms |
11936 KB |
Output is correct |
3 |
Correct |
99 ms |
12032 KB |
Output is correct |
4 |
Correct |
99 ms |
11952 KB |
Output is correct |
5 |
Correct |
117 ms |
12048 KB |
Output is correct |
6 |
Correct |
130 ms |
12380 KB |
Output is correct |
7 |
Correct |
122 ms |
12160 KB |
Output is correct |
8 |
Correct |
118 ms |
12064 KB |
Output is correct |
9 |
Correct |
140 ms |
12172 KB |
Output is correct |
10 |
Correct |
118 ms |
12164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
472 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
472 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
344 KB |
Output is correct |
27 |
Correct |
2 ms |
344 KB |
Output is correct |
28 |
Correct |
2 ms |
504 KB |
Output is correct |
29 |
Correct |
1 ms |
476 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
11788 KB |
Output is correct |
2 |
Correct |
199 ms |
12100 KB |
Output is correct |
3 |
Correct |
214 ms |
12212 KB |
Output is correct |
4 |
Correct |
238 ms |
12136 KB |
Output is correct |
5 |
Correct |
173 ms |
12064 KB |
Output is correct |
6 |
Correct |
221 ms |
13796 KB |
Output is correct |
7 |
Correct |
243 ms |
11856 KB |
Output is correct |
8 |
Correct |
232 ms |
12116 KB |
Output is correct |
9 |
Correct |
193 ms |
11924 KB |
Output is correct |
10 |
Correct |
202 ms |
12048 KB |
Output is correct |
11 |
Correct |
142 ms |
11924 KB |
Output is correct |
12 |
Correct |
113 ms |
12196 KB |
Output is correct |
13 |
Correct |
97 ms |
11932 KB |
Output is correct |
14 |
Correct |
214 ms |
12056 KB |
Output is correct |
15 |
Correct |
37 ms |
13768 KB |
Output is correct |
16 |
Correct |
92 ms |
11988 KB |
Output is correct |
17 |
Correct |
199 ms |
12260 KB |
Output is correct |
18 |
Correct |
34 ms |
12124 KB |
Output is correct |
19 |
Correct |
37 ms |
11788 KB |
Output is correct |
20 |
Correct |
184 ms |
12144 KB |
Output is correct |
21 |
Correct |
143 ms |
14028 KB |
Output is correct |
22 |
Correct |
118 ms |
11936 KB |
Output is correct |
23 |
Correct |
99 ms |
12032 KB |
Output is correct |
24 |
Correct |
99 ms |
11952 KB |
Output is correct |
25 |
Correct |
117 ms |
12048 KB |
Output is correct |
26 |
Correct |
130 ms |
12380 KB |
Output is correct |
27 |
Correct |
122 ms |
12160 KB |
Output is correct |
28 |
Correct |
118 ms |
12064 KB |
Output is correct |
29 |
Correct |
140 ms |
12172 KB |
Output is correct |
30 |
Correct |
118 ms |
12164 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
472 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
344 KB |
Output is correct |
52 |
Correct |
2 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
344 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
344 KB |
Output is correct |
57 |
Correct |
2 ms |
344 KB |
Output is correct |
58 |
Correct |
2 ms |
504 KB |
Output is correct |
59 |
Correct |
1 ms |
476 KB |
Output is correct |
60 |
Correct |
2 ms |
348 KB |
Output is correct |
61 |
Correct |
216 ms |
11940 KB |
Output is correct |
62 |
Correct |
319 ms |
12228 KB |
Output is correct |
63 |
Correct |
222 ms |
11892 KB |
Output is correct |
64 |
Correct |
296 ms |
14056 KB |
Output is correct |
65 |
Correct |
229 ms |
12156 KB |
Output is correct |
66 |
Correct |
242 ms |
12088 KB |
Output is correct |
67 |
Correct |
243 ms |
11944 KB |
Output is correct |
68 |
Correct |
158 ms |
12120 KB |
Output is correct |
69 |
Correct |
124 ms |
11928 KB |
Output is correct |
70 |
Correct |
117 ms |
11932 KB |
Output is correct |