显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=3010;
long long dp[maxn][maxn],s[maxn];
int n,m,p[maxn][maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("menci_journey.in","r",stdin);
freopen("menci_journey.out","w",stdout);
#endif
scanf("%d%d",&n,&m);
memset(dp,63,sizeof(dp));
for(int i=1;i<=n;i++){
scanf("%lld",&s[i]);
s[i]+=s[i-1];
}
dp[0][0]=0;
for(int k=1;k<=m;k++){
for(int i=k;i<=n;i++){
for(int j=p[k][i-1];j<i;j++){
if(dp[k][i]>dp[k-1][j]+(s[i]-s[j])*(s[i]-s[j])){
dp[k][i]=dp[k-1][j]+(s[i]-s[j])*(s[i]-s[j]);
p[k][i]=j;
}
}
}
}
printf("%lld\n",1ll*dp[m][n]*m-s[n]*s[n]);
return 0;
}