//非常有可能被卡常,且有可能卡WA
//期望得分:100
//实际得分:80-100
#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#define pf(x) ((ll)(x)*(ll)(x))
#define OPT __attribute__((optimize("O2")))
using namespace std;
typedef long long ll;
const int N=10005;
ll f[N][2],ans[N];
int n,m,a[N<<1],b[N],tp[30];
OPT inline int read(){
int 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*10+ch-‘0‘;ch=getchar();}
return x*f;
}
OPT inline ll DP(int start){
if(ans[start]) return ans[start];
for(int cnt=0,i=start;i<=start+n;i++) b[++cnt]=a[i];
f[1][0]=f[1][1]=0;
for(int i=2;i<=n+1;i++){
if(b[i]>b[i-1]){
f[i][1]=min(f[i-1][0]+m+b[i]-b[i-1],f[i-1][1]+b[i]-b[i-1]);
f[i][0]=min(f[i-1][1]+m+pf(b[i]-b[i-1]),f[i-1][0]+pf(b[i]-b[i-1]));
}
else{
f[i][0]=min(f[i-1][1]+m+b[i-1]-b[i],f[i-1][0]+b[i-1]-b[i]);
f[i][1]=min(f[i-1][0]+m+pf(b[i]-b[i-1]),f[i-1][1]+pf(b[i]-b[i-1]));
}
}
return ans[start]=min(f[n+1][1],f[n+1][0]);
}
OPT int main(){
srand(time(0));
n=read();m=read();
for(int i=1;i<=n;i++) a[i+n]=a[i]=read();
for(int i=1;i<=18;i++) tp[i]=rand()%n+1;
for(double T=n;T>=10;T*=0.5){
for(int i=1,now;i<=18;i++){
for(int j=1;j<=21;j++){
now=tp[i]+sin((double)(rand()%10000)/10000)*T;
if(now<=0||now>n) continue;
if(DP(now)>=DP(tp[i])) continue;
tp[i]=now;
}
}
}
ll Ans=0x7fffffffffffffffLL;
for(int i=1;i<=18;i++) Ans=min(Ans,ans[tp[i]]);
cout<<Ans;
return 0;
}
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define pf(x) (1LL*(x)*(x))
using namespace std;
typedef long long ll;
inline int read(){
int 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*10+ch-‘0‘;ch=getchar();}
return x*f;
}
const int N=10005;
int n,m,a[N<<1];
ll f[N][2][2],Ans;
void DP(){
for(int i=1;i<=n;i++){
for(int j=0;j<2;j++){
if((a[i]<a[i-1])^j){
f[i][j][0]=f[i-1][j][0]+abs(a[i]-a[i-1]);
f[i][j][1]=min(f[i-1][j][1],
min(f[i-1][j^1][0],f[i-1][j^1][1])+m)+abs(a[i]-a[i-1]);
}
else{
f[i][j][0]=f[i-1][j][0]+pf(a[i]-a[i-1]);
f[i][j][1]=min(f[i-1][j][1],
min(f[i-1][j^1][0],f[i-1][j^1][1])+m)+pf(a[i]-a[i-1]);
}
}
}
}
int main(){
n=read();m=read();
for(int i=0;i<n;i++) a[i]=read();a[n]=a[0];
//情况1、2
memset(f,0,sizeof f);memset(f[0],0x3f,sizeof f[0]);
f[0][0][0]=f[0][1][0]=0;;
DP();
Ans=min(min(f[n][0][0],f[n][0][1]),min(f[n][1][0],f[n][1][1]));
//情况3
memset(f,0,sizeof f);memset(f[0],0x3f,sizeof f[0]);
f[0][0][0]=0;
DP();Ans=min(Ans,f[n][0][1]-m);
//情况3
memset(f,0,sizeof f);memset(f[0],0x3f,sizeof f[0]);
f[0][1][0]=0;
DP();Ans=min(Ans,f[n][1][1]-m);
cout<<Ans;
return 0;
}