石子合并

来源 : https://www.acwing.com/problem/content/284/

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 310;
int s[N], a[N];
int f[N][N];

int main() {
    IO;
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i];
    memset(f, 0x3f, sizeof f);
    for (int len = 1; len <= n; ++len)   
        for (int l = 1; l + len - 1 <= n; ++l) {
            int r = l + len - 1;
            if (l == r) f[l][r] = 0;
            for (int k = l; k < r; ++k) 
                f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);   
    }

    cout << f[1][n] << endl;
    return 0;
}
posted @ 2021-01-25 20:13  phr2000  阅读(36)  评论(0编辑  收藏  举报