stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include "bits/stdc++.h"

#pragma GCC optimize "03"

using namespace std;



#define int long long int

#define pb push_back

#define pii pair<int,int>

#define fi first

#define se second

#define rep(i,a,b) for (int i = a; i < b; ++i)

#define dbg(x) { cerr<<"> "<<#x<<": "<<x<< endl; }

#define dbg2(x,y) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<endl; }

#define dbg3(x,y,z) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<" , "<<#z<<": "<<z<<endl; }

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)

#ifndef LOCAL

#define endl '\n'

#endif

const int inf = INT_MAX;

const double eps = 0.0000001;

const double PI = acos(-1.0);

const int MOD = 1e9+7;

const int N  = 1e5+5;



int a[N], dp[N], k;



int calc (int n) {

	if (dp[n] == -1) {

		dp[n] = calc(n-1)+abs(a[n]-a[n-1]);

		for (int i = 2; i <= k; i++) {

			if (n-i >= 1)

			dp[n] = min(dp[n],calc(n-i)+abs(a[n]-a[n-i]));

		}

	}

	return dp[n];

}



signed main(){

    #ifdef LOCAL

        freopen("input.txt", "r", stdin);

        //freopen("output.txt", "w", stdout);

    #endif  

    IOS;



    memset(dp, -1, sizeof(dp));

    int n;

    cin >> n >> k;

    for (int i = 1; i <= n; i++) {

    	cin >> a[i];

    }

    dp[1] = 0;

    calc(n);

    cout << dp[n];

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.7941489525004835x):
#include "bits/stdc++.h"

#pragma GCC optimize "03"

using namespace std;



#define int long long int

#define pb push_back

#define pii pair<int,int>

#define fi first

#define se second

#define rep(i,a,b) for (int i = a; i < b; ++i)

#define dbg(x) { cerr<<"> "<<#x<<": "<<x<< endl; }

#define dbg2(x,y) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<endl; }

#define dbg3(x,y,z) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<" , "<<#z<<": "<<z<<endl; }

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)

#ifndef LOCAL

#define endl '\n'

#endif

const int inf = INT_MAX;

const double eps = 0.0000001;

const double PI = acos(-1.0);

const int MOD = 1e9+7;

const int N  = 1e5+5;



int a[N], dp[N];



signed main(){

    #ifdef LOCAL

        freopen("input.txt", "r", stdin);

        //freopen("output.txt", "w", stdout);

    #endif  

    IOS;



    int n, k;

    cin >> n >> k;

    for (int i = 1; i <= n; i++)

    	cin >> a[i];

    for (int i = 2; i <= n; i++) {

    	dp[i] = inf;

    	for (int j = 1; j <= k; j++) {

    		if (i - j >= 1) {

    			dp[i] = min (dp[i], dp[i-j]+abs(a[i]-a[i-j]));

    		}

    	}

    }

    cout << dp[n];

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;





#define x_ real()

#define y_ imag()

#define cross(a, b) (conj(a)*(b)).imag()

#define dot(a, b) (conj(a)*(b)).real()

#define PI acos(-1)

#define F first

#define S second

#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)

#define fileIO freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)

typedef long double ld;

typedef long long ll;

typedef unsigned long long ull;

typedef complex<ld> point;

typedef tuple<int, int, int> line;

typedef vector<point> polygon;

typedef pair<double, double> pd;

pair<int, int> dirs[] = { {1, 2}, {-1, 2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, -2}, {-1, -2} };

ll mod = 1e9 + 7;

ld eps = 1e-6;









int main() {

	fastIO;

	int n, k;

	cin >> n >> k;

	int dp[n+1], arr[n+1];

	for (int i = 0; i < n; i++) {

		cin >> arr[i];

	}

	dp[0] = 0;

	for (int i = 1; i < n; i++) {

		dp[i] = abs(arr[i-1]-arr[i]) + dp[i-1];

		for (int j = 2; j <= k; j++) {

			if (i - j < 0)

				continue;

			dp[i] = min(dp[i], abs(arr[i-j]-arr[i]) + dp[i-j]);

		}

	}



	cout << dp[n-1];



}


// Faster C++ solution for the same problem (measured speedup: 5.278270995092982x):
#include<stdio.h>

#include<stdlib.h>

#include<math.h>





typedef long long ll;





ll *mem, *inp, dist, min_dist, N;

int k;



int main()

{

    ll i, j;

    scanf("%lld%d", &N, &k);



    mem =(ll*) malloc(N * sizeof(ll));

    inp =(ll*) malloc(N * sizeof(ll));



    for(i = 0; i<N; ++i){

        scanf("%lld", inp+i);

    }



    *(mem+N-2) = llabs(*(inp+N-2) - *(inp+N-1));



    for(i = 3; i<=k+1 && i<=N; ++i)

        *(mem+N-i) = llabs(*(inp+N-i) - *(inp+N-1));



    for(i = N-(k+2); i>=0; --i){



        min_dist = llabs(*(inp+i) - *(inp+i+1)) + *(mem+i+1);

        for(j = 2; j<=k && i+j<N; ++j){

            dist = llabs(*(inp+i) - *(inp+i+j)) + *(mem+i+j);

            if (dist < min_dist) min_dist = dist;

        }



        *(mem+i) = min_dist;

    }



    printf("%lld\n", *mem);



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)



int f(int n) {

    if (n % 2 == 0) return n / 2;

    else return 3 * n + 1;

}



int main() {

    int s;

    cin >> s;

    int n = 1000000;

    vector<int> a(n);

    for (int i = 1; i <= n; i++) {

        if (i == 1) a[i] = s;

        else {

            a[i] = f(a[i-1]);

            for (int j = 1; j < i; j++) {

                if (a[j] == a[i]) {

                    cout << i << endl;

                    goto out;

                }

            }

        }

    }

    out:

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.047423028831771x):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)



