#include<iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int purchase,n,D,y,profit=0;
bool haveStock=true;
cin>>n>>D>>purchase;
for(int i=1;i<n;i++){
cin>>y;
if(haveStock&&y>=purchase+D){
haveStock=false;
int p=y-purchase;
profit+=p;
purchase=y;
}
if(!haveStock&&y<=purchase-D){
haveStock=true;
purchase=y;
}
}
cout<<profit<<'\n';
return 0;
}