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 ll long long
using namespace std;
const int N=1e5+5;
const int K=205;
int n,k,a[N],opt[K][N];
ll s[N],dp[2][N];
void calc(int lev,int l,int r,int L,int R)
{
if(l>r) return ;
int mid=(l+r)/2;
dp[1][mid]=1e18;
for(int i=L;i<=min(mid-1,R);i++)
{
if(dp[1][mid]>dp[0][i]+(s[mid]-s[i])*(s[mid]-s[i]))
{
dp[1][mid]=dp[0][i]+(s[mid]-s[i])*(s[mid]-s[i]);
opt[lev][mid]=i;
}
}
calc(lev,l,mid-1,L,opt[lev][mid]);
calc(lev,mid+1,r,opt[lev][mid],R);
}
main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>k;
k++;
for(int i=1;i<=n;i++)
{
cin>>a[i];
s[i]=s[i-1]+a[i];
}
ll res=s[n]*s[n];
dp[0][0]=1e18;
dp[1][0]=1e18;
for(int i=1;i<=n;i++)
{
dp[0][i]=s[i]*s[i];
opt[1][i]=i;
}
for(int lev=2;lev<=k;lev++)
{
calc(lev,1,n,0,n-1);
for(int j=0;j<=n;j++) dp[0][j]=dp[1][j];
}
res-=dp[0][n];
cout<<res/2<<'\n';
int cur=n;
while(k>1)
{
cur=opt[k][cur];
k--;
cout<<cur<<' ';
}
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
sequence.cpp:27:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
27 | 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... |