#include <iostream>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <memory>
#include <iterator>
#include <stdexcept>
#include <utility>
#include <string>
#include <sstream>
#include <cmath>
#include <queue>
#include <numeric>
#include <functional>

using namespace std;
using ll = long long;

#define testcases int testcases;\
cin >> testcases;\
for (int testcase = 0; testcase < testcases; testcase++)

#define input(...) long __VA_ARGS__;\
inputhelper(__VA_ARGS__);

#define gerzytet ios_base::sync_with_stdio(false);\
cin.tie(0), cout.tie(0);\
int N;

const int MAX_INT = numeric_limits<int>().max();
const int MIN_INT = numeric_limits<int>().min();
const ll MAX_LL = numeric_limits<ll>().max();
const ll MIN_LL = numeric_limits<ll>().min();


template<typename T>
string convert(T t) {
    ostringstream out;
    out << t;
    return out.str();
}

string convert(string s) {
    return '"' + s + '"';
}

string convert(const char* s) {
    string out("\"");
    return out + s + "\"";
}

template<typename T, typename V>
string convert(pair<T, V> p) {
    return "(" + convert(p.first) + ' ' + convert(p.second) + ")";
}

template<class Iterator>
string convert(Iterator start, Iterator end) {
    string out = "";
    out += '{';
    for (auto s = start; s != end; s++) {
        out += convert(*s) + ' ';
    }
    out += '}';
    return out;
}
template<typename T> string convert(vector<T> container) { return convert(container.begin(), container.end()); }
template<typename T> string convert(set<T> container) { return convert(container.begin(), container.end()); }
template<typename T, typename V> string convert(map<T, V> container) { return convert(container.begin(), container.end()); }
template<typename T> string convert(unordered_set<T> container) { return convert(container.begin(), container.end()); }
template<typename T, typename V> string convert(unordered_map<T, V> container) { return convert(container.begin(), container.end()); }

template<typename... Args>
void debug(int id, Args... args) {
    cout << '(' << id << "):";
    ostream* rubbish[] = { &(cout << ' ' << convert(args))... };
    cout << '\n';
}

template<typename T = int>
vector<T> listinput(int length) {
    vector<T> out(length);
    for (int i = 0; i < length; i++) {
        cin >> out[i];
    }
    return out;
}

template<typename T = int>
vector<T> listinput() {
    int length;
    cin >> length;
    return listinput<T>(length);
}


template<typename T>
void inputhelper(T& t) {
    cin >> t;
}

template<typename... Args, typename T>
void inputhelper(T& t, Args&...args) {
    cin >> t;
    inputhelper(args...);
}

template<typename T = int>
vector<vector<T>> gridinput(int rows, int cols) {
    vector<vector<T>> out(rows, vector<T>(cols));
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            cin >> out[r][c];
        }
    }
    return out;
}

int main() {
    gerzytet;

    input(n, k);
    vector<int> stones = listinput(n);

    vector<int> DP(n);
    DP[0] = 0;

    for (int i = 1; i < n; i++) {
        int best = MAX_INT;
        for (int j = i - 1; j >= max(0L, i-k); j--) {
            best = min(best, abs(stones[i] - stones[j]) + DP[j]);
        }
        DP[i] = best;
    }

    cout << DP[n - 1] << '\n';
}

Measured memory_bytes: 3992