int f(int n) {

    if (n % 2 == 0) return n / 2;

    else return 3 * n + 1;

}



int main() {

    int s;

    cin >> s;

    // int n = 1e6+1;

    // vector<int> a(n);

    int prev = s;

    set<int> m;

    m.insert(s);

    for (int i = 2; ; i++) {

        prev = f(prev);

        if (m.count(prev)) {

            cout << i << "\n";

            break;

        } else {

            m.insert(prev);

        }

    }

    // for (int i = 1; i <= n; i++) {

    //     if (i == 1) {

    //         a[i] = s;

    //     } else {

    //         a[i] = f(a[i-1]);

    //         for (int j = 1; j < i; j++) {

    //             if (a[j] == a[i]) {

    //                 cout << i << endl;

    //                 goto out;

    //             }

    //         }

    //     }

    // }

    // out:

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define all(x) (x).begin(),(x).end()



using namespace std;



const int MSIZE = 16;



int g[MSIZE][MSIZE];

long long Precalc[1 << MSIZE];

vector<int> indexes[1 << MSIZE];

long long dp[1 << MSIZE];



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    int n;

    cin >> n;

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++) {

            cin >> g[i][j];

        }

    }

    for (int mask = 0; mask < (1 << n); mask++) {

        for (int i = 0; i < n; i++) {

            for (int j = i + 1; j < n; j++) {

                if ((mask >> i) & (mask >> j) & 1) {

                    Precalc[mask] += g[i][j];

                }

            }

        }

        for (int i = 0; i < n; i++) {

            if (((mask >> i) & 1) == 0) {

                indexes[mask].push_back(i);

            }

        }

    }



    vector<pair<int,int> > masks(1 << n);

    for (int mask = 0; mask < (1 << n); mask++) {

        masks[mask] = make_pair(__builtin_popcount(mask), mask);

    }

    sort(all(masks));



    for (int i = 1; i < (1 << n); i++) {

        int mask = masks[i].second;

        int sz = indexes[mask].size();

        for (int mask2 = mask; mask2 > 0; mask2 = (mask2 - 1) & mask) {

            dp[mask] = max(dp[mask ^ mask2] + Precalc[mask2], dp[mask]);

        }

    }

    cout << dp[(1 << n) - 1] << endl;

}


// Faster C++ solution for the same problem (measured speedup: 2.6583564345321435x):
#include <bits/stdc++.h>

#define all(x) (x).begin(),(x).end()



using namespace std;



const int MSIZE = 16;



int g[MSIZE][MSIZE];

long long Precalc[1 << MSIZE];

long long dp[1 << MSIZE];



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);

  int n;

  cin >> n;

  for (int i = 0; i < n; i++) {

    for (int j = 0; j < n; j++) {

      cin >> g[i][j];

    }

  }

  for (int mask = 0; mask < (1 << n); mask++) {

    for (int i = 0; i < n; i++) {

      for (int j = i + 1; j < n && ((mask >> i) & 1); j++) {

        if ((mask >> j) & 1) {

          Precalc[mask] += g[i][j];

        }

      }

    }

  }

  for (int i = 1; i < (1 << n); i++) {

    int mask = i;

    for (int mask2 = mask; mask2 > 0; mask2 = (mask2 - 1) & mask) {

      dp[mask] = max(dp[mask ^ mask2] + Precalc[mask2], dp[mask]);

    }

  }

  cout << dp[(1 << n) - 1] << endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int n,k,a[100005],dp[100005];

