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>
using namespace std;
#define int long long
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define fori(x,a,b) for (int x=a;x<=b;x++)
#define ford(x,a,b) for (int x=a;x>=b;x--)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define getbit(x,i) ((x>>i)&1)
#define batbit(x,i) (x|(1ll<<i))
#define tatbit(x,i) (x&~(1<<i))
#define gg exit(0);
const int N = 3e5+10;
int n,a[N],k;
int f[N][2],cnt[N][2];
bool check(int val)
{
//0 : ko chon i
//1 : chon i
fori(i,1,n) fori(j,0,1) f[i][j] = 0;
f[1][0] = 0;
f[1][1] = a[1]-val;
cnt[1][0] = 0;
cnt[1][1] = 1;
fori(i,2,n)
{
f[i][0] = max(f[i-1][0],f[i-1][1]);
if(f[i-1][0] > f[i-1][1]) cnt[i][0] = cnt[i-1][0];
else cnt[i][0] = cnt[i-1][1];
f[i][1] = max(f[i-1][0]-val,f[i-1][1]) + a[i];
if(f[i-1][0]-val > f[i-1][1]) cnt[i][1] = cnt[i-1][0] + 1;
else cnt[i][1] = cnt[i-1][1];
}
if(f[n][0] > f[n][1]) return (cnt[n][0]>=k);
return (cnt[n][1]>=k);
}
main()
{
fasty;
//freopen("feast.inp","r",stdin);
//freopen("feast.out","w",stdout);
cin>>n>>k;
fori(i,1,n) cin>>a[i];
int l=0,r=1e15,res=0;
while(l<=r)
{
int mid = (l+r)/2;
if(check(mid)) l=mid+1,res=mid;
else r=mid-1;
}
fori(i,1,n) fori(j,0,1) f[i][j] = 0;
f[1][0] = 0;
f[1][1] = a[1]-res;
cnt[1][0] = 0;
cnt[1][1] = 1;
fori(i,2,n)
{
f[i][0] = max(f[i-1][0],f[i-1][1]);
if(f[i-1][0] > f[i-1][1]) cnt[i][0] = cnt[i-1][0];
else cnt[i][0] = cnt[i-1][1];
f[i][1] = max(f[i-1][0]-res,f[i-1][1]) + a[i];
if(f[i-1][0]-res > f[i-1][1]) cnt[i][1] = cnt[i-1][0] + 1;
else cnt[i][1] = cnt[i-1][1];
}
if(f[n][0] > f[n][1]) cout<<cnt[n][0]*res + f[n][0];
else cout<<cnt[n][1]*res+f[n][1];
}
Compilation message (stderr)
feast.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main()
| ^~~~
# | 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... |