#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 111
#define maxm 8111111
#define INF 11111111111
long long dp[maxn][maxn], f[maxn][maxn];
int n;
long long a[maxn];
long long sum[maxn];
int main () {
//freopen ("in", "r", stdin);
int t, kase = 0;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
sum[0] = 0;
for (int i = 1; i <= n; i++)
scanf ("%lld", &a[i]);
for (int i = 1; i <= n; i++)
sum[i] = sum[i-1]+a[i];
memset (f, 0, sizeof f);
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
f[i][j] = f[i+1][j]+a[i]*(j-i);
}
}
for (int i = 0; i <= n+1; i++) {
for (int j = 0; j <= n+1; j++) {
dp[i][j] = INF;
}
}
for (int i = 1; i <= n; i++)
dp[i][i] = 0;
for (int l = 1; l <= n; l++) {
for (int i = 1; i+l <= n; i++) {
for (int j = i; j+1 <= i+l; j++) {
int cnt1 = j-i+1, cnt2 = i+l-j;
int sum1 = sum[j]-sum[i-1], sum2 = sum[i+l]-sum[j];
dp[i][i+l] = min (dp[i][i+l], min (dp[i][j]+dp[j+1][i+l]+sum2*cnt1, dp[j+1][i+l]+f[i][j]+sum1*cnt2));
}
}
}
printf ("Case #%d: %lld\n", ++kase, dp[1][n]);
}
return 0;
}
HDU4283(区间DP)
最新推荐文章于 2021-08-17 16:13:10 发布