int solve(int idx){

    if(idx >= n - 1)

        return 0;

    int &ans = dp[idx];

    if(ans != -1)

        return ans;

    ans = 1e9;

    for(int i = 1; i <= k; i++){

        if(idx + i < n)

            ans = min(ans, abs(a[idx] - a[idx + i]) + solve(idx + i));

    }

    return ans;

}

int main()

{

    ios::sync_with_stdio(0);

    cin >> n >> k;

    memset(dp , -1, sizeof dp);

    for(int i = 0; i < n; i++)

        cin >> a[i];

    cout << solve(0) << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.395834273124722x):
#include <bits/stdc++.h>

using namespace std;

int n,k,a[100005],dp[100005];

int solve(int idx){

    if(idx >= n - 1)

        return 0;

    int &ans = dp[idx];

    if(ans != -1)

        return ans;

    ans = 1e9;

    for(int i = 1; i <= k; i++){

        if(idx + i < n)

            ans = min(ans, abs(a[idx] - a[idx + i]) + solve(idx + i));

    }

    return ans;

}

int main()

{

    ios::sync_with_stdio(0);

    cin >> n >> k;

    for(int i = 0; i < n; i++)

        cin >> a[i], dp[i] = 1e9;

    dp[n - 1] = 0;

    for(int i = n - 1; i > -1; i--){

        for(int j = 1; j <= k; j++){

            if(i - j > -1)

                dp[i - j] = min(dp[i - j], abs(a[i] - a[i - j]) + dp[i]);

        }

    }

    cout << dp[0] << endl;

    return 0;

}
// Slower C++ solution:
//14:17

#include <iostream>

#include <string>

#include <map>

#include <vector>

#include <algorithm>

#include <queue>

using namespace std;



#define REP(i,k,n) for(int i=k;i<n;i++)

#define rep(i,n) for(int i=0;i<n;i++)



#define all(v) v.begin(),v.end()

#define INF 1<<30

#define mp make_pair

#define pb push_back



#define fi first

#define se second



#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)



typedef vector<int> vi;

typedef vector<vi> vvi;

typedef pair<int,int> pii;

typedef vector<pair<int, int> > vii;



typedef long long ll;

int per[10100][1000];

int ln[10100];//???????????¨????????°?????°

