1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<algorithm>
6 #include<ctime>
7 #include<cstdlib>
8 using namespace std;
9 long long n,l,c,a[50010],sum[50010],q[50010],f[50010];
10 inline int read()
11 {
12 int x=0,f=1; char ch=getchar();
13 while(!isdigit(ch)) {if(ch==‘-‘) f=-1; ch=getchar();}
14 while(isdigit(ch)) {x=x*10+ch-‘0‘; ch=getchar();}
15 return x*f;
16 }
17 double slop(int x,int y) //计算斜率
18 {return (f[x]-f[y]+sum[x]*sum[x]-sum[y]*sum[y])*1.0/(sum[x]-sum[y]);}
19 int main()
20 {
21 n=read(); l=read(); c=l+1;
22 for(int i=1;i<=n;i++) a[i]=read();
23 for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
24 for(int i=1;i<=n;i++) sum[i]+=i;
25 int l=0,r=0;
26 for(int i=1;i<=n;i++)
27 {
28 while(l<r&&slop(q[l],q[l+1])<=2*(sum[i]-c)) l++;
29 int t=q[l];
30 f[i]=f[t]+(sum[i]-sum[t]-c)*(sum[i]-sum[t]-c);
31 while(l<r&&slop(q[r],i)<slop(q[r-1],q[r])) r--;
32 q[++r]=i;
33 }
34 printf("%lld\n",f[n]);
35 return 0;
36 }