#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[1][0]={0,0};
dp[1][1]={a[1]-lambda,1};
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)/2;
pll p=go(md);
if(p.ss>=k){ans=p.ff;lmd=md;l=md+1;}
else{r=md-1;}
}
cout<<max(0ll,ans+k*lmd)<<'\n';
}
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:52:22: warning: 'lmd' may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | cout<<max(0ll,ans+k*lmd)<<'\n';
| ~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
11804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
11868 KB |
Output is correct |
2 |
Correct |
69 ms |
12144 KB |
Output is correct |
3 |
Correct |
74 ms |
11856 KB |
Output is correct |
4 |
Incorrect |
66 ms |
11868 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
12040 KB |
Output is correct |
2 |
Correct |
103 ms |
11908 KB |
Output is correct |
3 |
Correct |
97 ms |
12004 KB |
Output is correct |
4 |
Correct |
93 ms |
11896 KB |
Output is correct |
5 |
Correct |
102 ms |
11868 KB |
Output is correct |
6 |
Correct |
95 ms |
11904 KB |
Output is correct |
7 |
Correct |
97 ms |
12124 KB |
Output is correct |
8 |
Correct |
104 ms |
11868 KB |
Output is correct |
9 |
Correct |
100 ms |
12124 KB |
Output is correct |
10 |
Correct |
95 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
11804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |