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<iostream>
#include<deque>
using namespace std;
long long d[100007][201],n,k,a[200000],A[200000];
int i,j;
deque<int> q;
int m(int x,int y)
{
return (A[i]-A[y])*A[y]+d[y][j-1]-(A[i]-A[x])*A[x]-d[x][j-1];
}
int main()
{
cin>>n>>k;
for(i=1;i<=n;i++)
{
cin>>a[i];
A[i]=A[i-1]+a[i];;
}
for(j=1;j<=k;j++)
{
q.clear();
for(i=j;i<=n;i++)
{
while(q.size()>=2 && m(q[0],q[1])>=0)
q.pop_front();
if (!q.empty())
d[i][j] = (A[i]-A[q[0]])*A[q[0]]+d[q[0]][j-1];
while (q.size() >= 2 && m(q[q.size()-2], q[q.size()-1])< m(q[q.size()-1],i))
q.pop_back();
q.push_back(i);
}
}
cout<<d[n][k]<<endl;
int l=n,r=k;
for(i=n-1;i>=1;i--)
{
if((A[l]-A[i])*A[i]+d[i][r-1]==d[l][r])
{
cout<<i<<" ";
r--;
if(r==0)
break;
l=i;
}
}
return 0;
}
/*
7 3
4 1 3 4 0 2 3*/
# | 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... |