int main(){

	

	//stu[m] [0]?????? t[1]PC n[2]?????°?????? s

	while(1){

		vii stu[10100];

		int n,m;

		int r;

		cin >> n >> m;

		if(n==0&&m==0) break;

		cin >> r;

		rep(i,m){

			ln[i]=0;

			rep(j,1000){

				per[i][j]=0;

			}	

		}

		rep(i,r){

			int t,n,m,s;

			cin >> t >> n >> m >> s;

			m-=1;

			stu[m].pb(make_pair(t,s));

			ln[m]++;

 		}

 		rep(i,m){

 			int ms = 0;

 			int p = 0;

 			rep(j,ln[i]){

 				if(stu[i][j].se==1){



 					if(ms==0){

 						per[i][p++] = stu[i][j].fi;

 					}

 					ms++;

 				}else if(stu[i][j].se==0){

 					if(ms==1){

 						per[i][p++] = stu[i][j].fi;

 					}

 					ms--;

 				}

 			}

 			ln[i]=p;



 		}

 	// 	cout << "debug" << endl;

 	// 	rep(i,m){

		// 	rep(j,ln[i]){

		// 		cout << "j:"<<j<<""<< per[i][j] << " ";

		// 	}

		// 	cout << endl;

		// }

		// cout << endl;





		int q;

		cin >> q;

		rep(i,q){

			int ans =0;

			int ts,te,m;

			cin >> ts >> te >> m;

			m--;

			for(int j=0;j<ln[m]/2;j++){

				ans += max(0,min(te,per[m][j*2+1])-max(ts,per[m][j*2]));

			}

			// cout << "ans" << endl;

			cout << ans << endl;

		}



		



	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4119926489246466x):
#include<bits/stdc++.h>



#define rep(i,N) for(int i=0;i<(int)(N);i++)

#define REP(i,k,N) for(int i=k;i<(int)(N);i++)



using namespace std;

int main(){

    int N,M;

    while(cin >> N >> M,N) {

        int t[1000], n[1000], m[1000], s[1000];

        int r, q, begin, end, id;

        cin >> r;

        rep(i, r) cin >> t[i] >> n[i] >> m[i] >> s[i];

        cin >> q;

        rep(i, q) {

            cin >> begin >> end >> id;

            //ここまで入力



            int ans = 0,  pc= 0, B;

            rep(j, r) {

                if (m[j] != id)continue;

                if (s[j] == 1) {

                    //ログイン

                    pc++;

                    if (pc == 1) B = t[j];

                }else{

                    //ログアウト

                    pc--;

                    if (pc == 0 && begin <= t[j] && B <= end) {

                        ans += (min(end, t[j]) - max(begin, B));

                    }

                }

            }

            cout << ans << endl;

        }

    }

    return 0;

}
// Slower C++ solution:
//Robs Code

/***********HEADER***************/

#include <iostream>

#include <bitset>

#include <deque>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <algorithm>

#include <list>

#include <vector>

#include <unordered_set>

#include <unordered_map>

#include <stack>

#include <set>

#include <queue>

#include <map>

/***********MACROS***************/

#define int long long int

#define fri(l,k) for (i=l;i<=k;i++)

#define frj(l,k) for (j=l;j>=k;j--)

#define fij(a,b,l,k) for(i=a;i<=b;i++) for(j=l;j<=k;j++)

#define all(x) x.begin(),x.end()

#define allr(x) x.rbegin(),x.rend()

#define endl "\n"

#define pb push_back

#define _1 first

#define _2 second

#define mxsz 1e5+10

#define mod 1e9 + 7

#define pii pair<int,int>

using namespace std;

const int N = 1e5 + 1;

int n,m,i,j,l,k,x,y,a,b,cnt[3],sum[3];

/***********MAIN**************/

vector<int> dp,v;

int brute(int indx){

	if(indx >= (n)){

		return 1e9;

	}

	if(indx == (n-1))

		return 0;

	if(dp[indx] != -1){

		return dp[indx];

	}

	int ans = 1e9;

	for(int i=1;i<=k;i++)

		ans = min(ans,brute(indx+i) + abs(v[indx] - v[indx+i]));

	return dp[indx] = ans;

}

signed main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

	cin>>n>>k;

	v.assign(n,0);

	for(auto &x : v){

		cin>>x;

	}

	dp.assign((int)1e5+10,-1);

	cout<<brute(0);

    return 0;

}

//.........

// Faster C++ solution for the same problem (measured speedup: 1.8033271477212764x):
//Robs Code

/***********HEADER***************/

#include <iostream>

#include <bitset>

#include <deque>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <algorithm>

#include <list>

#include <vector>

#include <unordered_set>

#include <unordered_map>

#include <stack>

#include <set>

#include <queue>

#include <map>

/***********MACROS***************/

#define int long long int

#define fri(l,k) for (i=l;i<=k;i++)

#define frj(l,k) for (j=l;j>=k;j--)

#define fij(a,b,l,k) for(i=a;i<=b;i++) for(j=l;j<=k;j++)

#define all(x) x.begin(),x.end()

#define allr(x) x.rbegin(),x.rend()

#define endl "\n"

#define pb push_back

#define _1 first

#define _2 second

#define pii pair<int,int>

using namespace std;

int n,m,i,j,l,k,x,y,a,b,cnt[3],sum[3];

/***********MAIN**************/

signed main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

	cin>>n>>k;

	int dp[n+1],h[n+1];

	for(int i=1;i<=n;i++){

		cin>>h[i];

	}

	dp[1] = 0;

	dp[2] = abs(h[2] - h[1]);

	for(int i=3;i<=n;i++){

		dp[i] = 1e18;

		for(int j = 1;j <= k && (i-j) >= 1;j++){

			dp[i] = min(dp[i],dp[i-j] + abs(h[i] - h[i-j]));

		}

	}

	cout<<dp[n];

	return 0;

}

//.........
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")

#pragma GCC optimize ("-ffloat-store")  

#pragma GCC optimize ("-fno-defer-pop")

#define all(a) a.begin(),a.end()

#define ll long long int

#define ld long double

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

#define ps push_back

#define fs first

#define sc second

#define N 300005

#define endl "\n"

