#include<stdio.h>
main(){
int y, D, i, n, x, m=0//紀錄利潤, z=0//紀錄賣出買入;
scanf("%d%d", &n, &D);
scanf("%d", &x);
z=1;
for(i=0;i<n-1;i++){
scanf("%d", &y);
if(z==1){
if(y>=x+D){
m+=y-x;
x=y;
z=0;
}
}
else{
if(y<=x-D){
x=y;
z=1;
}
}
}
printf("%d", m);
}