AcWing 282. 石子合并
原题链接
简单
作者:
HalfSummer
,
2020-05-08 15:44:03
,
所有人可见
,
阅读 653
#include<iostream>
#include<algorithm>
using namespace std;
int a[305], s[305];
int dp[305][305];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s[i] = a[i] + s[i - 1];
}
for (int len = 2; len <= n; len++) {
for (int i = 1; i + len - 1 <= n; i++) {
int l = i, r = i + len - 1;
dp[l][r] = 0x3f3f3f3f;
for (int k = l; k < r; k++) {
dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r] + s[r] - s[l - 1]);
}
}
}
cout << dp[1][n];
}