# 石子合并

（1）选择一种合并石子的方案，使得做n-1次合并，得分的总和最少

（2）选择一种合并石子的方案，使得做n-1次合并，得分的总和最多

### 输入样例:

4
4 5 9 4


### 输出样例:

44
54

### 题解


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> vec(101);
vector<vector<int>> dp_max(101, vector<int>(101, 0));
vector<vector<int>> dp_min(101, vector<int>(101, 0));
int max_num(int i, int j) {
if (dp_max[i][j] ) {
return dp_max[i][j];
}
if (i == j) {
return 0;
}
int sum = 0, MAX = -1;
for (int k = i; k <= j; k++) {
sum += vec[k];
}
for (int k = i; k < j; k++) {
MAX = max(max_num(i,k) + max_num(k+1,j) + sum, MAX);
}
dp_max[i][j]=MAX;
return MAX;
}
int min_num(int i, int j) {
if (dp_min[i][j]) {
return dp_min[i][j];
}
if (i == j) {
return 0;
}
int sum = 0, MIN =2010;
for (int k = i; k <= j; k++) {
sum += vec[k];
}
for (int k = i; k < j; k++) {
MIN = min(min_num(i,k) + min_num(k+1,j) + sum, MIN);
}
dp_min[i][j]=MIN;
return MIN;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
cout<< min_num(0,n-1)<<endl;
cout << max_num(0, n-1) ;
system("pause");
}