蒟蒻不会四边形不等式
形如
fi,j=min{fi,k+fk+1,j+wi,j}
的方程 且满足四边形不等式和区间包含单调性 就可以用四边形不等式优化
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=2005;
int n;
int a[N]; ll sum[N];
ll f[N][N]; int s[N][N];
inline ll w(int l,int r){
return sum[r]-sum[l-1];
}
int main(){
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); for (int i=1;i<=n;i++) read(a[i]),a[n+i]=a[i]; n<<=1;
for (int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
memset(f,0x3f,sizeof(f));
for (int i=1;i<=n;i++) f[i][i]=0,s[i][i]=i;
for (int i=2;i<=n/2;i++)
for (int l=1;l+i-1<=n;l++){
int r=l+i-1;
for (int k=s[l][r-1];k<=s[l+1][r];k++)
if (f[l][r]>f[l][k]+f[k+1][r]+w(l,r))
f[l][r]=f[l][k]+f[k+1][r]+w(l,r),s[l][r]=k;
}
ll ans=1LL<<30;
for (int i=1;i<=n/2;i++)
ans=min(ans,f[i][i+n/2-1]);
printf("%lld\n",ans);
return 0;
}