比赛 清明时悲哀杯 评测结果 C
题目名称 石子合并 最终得分 0
用户昵称 Hyoi_ctime 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2017-03-31 20:47:51
显示代码纯文本
#include <iostream>
#include <cstring>

 
using namespace std;
 
 const int MaxInt=2147483646;
int n, Sands[101], Cost[101][101], Dp[101][101];
 
int Min(int a, int b) {
    return a < b ? a : b;
}
 
int main() {
    freopen("shizi.in","r",stdin);
    freopen("shizi.out","w",stdout);
    cin >> n;
    memset(Sands, 0, sizeof(Sands));
    memset(Cost, 0, sizeof(Cost));
    for (int i = 0; i < n; i++) cin >> Sands[i];
    for (int i = 0; i < n; i++)
        for (int j = i; j < n; j++)
            for (int k = i; k <= j; k++)
                Cost[i][j] += Sands[k];
    for (int t = 1; t < n; t++) {
        for (int i = 0; i < n; i++) {
            int j = i + t;
            Dp[i][j] = MaxInt;
            for (int k = i; k < j; k++) {
                Dp[i][j] = Min(Dp[i][k] + Dp[k + 1][j] + Cost[i][j], Dp[i][j]);
            }
        }
    }
    cout << Dp[0][n - 1] << endl;
    
    return (0);
}