题意和思路:参照大牛博客点击打开链接
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100000
#define LL long long
#define inf 1e9
int cas=1,T;
int dp[105][105];
int val[105],sum[105];
int main()
{
int n;
//freopen("in","r",stdin);
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
sum[0]=0;
for (int i = 1;i<=n;i++)
{
scanf("%d",&val[i]);
sum[i]=sum[i-1]+val[i];
}
memset(dp,0,sizeof(dp));
for (int i = 1;i<=n;i++)
for (int j = i+1;j<=n;j++)
dp[i][j]=inf;
for (int l=1;l<=n-1;l++)
for (int i = 1;i<=n-l;i++)
{
int j = i+l;
for (int k = i;k<=j;k++)
dp[i][j]=min(dp[i][j],val[i]*(k-i)+(k-i+1)*(sum[j]-sum[k])+dp[i+1][k]+dp[k+1][j]);
}
printf("Case #%d: %d\n",cas++,dp[1][n]);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}