#define mod 1000000007

ll n,k,ar[N],dp[N];

ll minval(ll i){

	if(i==n){

		return 0;

	}

	if(dp[i]!=-1){

		return dp[i];

	}



	ll j,mn=1e18;

	for(j=1;j<=k&&i+j<=n;j++){

		mn=min(mn,minval(i+j)+abs(ar[i+j]-ar[i]));

	}

	return dp[i]=mn;

}

int main(){



	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	

	ll i,j,l;

	cin>>n>>k;

	memset(dp,-1,sizeof(dp));

	for(i=1;i<=n;i++){

		cin>>ar[i];

	}

	

	l=minval(1);

	cout<<l;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.8834152796520114x):
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")

#pragma GCC optimize ("-ffloat-store") // to restrict undesirable precision

#pragma GCC optimize ("-fno-defer-pop")// to pop argument of function as soon as it returns

#define all(a) a.begin(),a.end()

#define ll long long int

#define ld long double

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

#define ps push_back

#define fs first

#define sc second

#define takeline cin.ignore();

#define iactive cout.flush();

#define N 3000005

#define endl "\n"

#define mod 1000000007

//((1.0l)*BIG MULTIPLY MAGIC?)

// string to integer stoi()

// string to long long stoll()

// string.substr(position,length);

// integer to string to_string();

//-----------------------------------------------

ll n,k,ar[100005],dp[100005];

ll findmin(ll i){

	if(i==n){

		return 0;

	}

	if(dp[i]!=-1){

		return dp[i];

	}

	ll an=1e18,j;

	for(j=1;j<=k;j++){

		if(i+j<=n){

			an=min(an,findmin(i+j)+abs(ar[i]-ar[i+j]));

		}

	}

	return dp[i]=an;

}

int main(){



	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);



	ll i,j,l;

	cin>>n>>k;

	for(i=1;i<=n;i++){

		cin>>ar[i];

	}

	memset(dp,-1,sizeof(dp));

	ll an=findmin(1);

	cout<<an;

	return 0;

}
// Slower C++ solution:
# include <iostream>

# include <cstdio>



using namespace std;



const int inf = 1e9 + 7;

const int N = 1e6 + 5;



int n, k;

long long ans;



int main(){

      scanf("%d %d", &n, &k);



      for(int b = k + 1; b <= n; b ++){

            if(k == 0)

                  ans += max(0, b - k - 1);

            else

                  ans += b - k;

            for(int a = b; a <= n; a += b){

                  int l = a + k, r = a + b - 1;

                  r = min(n, r);

                  ans += max(r - l + 1, 0);

            }

      }



      printf("%lld\n", ans);

}


// Faster C++ solution for the same problem (measured speedup: 4.892288913215986x):
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>



int main () {

    long long int n, k, i, j, ans, c1 ;

    scanf ( "%lld %lld", &n, &k ) ;

    ans = 0 ;

    for ( i = k ; i <= n ; i++ ) {

        if ( i == 0 ) {

            ans = n * n ;

            break ;

        }

        c1 = n / i ;

        ans += ( i - k ) * c1 ;

        j = n % ( c1 * i ) ;

        if ( j - k + 1 > 0 ) {

            ans += j - k + 1 ;

        }

    }

    printf ("%lld\n", ans ) ;

    return 0 ;

}
// Slower C++ solution:
#include<iostream> 

#include<string> 



using namespace std; 



int main() 

{

	int k; 

	string s; 

	cin >> k >> s; 



	if(k < s.size()) 

	{

		for(int i = 0; i < k; i++)

			cout << s[i]; 

		cout << "...\n"; 

	}

	else 

	{

		cout << s << "\n"; 

	}



	return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 5.287203547474737x):
#include<stdio.h>



