sdut2169:Sequence(dp)

时间:2023-12-24 09:06:31

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2169

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll n,m;
ll sum;
ll w[],num[],dp[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
sum=;
scanf("%lld%lld",&n,&m);
memset(num,,sizeof(num));
for(int i=;i<=n;i++)
{
scanf("%d",&w[i]);
num[i]=num[i-]+w[i];
dp[i]=num[i]*num[i];
sum+=w[i];
}
if(m==)
{
sum=sum*sum;
printf("%lld\n",sum);
continue;
}
bool F=false;
for(int j=;j<=m;j++)
{
for(int i=n-m+j;i>=j;i--)
{
for(int k=j-;k<i;k++)
{
dp[i]=min(dp[i],(dp[k]+(num[i]-num[k])*(num[i]-num[k])));
if(j==m&&k==i-)
{
F=true;
break;
}
}
if(F) break;
}
}
printf("%lld\n",dp[n]);
}
return ;
}