AcWing 282. 石子合并
原题链接
简单
作者:
acdongla
,
2021-03-12 13:57:16
,
所有人可见
,
阅读 342
#include <iostream>
using namespace std;
const int N = 310;
int n;
int s[N], f[N][N];
int main(void) {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
s[i] += s[i - 1];
}
// len=1时只有1堆石子,代价为0
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]);
}
cout << f[1][n];
return 0;
}