定义
DP
是指在定义状态的时候定义了一个区间,我们根据区间长度len
由小到大逐步递推。问题描述
分析
s
表示前缀和数组):代码
#include
using namespace std;
const int N = 310;
int n;
int s[N];
int f[N][N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++) s[i] += s[i - 1];
for (int len = 2; len <= n; len++)
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1;
f[i][j] = 1e8;
for (int k = i; k < j; k++)
f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j] + s[j] - s[i - 1]);
}
printf("%d\n", f[1][n]);
return 0;
}