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;
int ar[100005];
struct line{
long long m,c;
int num;
line(int mm=0,int cc=0,int i=0){
m=mm;
c=cc;
num=i;
}
long long pnt(int x){
return m*x+c;
}
};
bool better(line a,line b,line c){
return (c.c-a.c)*(a.m-b.m)<=(b.c-a.c)*(a.m-c.m);
}
struct hull{
deque<line>dq;
void add(line x){
while(dq.size()>1&&better(dq[dq.size()-2],dq.back(),x)){
dq.pop_back();
}
dq.push_back(x);
}
pair<long long,int> fbest(long long x,int mxl){
while(dq.size()>1&&dq.front().num<mxl&&(dq.front().pnt(x)<=dq[1].pnt(x))){
dq.pop_front();
}
if(dq.empty()){
return {0,0};
}
return {dq.front().pnt(x),dq.front().num};
}
}dp[205];
long long sum[100005];
int path[205][100005];
vector<int>v;
int main(){
int n,k;
cin>>n>>k;
long long sm=0;
for(int i=1;i<=n;i++){
cin>>ar[i];
sm+=ar[i];
sum[i]=sm;
}
long long ans=0;
long long lnum;
//cerr<<"work"<<endl;
for(int i=1;i<=k;i++){
for(int j=i;j<n;j++){
//cerr<<"i:j "<<i<<" "<<j<<endl;
pair<long long,int>p=dp[i].fbest(sum[j],j);
//cerr<<"after fbest"<<endl;
long long x=p.first;
//cerr<<"x:"<<x<<endl;
int pn=p.second;
long long tans=x+sm*sum[j]-sum[j]*sum[j];
//cerr<<"ans:"<<tans<<endl;
long long nm=sum[j];
long long nc=tans-sm*sum[j];
dp[i+1].add(line(nm,nc,j));
//cerr<<"after add"<<endl;
path[i][j]=pn;
if(i==k){
if(tans>ans){
ans=tans;
lnum=j;
}
}
}
}
//cerr<<"out"<<endl;
cout<<ans<<"\n";
v.push_back(lnum);
for(int i=k;i>=2;i--){
lnum=path[i][lnum];
v.push_back(lnum);
}
for(int i=v.size()-1;i>=0;i--){
cout<<v[i]<<" ";
}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:77:16: warning: 'lnum' may be used uninitialized in this function [-Wmaybe-uninitialized]
77 | v.push_back(lnum);
| ~~~~~~~~~~~^~~~~~
# | 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... |