#include #include #include #include #include #include #include #include #include #include // #include #include #define int long long #define inf 1000000007 #define pa pair #define ll long long #define pal pair #define ppa pair #define ppap pair #define ssa pair #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b))b) return gcd(b,v); if(v==b) return b; if(b%v==0) return v; return gcd(v,b%v); } double distans(double x1,double y1,double x2,double y2){ double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); return sqrt(rr); } /* int pr[100010]; //int inv[100010]; */ int beki(int wa,int rr,int warukazu){ if(rr==0) return 1ll; if(rr==1) return wa%warukazu; if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu; int zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } /* void gya(){ pr[0]=1; for(int i=1;i<100010;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<100010;i++) inv[i]=beki(pr[i],inf-2); } */ //sort(ve.begin(),ve.end(),greater()); //----------------kokomade tenpure------------ //vector ans(100000000),ans2(100000000); /* int par[200100],ranks[200100],kosuu[200100]; void shoki(int n){ for(int i=0;i yama,tani; int sab(int j,int i){ return abs(b[i]-b[j]); } signed main(){ int p; cin>>n>>p; int cnt=1; a[0]=inf; me[1]=inf; int sa=0; bool bo=false; for(int i=1;i<=n;i++){ cin>>a[i]; a[i]+=sa; if(i>1){ int d=a[i]-a[i-1]; if(d>p){ a[i ] -= d-p; sa-= d-p; } if(d<-p){ a[i] += -d-p; sa += -d-p; } } } b[1]=a[1]; for(int i=2;i<=n;i++){ if(a[i]==a[i-1]) continue; cnt++; b[cnt]=a[i]; // cout<b[cnt+1]) cnt++; else break; } b[cnt-1]=inf; for(int i=cnt;i<=num-1;i++){ if(b[i]>b[i-1]&& b[i]>b[i+1]) yama.pb(i); if(b[i]b[num-1]) yama.pb(num); else tani.pb(num); // for(int i=0;iyama.size() && i==2*ta-1) break; if(i%2==1){ dp[i][0]=min({dp[i-1][0],dp[i-1][1],dp[i-1][2]}); dp[i][1]=sab(tani[i/2],yama[i/2]-1)+min({dp[i-1][0],dp[i-1][1],dp[i-1][2]}); dp[i][2]=sab(tani[i/2],yama[i/2])+min({dp[i-1][0],dp[i-1][1],dp[i-1][2]}); } else{ dp[i][0]=min({dp[i-1][0]+2*p,dp[i-1][1]+p,dp[i-1][2]}); dp[i][1]=sab(tani[i/2],yama[i/2-1]+1)+min({dp[i-1][0]+p,dp[i-1][1]+p,dp[i-1][2]}); dp[i][2]=sab(tani[i/2],yama[i/2-1])+min({dp[i-1][0],dp[i-1][1],dp[i-1][2]}); } // for(int j=0;j<3;j++) cout<yama.size()){ cout<