链接:点我
就是自己写不出来
#include <cstdio>
#include <climits>
#include <memory.h>
using namespace std;
const int MAX = ; int dp[MAX][MAX];
int pos[MAX][MAX];
int val[MAX], sum[MAX][MAX]; void print_path(int i, int j){
if(i == j){
printf("%d", val[i]);
return;
}
printf("(");
print_path(i, pos[i][j]);
printf("+");
print_path(pos[i][j] + , j);
printf(")");
} int print_intermedia(int x, int y){
if(x == y)return val[x];
int v = print_intermedia(x, pos[x][y]) + print_intermedia(pos[x][y] + , y);
printf("%d ", v);
return v;
} int main(int argc, char const *argv[]){
int N;
scanf("%d", &N);
for(int i = ; i <= N; ++i){
scanf("%d", &val[i]);
}
memset(sum, -, sizeof(sum)); for(int i = ; i <= N; ++i){
dp[i][i] = ;
pos[i][i] = i;
sum[i][i]= val[i];
for(int j = i + ; j <= N; ++j){
sum[i][j] = sum[i][j - ] + val[j];
}
} for(int j = ; j < N; ++j){
for(int i = ; i + j <= N; ++i){
int opt_v = INT_MAX, opt_p = ;
for(int k = i + j - ; k >= i; --k){
if(opt_v > dp[i][k] + dp[k + ][i + j] + sum[i][i + j]){
opt_v = dp[i][k] + dp[k + ][i + j] + sum[i][i + j];
opt_p = k;
}
}
pos[i][i + j] = opt_p;
dp[i][i + j] = opt_v;
}
}
print_path(, N);
printf("\n");
printf("%d\n", dp[][N]);
print_intermedia(, N);
printf("\n"); return ;
}