#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAX_N = 1e5;
int n, k;
int h[MAX_N];
int memo[MAX_N];
int dp(int i) {
if (i == 0) {
return 0;
}
if (memo[i] != -1) {
return memo[i];
}
int result = 1e9;
for (int j = 1; j <= k; ++j) {
if (i - j >= 0) {
result = min(result, dp(i - j) + abs(h[i] - h[i - j]));
}
}
memo[i] = result;
return memo[i];
}
int main() {
cin >> n >> k;
memset(memo, -1, sizeof(memo));
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
cout << dp(n - 1);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y3N0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBNQVhfTiA9IDFlNTsKCmludCBuLCBrOwppbnQgaFtNQVhfTl07CmludCBtZW1vW01BWF9OXTsKCmludCBkcChpbnQgaSkgewogICAgaWYgKGkgPT0gMCkgewogICAgICAgIHJldHVybiAwOwogICAgfQoKICAgIGlmIChtZW1vW2ldICE9IC0xKSB7CiAgICAgICAgcmV0dXJuIG1lbW9baV07CiAgICB9CiAgICAKICAgIGludCByZXN1bHQgPSAxZTk7CiAgICBmb3IgKGludCBqID0gMTsgaiA8PSBrOyArK2opIHsKICAgIAlpZiAoaSAtIGogPj0gMCkgewogICAgCQlyZXN1bHQgPSBtaW4ocmVzdWx0LCBkcChpIC0gaikgKyBhYnMoaFtpXSAtIGhbaSAtIGpdKSk7CiAgICAJfQogICAgfQogICAgCiAgICBtZW1vW2ldID0gcmVzdWx0OwogICAgcmV0dXJuIG1lbW9baV07Cn0KCmludCBtYWluKCkgewogICAgY2luID4+IG4gPj4gazsKCiAgICBtZW1zZXQobWVtbywgLTEsIHNpemVvZihtZW1vKSk7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBjaW4gPj4gaFtpXTsKICAgIH0KCiAgICBjb3V0IDw8IGRwKG4gLSAxKTsKCiAgICByZXR1cm4gMDsKfQ==