Language: C++

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
const int Maxn=200010;
const LL inf=(1LL<<61);
LL read()
{
	LL 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<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*f;
}
int n,a[Maxn];LL d,v[3][Maxn];
struct Node{int x,y;Node(int _x,int _y){x=_x,y=_y;}};
struct Seg{int l,r,lc,rc,pos,pos1;}tr[3][Maxn<<1];
int tot[3];
void up(int o,int x)
{
	int lc=tr[o][x].lc,rc=tr[o][x].rc;
	if(v[o][tr[o][lc].pos]<v[o][tr[o][rc].pos])tr[o][x].pos=tr[o][lc].pos,tr[o][x].pos1=tr[o][lc].pos1;
	else tr[o][x].pos=tr[o][rc].pos,tr[o][x].pos1=tr[o][rc].pos1;
}
void build(int o,int l,int r)
{
	int x=++tot[o];
	tr[o][x].l=l;tr[o][x].r=r;
	if(l==r)
	{
		tr[o][x].pos=l;tr[o][x].pos1=l;
		return;
	}
	int mid=l+r>>1;
	tr[o][x].lc=++tot[o],build(o,l,mid);
	tr[o][x].rc=++tot[o],build(o,mid+1,r);
	up(o,x);
}
int O;
void modify(int o,int x,int p)
{
	if(tr[o][x].l==tr[o][x].r)
	{
		if(o==0)tr[o][x].pos1=O;
		return;
	}
	int mid=tr[o][x].l+tr[o][x].r>>1,lc=tr[o][c].lc,rc=tr[o][x].rc;
	if(p<=mid)modify(o,lc,l,mid,p);
	else modiyf(o,rc,mid+1,r,p);
	up(o,x);
}
int query(int o,int x,int l,int r)
{
	if(l>r)return 0;
	if(tr[o][x].l==l&&tr[o][x].r==r)
	{
		if(o==0)O=tr[o][x].pos1;
		return tr[o][x].pos;
	}
	int mid=tr[o][x].l+tr[o][x].r>>1,lc=tr[o][c].lc,rc=tr[o][x].rc;
	if(r<=mid)return query(o,lc,l,r);
	if(l>mid)return query(o,rc,l,r);
	int t1=query(o,lc,l,mid),t2=query(o,rc,mid+1,r);
	if(v[o][t1]<v[o][t2])return t1;return t2;
}
void Del(int Y2)
{
	v[2][Y2]=inf;modify(2,1,Y2);
	v[1][Y2]=inf;modify(1,1,Y2);
}
void Ins(int Y2)
{
	int t1=query(1,1,1,Y2-1),t2=query(2,1,Y2+1,n);
	if(v[1][t1]+a[Y2]-(LL)Y2*d<v[2][t2]+a[Y2]+(LL)Y2*d)
	{
		O=t1;
		v[0][Y2]=v[1][t1]+a[Y2]-(LL)Y2*d;
		modify(0,1,Y2);
	}
	else
	{
		O=t2;
		v[0][Y2]=v[2][t2]+a[Y2]+(LL)Y2*d;
		modify(0,1,Y2);
	}
}
int main()
{
	n=read(),d=read();
	for(int i=1;i<=n;i++)a[i]=read();
	v[0][0]=v[1][0]=v[2][0]=inf;
	for(int i=1;i<=n;i++)v[0][i]=inf;
	for(int i=1;i<=n;i++)v[1][i]=a[i]+(LL)i*d;
	for(int i=1;i<=n;i++)v[2][i]=a[i]-(LL)i*d;
	build(0,1,n),build(1,1,n),build(2,1,n);
	LL ans=0,tmp=a[n]+(LL)n*d,mn=inf;int xx,Y1=n;
	for(int i=n-1;i;i--)
	{
		if(a[i]-(LL)i*d+tmp<mn)mn=a[i]-(LL)i*d+tmp,xx=i;
		if(a[i]+(LL)i*d<tmp)tmp=a[i]+(LL)i*d,Y1=i;
	}
	tmp=a[1]-d;int Y2=1;bool flag=false;
	for(int i=2;i<=n;i++)
	{
		if(a[i]+(LL)i*d+tmp<mn)mn=a[i]+(LL)i*d+tmp,xx=i,flag=true;
		if(a[i]-(LL)i*d<tmp)tmp=a[i]-(LL)i*d,Y2=i;
	}
	ans+=mn;
	Del(xx);
	if(flag)Del(Y2);else Del(Y1);
	Ins(xx);
	if(flag)Ins(Y2);else Ins(Y1);
	int m=n-1;
	while(m--)
	{
		int t=query(0,1,1,n);
		ans+=v[0][t];
		Del(O);Ins(O);
	}
	printf("%lld",ans);
}

Compiler output:
a.cc: In function 'void modify(int, int, int)':
a.cc:46:51: error: 'c' was not declared in this scope; did you mean 'lc'?
   46 |         int mid=tr[o][x].l+tr[o][x].r>>1,lc=tr[o][c].lc,rc=tr[o][x].rc;
      |                                                   ^
      |                                                   lc
a.cc:47:31: error: 'l' was not declared in this scope; did you mean 'lc'?
   47 |         if(p<=mid)modify(o,lc,l,mid,p);
      |                               ^
      |                               lc
a.cc:48:23: error: 'rc' was not declared in this scope; did you mean 'lc'?
   48 |         else modiyf(o,rc,mid+1,r,p);
      |                       ^~
      |                       lc
a.cc:48:32: error: 'r' was not declared in this scope
   48 |         else modiyf(o,rc,mid+1,r,p);
      |                                ^
a.cc:48:14: error: 'modiyf' was not declared in this scope; did you mean 'modify'?
   48 |         else modiyf(o,rc,mid+1,r,p);
      |              ^~~~~~
      |              modify
a.cc: In function 'int query(int, int, int, int)':
a.cc:59:51: error: 'c' was not declared in this scope; did you mean 'lc'?
   59 |         int mid=tr[o][x].l+tr[o][x].r>>1,lc=tr[o][c].lc,rc=tr[o][x].rc;
      |                                                   ^
      |                                                   lc
a.cc:61:33: error: 'rc' was not declared in this scope; did you mean 'lc'?
   61 |         if(l>mid)return query(o,rc,l,r);
      |                                 ^~
      |                                 lc
a.cc:62:45: error: 'rc' was not declared in this scope; did you mean 'lc'?
   62 |         int t1=query(o,lc,l,mid),t2=query(o,rc,mid+1,r);
      |                                             ^~
      |                                             lc