AcWing 282. 石子合并
原题链接
简单
作者:
牛奶小柒Luke
,
2021-03-29 20:27:35
,
所有人可见
,
阅读 243
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 310;
int n;
int a[N],s[N];
int f[N][N];
int main(){
scanf("%d",&n);
for(int i = 1;i <= n;++i){
scanf("%d",&a[i]);
s[i] += s[i - 1] + a[i];
}
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 - 1;++k){
f[i][j] = min(f[i][j],f[i][k] + f[k + 1][j] + s[j] - s[i - 1]);
}
}
}
cout << f[1][n] << endl;
return 0;
}