n,d=map(int,input().split())
a=[int(x) for x in input().split()]
share=True
buy=a[0]
profit=0
for i in range(0,n):
if share:
if a[i]>=buy+d:
profit += a[i]-buy
sale=a[i]
share=False
else:
if a[i]<=sale-d:
buy=a[i]
share=True
print(profit)