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;
typedef long long ll;
typedef long double ld;
const int N=200+7;
int n,k;
int p[N];
int s(int l,int r)
{
return p[r]-p[l-1];
}
int dp[N][N][N];
int f[N][N][N];
int split[N][N][N];
bool viz[N];
void go(int cnt,int l,int r)
{
if(cnt>0)
{
cout<<split[cnt][l][r]<<" ";
go(f[cnt][l][r],l,split[cnt][l][r]);
go(cnt-1-f[cnt][l][r],split[cnt][l][r]+1,r);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("input","r",stdin);
//freopen("output","w",stdout);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
p[i]=p[i-1]+x;
}
int val;
for(int cnt=1;cnt<=k;cnt++)
{
for(int pa=0;pa<cnt;pa++)
{
int pb=cnt-1-pa;
for(int l=1;l<=n;l++)
{
for(int r=l;r<=n;r++)
{
for(int k=l;k<r;k++)
{
val=dp[pa][l][k]+dp[pb][k+1][r]+s(l,k)*s(k+1,r);
if(dp[cnt][l][r]<=val)
{
dp[cnt][l][r]=val;
f[cnt][l][r]=pa;
split[cnt][l][r]=k;
}
}
}
}
}
}
cout<<dp[k][1][n]<<"\n";
go(k,1,n);
cout<<"\n";
return 0;
for(int i=1;i<=n;i++)
{
if(viz[i])
{
cout<<i<<" ";
}
}
cout<<"\n";
return 0;
}
/**
**/
# | 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... |