//Solution By SlavicG
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
int main()
{
int n,k;
cin >> n >> k;
int a[n];
int dp[n];
forn(i,n)cin >> a[i];
forn(i,n)dp[i] = INT_MAX;
dp[0] = 0;
for(int i = 0;i<n;i++)
{
for(int j = 1 ; j <= k;j++)
{
if(i + j < n)dp[i+j] = min(dp[i + j], dp[i] + max(a[i + j] , a[i]) - min(a[i + j] , a[i]));
}
}
cout << dp[n-1] << endl;
}
Ly9Tb2x1dGlvbiBCeSBTbGF2aWNHCiNpbmNsdWRlICJiaXRzL3N0ZGMrKy5oIgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCiNkZWZpbmUgbGwgICAgICAgICAgICAgICAgICAgICAgICAgIGxvbmcgbG9uZwogCiNkZWZpbmUgZm9ybihpLG4pICAgICAgICAgICAgICAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspCiNkZWZpbmUgYWxsKHYpICAgICAgICAgICAgICAgICAgICAgIHYuYmVnaW4oKSwgdi5lbmQoKQojZGVmaW5lIHJhbGwodikgICAgICAgICAgICAgICAgICAgICB2LnJiZWdpbigpLHYucmVuZCgpCgkKI2RlZmluZSBwYiAgICAgICAgICAgICAgICAgICAgICAgICAgcHVzaF9iYWNrCiNkZWZpbmUgc3ooYSkgICAgICAgICAgICAgICAgICAgICAgIChpbnQpYS5zaXplKCkKIAojZGVmaW5lIGZhc3RpbyAgICAgICAgICAgICAgICAgICAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTtjaW4udGllKDApO2NvdXQudGllKDApOwojZGVmaW5lICAgZW5kbCAgICAgICAgICAgICAgICAgICAgICAiXG4iCgppbnQgbWFpbigpCnsKCWludCBuLGs7CgljaW4gPj4gbiA+PiBrOwogICAgaW50IGFbbl07CiAgICBpbnQgZHBbbl07CiAgICBmb3JuKGksbiljaW4gPj4gYVtpXTsKICAgIGZvcm4oaSxuKWRwW2ldID0gSU5UX01BWDsKICAgIGRwWzBdID0gMDsKICAgIGZvcihpbnQgaSA9IDA7aTxuO2krKykKICAgIHsKICAgICAgICBmb3IoaW50IGogPSAxIDsgaiA8PSBrO2orKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKGkgKyBqIDwgbilkcFtpK2pdID0gbWluKGRwW2kgKyBqXSwgZHBbaV0gKyBtYXgoYVtpICsgal0gLCBhW2ldKSAtIG1pbihhW2kgKyBqXSAsIGFbaV0pKTsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IGRwW24tMV0gPDwgZW5kbDsKfQ==