#include <bits/stdc++.h>
#define PI acos(-1.0)
#define pll pair<ll, ll>
#define ll long long
#define ld long double
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define pll2 pair<pll,ll>
#define bit(i,k) ((k>>i)&1)
#define pii pair<int,int>
#define mp make_pair
using namespace std;
const long long mod = 1e9+7;
const ll inf = 1e18;
const ll maxN = 3e5+5;
ll n,k;
ll a[maxN];
pll go(ll lambda){
pll dp[n+5][2]={};//lay i hoac deo
dp[0][1].ff=-inf;
for(int i=2;i<=n;i++){
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
pll p1={dp[i-1][0].ff+a[i]-lambda,dp[i-1][0].ss+1};
pll p2={dp[i-1][1].ff+a[i],dp[i-1][1].ss};
dp[i][1]=max(p1,p2);
}
return max(dp[n][1],dp[n][0]);
}
void solve() {
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
ll l=0,r=inf;
ll lmd;
ll ans=-inf;
while(l<r){
ll md=(l+r+1)/2;
pll p=go(md);
if(p.ss>=k){ans=p.ff;l=md;}
else{r=md-1;}
}
cout<<max(0ll,go(l).ff+k*l);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("rao.inp", "r", stdin);
// freopen("rao.out", "w", stdout);
ll nhim = 1;
// cin >> nhim;
// cout<<nhim<<'\n';
while (nhim--) {
solve();
}
}
Compilation message
feast.cpp: In function 'void solve()':
feast.cpp:42:6: warning: unused variable 'lmd' [-Wunused-variable]
42 | ll lmd;
| ^~~
feast.cpp:43:6: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
43 | ll ans=-inf;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
14456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
103 ms |
15064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
14456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |