CCCPaste

36c99805e39d6

#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 10;
const int INF = 2e9;
int n, a[maxn];
int k;
int dp[maxn];
int rec(int at) {
if(at == n - 1) {
return 0;
}
if(dp[at] != -1) {
return dp[at];
}

int res = INF;

for(int i = 1; i <= k; i++) {
if(at + i < n) {
int cost = abs(a[at] - a[at + i]);
res = min(res, rec(at + i) + cost);
}
}

dp[at] = res;
return res;
}
int main() {
ios::sync_with_stdio(false);
memset(dp, -1, sizeof dp);

cin >> n >> k;

for(int i = 0; i < n; i++) {
cin >> a[i];
}

cout << rec(0) << endl;
return 0;
}