原文链接:https://www.luogu.com.cn/problem/P1880
AC代码:
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
int n,mapp[105],dp[205][205],sum[205],maxn=0,dp1[205][205],minx=1e8;
int main(){
int i,j,p;
cin>>n;
memset(mapp,0,sizeof(mapp));
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
for(i=0;i<105;i++){
for(j=0;j<105;j++){
dp1[i][j]=1e8;
}
dp1[i][i]=0;
}
for(i=1;i<=n;i++){
cin>>mapp[i];
sum[i]=sum[i-1]+mapp[i];
}
for(i=n+1;i<=2*n;i++){
sum[i]=sum[i-1]+mapp[i-n];
}
for(int len=1;len<n;len++){
for(i=1;i+len<=2*n;i++){
j=i+len;p=1e8;
for(int k=i;k<j;k++){
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);
maxn=max(maxn,dp[i][j]);
p=min(p,dp1[i][k]+dp1[k+1][j]);
}
dp1[i][j]=p+sum[j]-sum[i-1];
}
}
for(i=1;i<n;i++){
minx=min(dp1[i][i+n-1],minx);
}
cout<<minx<<endl;
cout<<maxn<<endl;
return 0;
}