int main(){

	int k, len = 0;

	char str[100];

	scanf("%d %s", &k, str);

	for(int i = 0; str[i] != '\0'; i++){

		len++;

	}

	if(len <= k){

		printf("%s\n", str);

	}

	else{

		for(int i = k; i <= k + 3; i++){

			if(i < k + 3){

				str[i] = '.';

			}

			else{

				str[i] = '\0';

			}

		}

		printf("%s\n", str);

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

typedef long long ll;

using namespace std;

const int N=100010;

int main(){

	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);

	long long i,j,n,a[N],dp[N],k;

	cin>>n>>k;

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

	dp[0]=0;

	for(i=1;i<k;++i){

		dp[i]=abs(a[i]-a[0]);

		for(j=1;j<i;++j) dp[i]=min(dp[j]+abs(a[i]-a[j]),dp[i]);

	}

	for(i=k;i<n;++i){

		dp[i]=dp[i-k]+abs(a[i]-a[i-k]);

		for(j=i-k+1;j<i;++j) dp[i]=min(dp[j]+abs(a[i]-a[j]),dp[i]);

	}

	cout<<dp[n-1];

}


// Faster C++ solution for the same problem (measured speedup: 4.530446562789204x):
#include<cstdio>

#include<algorithm>



int main(){

  int s,k;

  long long st[100001],p[100001];

  scanf("%d%d",&s,&k);

  for(int i=1;i<=s;i++){

    scanf("%lld",&st[i]);

  }

  p[0]=0;

  p[1]=0;

  p[2]=(st[2]-st[1] < 0) ? st[1]-st[2] : st[2]-st[1];

  for(int i=3;i<=s;i++){

    p[i] = 1000000001;

  }

  for(int i=3;i<=s;i++){

    for(int j=1;j<=k;j++){

      if(i-j>=1){

	long long tmp;

	tmp = (st[i]-st[i-j] < 0) ? st[i-j]-st[i] : st[i]-st[i-j];

	if(p[i] > p[i-j]+tmp){

	  p[i] = p[i-j]+tmp;

	}

	if(tmp == 0) break;

      }else{

	break;

      }

    }

  }

  printf("%lld\n",p[s]);

  return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);

#define pii pair<int,int>

#define ll long long int

#define ull unsigned long long int

#define inf LONG_MAX;

using namespace std;

ll dp[100005], h[100005],n,k;

int main()

{

    fast_io

    for(int i=2;i<=100000;i++)

        dp[i]=inf;

    cin>>n>>k;

    for(int i=1;i<=n;i++)

        cin>>h[i];

    for(int i=1;i<n;i++)

    {

        for(int j=1;j<=k;j++)

            if(i+j<=n)

                dp[i+j]=min(dp[i+j],dp[i]+abs(h[i]-h[i+j]));

    }

    cout<<dp[n]<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.8156047745581296x):
 #include<bits/stdc++.h>

using namespace std;

#define ll long long

#define mod 10000007



int main()

{

    ll n,k;

    cin>>n;

    cin>>k;

    ll cost[n+1];

    ll dp[n+1];

    for(int i=1;i<=n;i++)

    {

        cin>>cost[i];



    }

    dp[n]=0;

     for(int i=n-1;i>0;i--)

    {

        dp[i]=INT_MAX ;

        for(int j=1;j<=k;j++)

        {    if(i+j<=n)

            {dp[i]=min(dp[i],(dp[i+j]+abs(cost[i+j]-cost[i])));

            }



        }

    }

    //

    cout<<dp[1]<<endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

//freopen("transmitters.in","r",stdin);

//cout<<fixed<<setprecision(6)<<n<<endl;

// transform(s.begin(), s.end(), s.begin(), ::tolower);

//double PI=3.14159265359,c=0;

#define all(v) v.begin(),v.end()

#define endl '\n'

#define ll long long

//#define f(n) for(int i=;i<n;i++)

double PI = acos(-1);

using namespace std;

void fast()

{

    std::ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

}

ll arr[100000];

int main()

{

    fast();

    ll n,t,x,mn=0,k;

    cin>>n>>k;

    vector<ll>v(n,1e12);

    for(int i=0; i<n; i++)

        cin>>arr[i];



    for(int i=0; i<n; i++)

    {

        x=v[i];

        for(int j=0; j<k; j++)

        {

           if(i+j+1<n)

           {

                if(i==0)

                    x=0;

                mn=x+abs(arr[i]-arr[i+j+1]);

                v[i+1+j]=min(v[j+i+1],mn);



           }



        }

        //cout<<v[i]<<" ";

    }

    cout<<v[v.size()-1]<<endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.295590886680668x):
#include <cstdio>

#include <algorithm>

#include <vector>

using namespace std;

 

int n,dp[100005],a[100005],k; 

int main(){

	scanf("%d%d",&n,&k);

	for (int i=1;i<=n;i++){

		scanf("%d",&a[i]);

	}

	for (int i=2;i<=n;i++){

		dp[i]=dp[i-1]+abs(a[i]-a[i-1]);

		for (int j=max(1,i-k);j<i;j++){

			dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j])); 

		}

	}

	printf("%d\n",dp[n]);

	return 0;

}