#include<iostream>
using namespace std;
int a[105];
int main(){
int n,D;
cin>>n>>D;
for(int i = 0;i < n;i++) cin>>a[i];
int stock = a[0],profit = 0,last_sell = 0,having = 1;
for(int i = 1;i < n;i++)
{
if(a[i] >= stock + D && having == 1)
{
profit += a[i] - stock;
last_sell = stock;
having = 0;
}
if(a[i] <= stock - D && having == 0)
{
stock = a[i];
having = 1;
}
}
cout<<profit;
}