#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e17;
ll n,k,v[300005];
struct date
{
ll val,l,r;
};
date dp[300005][2];
date combine(date a, date b)
{
if(a.val>b.val)
return a;
if(a.val<b.val)
return b;
a.l=min(a.l,b.l);
a.r=max(a.r,b.r);
return a;
}
date calc(ll cost)
{
dp[0][0]={0,0,0};
dp[0][1]={-INF,0,0};
for(int i=1;i<=n;i++)
{
dp[i][0]=combine(dp[i-1][0],dp[i-1][1]);
date a=dp[i-1][0];
a.l++;
a.r++;
a.val+=v[i]-cost;
dp[i][1]=a;
a=dp[i-1][1];
a.val+=v[i];
dp[i][1]=combine(dp[i][1],a);
a=dp[i-1][1];
a.l++;
a.r++;
a.val+=v[i]-cost;
dp[i][1]=combine(dp[i][1],a);
}
return combine(dp[n][0],dp[n][1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(ll i=1;i<=n;i++)
cin>>v[i];
ll st=-1e12;
ll dr=1e12;
while(st<=dr)
{
ll mij=(st+dr)/2;
date rez=calc(mij);
if(rez.l<=k&&rez.r>=k)
{
cout<<max(0LL,rez.val+k*mij);
return 0;
}
if(rez.l>k)
st=mij+1;
else
dr=mij-1;
}
assert(false);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
19572 KB |
Output is correct |
2 |
Correct |
26 ms |
19676 KB |
Output is correct |
3 |
Correct |
27 ms |
19540 KB |
Output is correct |
4 |
Correct |
27 ms |
19792 KB |
Output is correct |
5 |
Correct |
26 ms |
19540 KB |
Output is correct |
6 |
Correct |
26 ms |
19536 KB |
Output is correct |
7 |
Correct |
26 ms |
19536 KB |
Output is correct |
8 |
Correct |
25 ms |
19536 KB |
Output is correct |
9 |
Correct |
26 ms |
19540 KB |
Output is correct |
10 |
Correct |
26 ms |
19636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
17744 KB |
Output is correct |
2 |
Correct |
34 ms |
18012 KB |
Output is correct |
3 |
Correct |
32 ms |
17752 KB |
Output is correct |
4 |
Correct |
18 ms |
17756 KB |
Output is correct |
5 |
Correct |
28 ms |
19540 KB |
Output is correct |
6 |
Correct |
32 ms |
17756 KB |
Output is correct |
7 |
Correct |
19 ms |
18000 KB |
Output is correct |
8 |
Correct |
27 ms |
19540 KB |
Output is correct |
9 |
Correct |
27 ms |
19548 KB |
Output is correct |
10 |
Correct |
17 ms |
18012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
19796 KB |
Output is correct |
2 |
Correct |
43 ms |
19796 KB |
Output is correct |
3 |
Correct |
39 ms |
19804 KB |
Output is correct |
4 |
Correct |
41 ms |
19792 KB |
Output is correct |
5 |
Correct |
39 ms |
19796 KB |
Output is correct |
6 |
Correct |
44 ms |
19792 KB |
Output is correct |
7 |
Correct |
47 ms |
19796 KB |
Output is correct |
8 |
Correct |
45 ms |
19796 KB |
Output is correct |
9 |
Correct |
44 ms |
19796 KB |
Output is correct |
10 |
Correct |
46 ms |
19804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
19572 KB |
Output is correct |
2 |
Correct |
26 ms |
19676 KB |
Output is correct |
3 |
Correct |
27 ms |
19540 KB |
Output is correct |
4 |
Correct |
27 ms |
19792 KB |
Output is correct |
5 |
Correct |
26 ms |
19540 KB |
Output is correct |
6 |
Correct |
26 ms |
19536 KB |
Output is correct |
7 |
Correct |
26 ms |
19536 KB |
Output is correct |
8 |
Correct |
25 ms |
19536 KB |
Output is correct |
9 |
Correct |
26 ms |
19540 KB |
Output is correct |
10 |
Correct |
26 ms |
19636 KB |
Output is correct |
11 |
Correct |
38 ms |
17744 KB |
Output is correct |
12 |
Correct |
34 ms |
18012 KB |
Output is correct |
13 |
Correct |
32 ms |
17752 KB |
Output is correct |
14 |
Correct |
18 ms |
17756 KB |
Output is correct |
15 |
Correct |
28 ms |
19540 KB |
Output is correct |
16 |
Correct |
32 ms |
17756 KB |
Output is correct |
17 |
Correct |
19 ms |
18000 KB |
Output is correct |
18 |
Correct |
27 ms |
19540 KB |
Output is correct |
19 |
Correct |
27 ms |
19548 KB |
Output is correct |
20 |
Correct |
17 ms |
18012 KB |
Output is correct |
21 |
Correct |
43 ms |
19796 KB |
Output is correct |
22 |
Correct |
43 ms |
19796 KB |
Output is correct |
23 |
Correct |
39 ms |
19804 KB |
Output is correct |
24 |
Correct |
41 ms |
19792 KB |
Output is correct |
25 |
Correct |
39 ms |
19796 KB |
Output is correct |
26 |
Correct |
44 ms |
19792 KB |
Output is correct |
27 |
Correct |
47 ms |
19796 KB |
Output is correct |
28 |
Correct |
45 ms |
19796 KB |
Output is correct |
29 |
Correct |
44 ms |
19796 KB |
Output is correct |
30 |
Correct |
46 ms |
19804 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |