1 #include<cstdio>
2 #include<cstring>
3 #include<cmath>
4 #include<ctime>
5 #include<cstdlib>
6 #include<iostream>
7 #include<algorithm>
8 #define clr(a,x) memset(a,x,sizeof(a))
9 #define rep(i,l,r) for(int i=l;i<r;i++)
10 typedef long long ll;
11 using namespace std;
12 int read()
13 {
14 char c=getchar();
15 int ans=0,f=1;
16 while(!isdigit(c)){
17 if(c==‘-‘) f=-1;
18 c=getchar();
19 }
20 while(isdigit(c)){
21 ans=ans*10+c-‘0‘;
22 c=getchar();
23 }
24 return ans*f;
25 }
26 #define A(i) (s[i]+i)
27 #define B(i) (A(i)+1+l)
28 #define X(k,j) (d[k]+B(k)*B(k)-d[j]-B(j)*B(j))
29 #define Y(k,j) (B(k)-B(j))
30 const int maxn=50050,inf=0x3fffffff;
31 ll c[maxn],s[maxn],d[maxn],q[maxn];
32 int main()
33 {
34 clr(c,0),clr(s,0),clr(d,0),clr(q,0);
35 ll n=read(),l=read();
36 rep(i,1,n+1){
37 c[i]=read();
38 d[i]=inf;
39 }
40 s[1]=c[1];
41 rep(i,2,n+1) s[i]=s[i-1]+c[i];
42 int front=0,rear=0;
43 rep(i,1,n+1){
44 while(rear>front&&(X(q[front+1],q[front])<=Y(q[front+1],q[front])*2*A(i))) front++;
45 d[i]=d[q[front]]+(A(i)-B(q[front]))*(A(i)-B(q[front]));
46 while(rear>front&&X(i,q[rear])*Y(q[rear],q[rear-1])*2<=X(q[rear],q[rear-1])*Y(i,q[rear])*2) rear--;
47 q[++rear]=i;
48 }
49 printf("%lld\n",d[n]);
50 return 0;
51 }