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 fi first
#define se second.first
#define rd second.second
#define mp(a,b,c) make_pair(a,make_pair(b,c))
using ii = pair<long long,pair<long long,int>>;
const int N=100010;
int n,k,it;
long long ma;
int a[N];
long long s[N],x[N];
vector<int> ans;
int cur;
int P[210][N];
long long F[N],H[N];
ii st[N];
double G[N];
double I(ii u,ii v){return 1.0*(v.se-u.se)/(u.fi-v.fi);}
main()
{
ios_base::sync_with_stdio(false),cin.tie(nullptr);
cin>>n>>k;
for(int i=1;i<=n;++i) cin>>a[i],s[i]=s[i-1]+a[i];
for(int i=1;i<=n;++i) x[i]=s[n]-s[i];
for(int c=1;c<=k;++c)
{
cur=0;
for(int i=c;i<n;++i)
{
ii T=mp(-s[i-1],H[i-1],i-1);
if(cur && st[cur].fi==T.fi) cur--;
while(cur>=2 && G[cur]<=I(st[cur],T)) cur--;
st[++cur]=T;
G[cur]=I(st[cur],st[cur-1]);
int l=2,m,r=cur,o=1;
while(l<=r)
{
m=(l+r)/2;
if(G[m]>=x[i]) o=m,l=m+1;
else r=m-1;
}
F[i]=(st[o].fi+s[i])*x[i] + st[o].se;
P[c][i]=st[o].rd;
}
for(int i=c;i<n;++i) H[i]=F[i];
}
for(int i=k;i<n;++i) if(ma<=F[i]) ma=F[i],it=i;
for(;k>0;k--) ans.push_back(it),
it=P[k][it];
cout<<ma<<"\n";
reverse(ans.begin(),ans.end());
for(auto i:ans) cout<<i<<" ";
}
Compilation message (stderr)
sequence.cpp:23:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
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... |