#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=1;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;
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<<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:51:15: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
51 | cout<<ans+k*lmd<<'\n';
| ^~~
feast.cpp:51:14: warning: 'lmd' may be used uninitialized in this function [-Wmaybe-uninitialized]
51 | cout<<ans+k*lmd<<'\n';
| ~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
11600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
11888 KB |
Output is correct |
2 |
Correct |
74 ms |
12100 KB |
Output is correct |
3 |
Correct |
59 ms |
11864 KB |
Output is correct |
4 |
Incorrect |
64 ms |
11976 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
11900 KB |
Output is correct |
2 |
Correct |
115 ms |
11644 KB |
Output is correct |
3 |
Correct |
102 ms |
11868 KB |
Output is correct |
4 |
Correct |
94 ms |
11912 KB |
Output is correct |
5 |
Correct |
99 ms |
11860 KB |
Output is correct |
6 |
Correct |
99 ms |
11860 KB |
Output is correct |
7 |
Correct |
108 ms |
11916 KB |
Output is correct |
8 |
Correct |
92 ms |
11856 KB |
Output is correct |
9 |
Correct |
91 ms |
12168 KB |
Output is correct |
10 |
Correct |
88 ms |
11904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 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 |
348 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 |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
11600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |