区间DP模板题
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read(){
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
ll a[506],sum[505];
ll dp1[505][505];
ll dp2[505][505];
int main()
{
int n,m;
n=read();
memset(dp2,999999,sizeof dp2);
for(int i=1;i<=n;i++)
{
a[i]=read();
a[i+n]=a[i];
}
for(int i=1;i<=2*n;i++)
{
sum[i]=sum[i-1]+a[i];
dp2[i][i]=0;
}
for(int len=1;len<=2*n;len++)//枚举长度
{
for(int s=1;len+s-1<=2*n;s++)//枚举起点
{
int end=s+len-1;//计算终点
for(int k=s;k<end;k++)//枚举区间所有分割点(起点,终点-1)个区间
{
dp1[s][end]=max(dp1[s][end],dp1[s][k]+dp1[k+1][end]+sum[end]-sum[s-1]);
dp2[s][end]=min(dp2[s][end],dp2[s][k]+dp2[k+1][end]+sum[end]-sum[s-1]);
}
}
}
ll maxn=0,minn=99999;
for(int i=1;i<=n;i++)
{
maxn=max(dp1[i][i+n-1],maxn);
minn=min(minn,dp2[i][i+n-1]);
}
cout<<minn<<endl;
cout<<maxn<<endl;
return 0;
}