分析
简单的区间DP
AC代码
#include <bits/stdc++.h>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
typedef long long ll;
int Dp_max[305][305],Dp_min[305][305];
int a[305],sum[305];
inline int read(){
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
int main(int argc,char* argv[]){
int n=read();
for (int i=1;i<=n;i++) a[i]=read(),a[i+n]=a[i];
for (int i=1;i<=(n<<1);i++) sum[i]=sum[i-1]+a[i];
for (int i=(n<<1);i>=1;i--) {
for (int j=i+1;j<i+n;j++) {
Dp_min[i][j]=1<<30;
for (int k=i;k<j;k++) {
Dp_max[i][j]=max(Dp_max[i][j],Dp_max[i][k]+Dp_max[k+1][j]+sum[j]-sum[i-1]);
Dp_min[i][j]=min(Dp_min[i][j],Dp_min[i][k]+Dp_min[k+1][j]+sum[j]-sum[i-1]);
}
}
}
int ans1=1<<30,ans2=0;
for (int i=1;i<=n;i++) {
ans1=min(ans1,Dp_min[i][i+n-1]);
ans2=max(ans2,Dp_max[i][i+n-1]);
}
printf("%d
%d
",ans1,ans2);
return 0;
}