Pre
Ready \ (PPT \) when it came to the subject a little magic.
Looking directly at the solution to a problem, or \ (PPT \) endless.
Solution
Consider \ (dp [i] [j ] \) representing the interval \ ([i, j] \ ) answer difficult in design equations.
Can be considered the first \ (i \) personal when the stack is assumed to be \ (k \) , then it must be attached after the \ (k \) individuals out of the stack, can not have back, otherwise illegal.
If it is larger than \ (j-i + 1 \ ) one out of the stack, leaving behind the recalculation.
So you can \ (DP \) a.
Code
#include <cstdio>
#include <limits.h>
#include <algorithm>
using namespace std;
const int N = 100 + 5;
int dp[N][N], ans, n, sum[N];
int main () {
int t;
scanf ("%d", &t);
for (int lhjakioi = 1; lhjakioi <= t; lhjakioi++) {
scanf ("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf ("%d", &sum[i]);
sum[i] += sum[i - 1];
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
int u = j, v = j + i - 1;
dp[u][v] = INT_MAX;
if (v > n) continue;
for (int k = 1; k <= i; ++k) {
dp[u][v] = min (dp[u][v], dp[u + 1][u + k - 1] + (sum[u] - sum[u - 1]) * (k - 1) + dp[u + k][v] + (sum[v] - sum[u + k - 1]) * k);
}
}
}
printf ("Case #%d: %d\n", lhjakioi, dp[1][n]);
// printf ("%d\n", dp[1][n]);
}
return 0;
}
Conclusion
This question can be put \ (PPT \) above to shareMinato time