#33813: 解題報告


s11104220@school.saihs.edu.tw (施同學)


我的答案
ns=input().split(' ')
price=input().split(' ')
ans=0
has=True
haspos=0
for i in range(1,len(price)):
    if has and (int(price[i])>=int(price[haspos])+int(ns[1])):
        ans+=int(price[i])-int(price[haspos])
        haspos=i
        has=False
    elif has==False and (int(price[i])<=int(price[haspos])-int(ns[1])):
        haspos=i
        has=True
print(ans)