#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> bool maximize(T &a, const T &b){ return (a < b ? a = b, 1 : 0); }
template<class T> bool minimize(T &a, const T &b){ return (a > b ? a = b, 1 : 0); }
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define all(x) x.begin(), x.end()
#define TASK "nonsense"
/// end of template ///
const int N = 1e5 + 5;
const int K = 200 + 5;
int n,k,a[N],p[N];
namespace sub12
{
const int N = 50+1;
const ll inf = LLONG_MAX/2;
bool check()
{
return (n<N);
}
ll dp[N][N][N];
ii trace[N][N][N];
void work(int tri, int i, int j)
{
if(i==j || tri==0) return;
int tri1=trace[tri][i][j].fi;
int pos=trace[tri][i][j].se;
cout<<pos<<' ';
work(tri1,i,pos);
work(tri-1-tri1,pos+1,j);
}
void solve()
{
for(int tri=1;tri<=k;++tri) for(int i=1;i<=n;++i) for(int j=i;j<=n;++j)
{
dp[tri][i][j]=-inf;
// bool dbg=(tri==3 && i==1 && j==7);
for(int tri1=0;tri1<=tri-1;++tri1)
{
int tri2=tri-1-tri1;
// cout<<tri<<' '<<tri1<<' '<<tri2<<'\n';
for(int pos=i;pos<j;++pos)
{
// if(dbg) cout<<tri<<' '<<tri1<<' '<<tri2<<' '<<pos<<' '<<dp[tri1][i][pos]<<' '<<dp[tri2][pos+1][j]<<' '<<(p[j]-p[pos])*1LL*(p[pos]-p[i-1])<<'\n';
if(maximize(dp[tri][i][j],dp[tri1][i][pos]+dp[tri2][pos+1][j]+(p[j]-p[pos])*1LL*(p[pos]-p[i-1]))) trace[tri][i][j]=make_pair(tri1,pos);
}
}
// cout<<tri<<' '<<i<<' '<<j<<' '<<dp[tri][i][j]<<'\n';
}
cout<<dp[k][1][n]<<'\n';
work(k,1,n);
}
}
namespace sub34
{
bool check()
{
return (n<=1000);
}
const ll inf = 2e18;
const int N = 1e3 + 10;
const int K = 200 + 10;
ll dp[K][N];
int trace[K][N];
void solve()
{
for(int i=1;i<=n;++i) dp[0][i]=inf;
++k;
for(int tri=1;tri<=k;++tri) for(int i=0;i<=n;++i)
{
dp[tri][i]=inf;
for(int pos=1;pos<=i;++pos) if(minimize(dp[tri][i],dp[tri-1][pos-1]+(p[i]-p[pos-1])*1LL*(p[i]-p[pos-1]))) trace[tri][i]=pos;
}
cout<<(p[n]*1LL*p[n]-dp[k][n])/2<<'\n';
int tri=k,i=n;
// return;
while(tri>1)
{
// cout<<tri<<' '<<i<<'\n';
cout<<trace[tri][i]-1<<' ';
i=trace[tri][i]-1;
--tri;
}
}
}
void solve()
{
cin>>n>>k;
for(int i=1;i<=n;++i) cin>>a[i],p[i]=p[i-1]+a[i];
if(sub12::check()) return void(sub12::solve());
if(sub34::check()) return void(sub34::solve());
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
int testcase=1;
// cin>>testcase;
while (testcase--)
solve();
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... |