#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define pb push_back
using namespace std;
const ll N=3e5+9;
const ll mod=1e9+7;
ll t,n,k,i,m,x,a[N],tot[N],q,l,b[N],c[N],mu[N],sl=0,xor_basis[64],len=0,r;
ll dp[N][2];
ii check(ll pen)
{
dp[0][1]=-pen;
dp[0][0]=0;
ll res=0;
ll cnt=0;
for(i=1;i<=n;i++)
{
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
dp[i][1]=max(dp[i-1][0]-pen,dp[i-1][1])+a[i];
res=max(res,dp[i][1]);
}
bool op=0;
ll tot=res;
if(res==0) return {0,0};
for(i=n;i>0;i--)
if(res==dp[i][1])
{
res-=a[i];
if(!op){res-=pen; cnt++;}
op=1;
}
else op=0;
return {tot+pen*cnt,cnt};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>k;
for(i=1;i<=n;i++)
cin>>a[i];
ll l=0,r=1e18,kq=0;
while(l<=r)
{
ll mid=(l+r)/2;
ii ans=check(mid);
// cout<<mid<<" "<<ans.se<<'\n';
if(ans.se<=k){
if(ans.se>0)
kq=max(kq,ans.fi);
r=mid-1;
}
else l=mid+1;
}
cout<<kq;
}
/*
6 2
1 2 3 -10 5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
7256 KB |
Output is correct |
2 |
Correct |
68 ms |
7260 KB |
Output is correct |
3 |
Correct |
69 ms |
7256 KB |
Output is correct |
4 |
Correct |
76 ms |
7508 KB |
Output is correct |
5 |
Correct |
70 ms |
9040 KB |
Output is correct |
6 |
Correct |
77 ms |
9044 KB |
Output is correct |
7 |
Correct |
70 ms |
8828 KB |
Output is correct |
8 |
Correct |
80 ms |
9108 KB |
Output is correct |
9 |
Correct |
70 ms |
9044 KB |
Output is correct |
10 |
Correct |
70 ms |
9060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
8028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
9400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
7256 KB |
Output is correct |
2 |
Correct |
68 ms |
7260 KB |
Output is correct |
3 |
Correct |
69 ms |
7256 KB |
Output is correct |
4 |
Correct |
76 ms |
7508 KB |
Output is correct |
5 |
Correct |
70 ms |
9040 KB |
Output is correct |
6 |
Correct |
77 ms |
9044 KB |
Output is correct |
7 |
Correct |
70 ms |
8828 KB |
Output is correct |
8 |
Correct |
80 ms |
9108 KB |
Output is correct |
9 |
Correct |
70 ms |
9044 KB |
Output is correct |
10 |
Correct |
70 ms |
9060 KB |
Output is correct |
11 |
Incorrect |
58 ms |
8028 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |