#include "stdio.h"
#include "string.h"
#define inf 2000000000;
int min(int a,int b)
{
if (a<b) return a; else return b;
}
int main()
{
int ii,t,n,i,j,k,s,e;
int dp[101][101],a[101],sum[101];
scanf("%d",&t);
for (ii=1;ii<=t;ii++)
{
scanf("%d",&n);
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for (i=1;i<=n;i++)
for (j=i+1;j<=n;j++)
dp[i][j]=inf;
for (i=2;i<=n;i++)
for (j=1;j<=n-i+1;j++)
{
s=j; e=j+i-1;
dp[s][e]=dp[s+1][e]+sum[e]-sum[s];
for (k=2;k<=i;k++)
dp[s][e]=min(dp[s][e],dp[s+k][e]+dp[s+1][s+k-1]+(k-1)*a[s]+k*(sum[e]-sum[s+k-1]));
}
printf("Case #%d: ",ii);
printf("%d\n",dp[1][n]);
}
return 0;
}
HDU 4283 区间DP
最新推荐文章于 2024-06-16 13:07:12 发布