AcWing 282. 石子合并
原题链接
简单
作者:
Value
,
2020-05-31 11:21:16
,
所有人可见
,
阅读 425
#include <iostream>
#include <cstring>
using namespace std;
const int N = 310;
int sum[N], f[N][N];
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i ++ ) cin >> sum[i];
for(int i = 1; i <= n; i ++ ) sum[i] += sum[i - 1];
memset(f, 0x3f3f3f, sizeof f);
for(int i = 1; i <= n; i ++ ) f[i][i] = 0; // 搬运一堆石子消耗体力为0
for(int len = 2; len <= n; len ++ ){
for(int i = 1; i + len - 1 <= n; i ++ ){
int st = i, ed = i + len - 1;
for(int k = st; k < ed; k ++ ){
f[st][ed] = min(f[st][ed], f[st][k] + f[k + 1][ed] + sum[ed] - sum[st - 1]);
}
}
}
cout << f[1][n] << endl;
return 0;
}