易错点:
- 要清楚动态规划的基本概念和应对方式.
- 原题有环,处理环的话需要断开接一倍.
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=350;
int f[MAXN][MAXN],sum[MAXN],a[MAXN];
int main(){
memset(f,0x3f,sizeof(f));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int tmp;
scanf("%d",&tmp);
a[i]=tmp;
sum[i]+=sum[i-1]+tmp;
f[i][i]=0;
}
for(int len=2;len<=n;len++){
for(int l=1;l<=n-len+1;l++){
int r=l+len-1;
for(int k=l;k<r;k++){
f[l][r]=min(f[l][r],f[l][k]+f[k+1][r]+sum[r]-sum[l-1]);
}
}
}
printf("%d\n",f[1][n]);
return 0;
}