rt,未有任何优化,求助哪个地方写挂了。
其中,fi 表示装好前 i 个的最小花费,sn 表示 ∑i=1n(ci+1)。
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=5e4+6;
const int IM=2147483647;
const long long LLM=922337203685477580;
inline int read()
{
int x=0,y=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-') y=-y;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+(c^'0');c=getchar();}
return x*y;
}
int n,m,L,c[N],s[N],f[N];
int ksm(int a,int b)
{
int res=1;
while(b--) res*=a;
return res;
}
signed main()
{
n=read(),L=read();
for(int i=1;i<=n;i++) c[i]=read(),f[i]=LLM,s[i]=s[i-1]+c[i]+1;
f[0]=0;
for(int i=1;i<=n;i++)
{
f[i]=min(f[i],f[i-1]+ksm(c[i]-L,2));
for(int j=1;j<i;j++) f[i]=min(f[i],f[j]+ksm(s[i]-s[j]-1-L,2));
}
printf("%lld\n",f[n]);
return 0;
}