# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1017473 | vjudge1 | Feast (NOI19_feast) | C++14 | 112 ms | 14012 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define taskname ""
using namespace std;
ll n,k,i,l,r,mid,answer=-1e18,dp[300009][2],a[300009],cnt[300009][2],sum;
pll ans;
pll check(ll v){
dp[1][0]=0;
dp[1][1]=a[1]-v;
cnt[1][0]=0;
cnt[1][1]=1;
for(i=2;i<=n;i++){
if(dp[i-1][1]<dp[i-1][0]){
dp[i][0]=dp[i-1][0];
cnt[i][0]=cnt[i-1][0];
}else{
dp[i][0]=dp[i-1][1];
cnt[i][0]=cnt[i-1][1];
}
if(dp[i-1][1]+a[i]<dp[i-1][0]+a[i]-v){
dp[i][1]=dp[i-1][0]+a[i]-v;
cnt[i][1]=cnt[i-1][0]+1;
}else{
dp[i][1]=dp[i-1][1]+a[i];
cnt[i][1]=cnt[i-1][1];
}
}pll res={dp[n][1],cnt[n][1]};
pll result={dp[n][0],cnt[n][0]};
res=max(res,result);
return res;
}
int main() {
if (fopen(taskname".inp","r")) {
freopen(taskname".inp","r",stdin);
freopen(taskname".out","w",stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(i=1;i<=n;i++)
cin>>a[i];
l=0;r=1e18;
while(l<=r){
mid=(l+r)/2;
ans=check(mid);
if(ans.se<=k){
answer=max(answer,ans.fi+mid*ans.se);
r=mid-1;
}else l=mid+1;
}cout<<answer;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |