stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'

const int N = 1e6;

const int inf = 1e9;



int main(){

	ios_base::sync_with_stdio(0);

	int a,b,n,k,T,cas=0;

	cin>>n>>k;

	vector<int> A(n);

	for(int &x: A) {

		cin>>x;

	}

	vector<int> dp(n, inf);

	dp[0]=0;

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

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

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

		}

	}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>



using namespace std;



const int maxn = 1e5+5;

int n,K;

int h[maxn],f[maxn];



int abs(int x,int y) {

	if (x - y < 0) return y - x;

	return x - y;

}



int min(int x,int y) {

	if (x < y) return x;

	return y;

}



int main() {

	memset(f,0x3f,sizeof(f));

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

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

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

	}

	f[0] = f[1] = 0;

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

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

			f[i] = min(f[i-k]+abs(h[i],h[i-k]),f[i]);

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define H ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define ll long long

#define ld long double

using namespace std;

const ll  mod = 1000000007;





int solve(int n, int m, int counter){

	//base case

	if (n == m) return counter;

	if (m < n) return counter + (n - m);



	//sol steps

	if (m % 2 == 0)

		return solve(n, m / 2, counter + 1);

	return solve(n, m + 1, counter + 1);

}



int main()

{

	H

	int n, k;

	cin >> n >> k;

	int arr[n + 1];

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

		cin >> arr[i];

	}

	int dp[n + 1];

	memset(dp, 0x3f3f3f3f, sizeof dp);

	dp[0] = dp[1] = 0;

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

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

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

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

		}

	}

	cout << dp[n];

	return 0 ;

}


// Optimized solution to the same problem (faster):
#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;

}








// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstdlib>

#include <cstdio>

#include <cstring>

#include <iomanip>

#include <string>

#include <set>

#include <vector>

#include <queue>

#include <stack>

using namespace std;

#define ms(a, x) memset(a, x, sizeof(a))

#define fore(i, a, n) for (long long i = a; i < n; i++)

#define ford(i, a, n) for (long long i = n - 1; i >= a; i--)

#define si(a) scanf("%d", &a)

#define sii(a, b) scanf("%d%d", &a, &b)

#define debug(a) cout << a << endl

#define endl '\n'

#define pi acos(-1.0)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define IO ios::sync_with_stdio(false), cin.tie(0)

const double eps = 1e-8;

inline int sgn(const double &x) { return x < -eps ? -1 : x > eps; }

typedef long long ll;

const int inf = 0x3f3f3f3f;

const int maxn=100500;



//=================================================

int a[maxn];

int dp[maxn];

int n,k;

void init() {

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

        if(i==1) {

            dp[i]=0;

        }

        else  {

            dp[i]=inf;

        }

    }

}

int main()

{

    IO;

    cin>>n>>k;

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

        cin>>a[i];

    }

    init();

    if(k>=n-1) {

        cout<<abs(a[n]-a[1])<<endl;

    }

    else {

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

            int minn=min(i-1,k);

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

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

        }

        cout<<dp[n]<<endl;

    }

    



    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>



using namespace std;



const int maxn = 1e5+5;

int n,K;

int h[maxn],f[maxn];



int abs(int x,int y) {

	if (x - y < 0) return y - x;

	return x - y;

}



int min(int x,int y) {

	if (x < y) return x;

	return y;

}



int main() {

	memset(f,0x3f,sizeof(f));

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

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

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

	}

	f[0] = f[1] = 0;

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

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

			f[i] = min(f[i-k]+abs(h[i],h[i-k]),f[i]);

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
//Inbornhandsome

#include <bits/stdc++.h>

using namespace std;

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

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

#define endl '\n'

#define int long long



const int N = 1e5 + 7;



int stones[N], cost[N], n, k;



int find_cost(int stone)

{

    if(stone > n)   return 1e9;

    if(stone == n)  return 0;



    int &ans = cost[stone];



    if(ans != -1) return ans;



    ans = 1e10;



    for(int i = 1; i <= k and stone + i <= n; ++ i)

    {

        ans = min(ans, abs(stones[stone] - stones[stone + i]) + find_cost(stone + i));

    }

    

    

    return ans;

}



int32_t main()

{

    IOS; //FIO;



    cin >> n >> k;



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



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

        cin  >> stones[i];



    cout << find_cost(1);



    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define endl '\n'

#define int long long



const int N = 1e5+9;

int cache[N], h[N], n, k;



int32_t main()

{

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



	cin >> n >> k;

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

		cin >> h[i];

	}

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

		cache[i] = 1e9;

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

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

		}

	}

	cout << cache[1] << endl;



	return 0;

}

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include<iostream>

using namespace std;



int A[100005];



int partition(int p, int r) {

    int x = A[r];

    int i = p - 1;

    for (int j=p; j<r; j++) {

        if (A[j] <= x) {

            i++;

            swap(A[i], A[j]);

        }

    }

    swap(A[i+1], A[r]);

    return i + 1;

}



int main() {

    int n;

    cin >> n;

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

    int idx = partition(0, n-1);

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

        if (i == idx) cout << "[";

        cout << A[i];

        if (i == idx) cout << "]";

        cout << (i != n-1 ? " " : "\n");

    }

}


// Optimized solution to the same problem (faster):
/*

#include<iostream>

using namespace std;



int A[100005];



int partition(int p, int r) {

    int x = A[r];

    int i = p - 1;

    for (int j=p; j<r; j++) {

        if (A[j] <= x) {

            i++;

            swap(A[i], A[j]);

        }

    }

    swap(A[i+1], A[r]);

    return i + 1;

}



int main() {

    int n;

    cin >> n;

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

    int idx = partition(0, n-1);

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

        if (i == idx) cout << "[";

        cout << A[i];

        if (i == idx) cout << "]";

        cout << (i != n-1 ? " " : "\n");

    }

}

*/



#include<stdio.h>

#define MAX 100000



int A[MAX], n;



int partition(int p, int r) {

    int x, i, j, t;

    x = A[r];

    i = p - 1;

    for (j = p; j < r; j++) {

        if (A[j] <= x) {

            i++;

            t = A[i]; A[i] = A[j]; A[j] = t;

        }

    }

    t = A[i + 1]; A[i + 1] = A[r]; A[r] = t;

    return i + 1;

}



int main() {

    int i, q;



    scanf("%d", &n);

    for (i = 0; i < n; i++) scanf("%d", &A[i]);



    q = partition(0, n - 1);



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

        if (i) printf(" ");

        if (i == q) printf("[");

        printf("%d", A[i]);

        if (i == q) printf("]");

    }

    printf("\n");



    return 0;

}


// Fastest known solution for this problem:
#include<stdio.h>

#include<iostream>



#define MAX 100000



int partition(int* A, int p, int r)

{

    int i, j, x;

    x = A[r];

    i = p - 1;

    for(j = p; j < r; j++){

		if(A[j] <= x){ i++; std::swap(A[i], A[j]); }

    }

	std::swap(A[i + 1], A[r]);



    return i + 1;

}



int main()

{

    int i, n, A[MAX], q;

    scanf("%d", &n);

    for(i = 0; i < n; i++) scanf("%d", &A[i]);



    q = partition(A, 0, n - 1);



    for(i = 0; i < q; i++){ printf("%d ", A[i]); }



    printf("[%d]", A[q]);



    for(i = q + 1; i < n; i++){ printf(" %d", A[i]); }



    printf("\n");



    return 0;

}
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

 

int h[100000], mCost[100000];

int Nstones, k;



int main() {



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

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

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

	}

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

        	mCost[i] = 10000000000;

        	for (int j = i - 1; j >= max(i-k, 0); j--){

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

        	}

	}

	printf("%d", mCost[Nstones - 1]);

}

// Optimized solution to the same problem (faster):
#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;

} 

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define ll long long

#define endl "\n"

using namespace std;

inline int D()

{

    int t;

    scanf("%d",&t);

    return t;

}

inline ll llD()

{

    ll t;

    scanf("%lld",&t);

    return t;

}



const int Max=1e6+9;

const int OO=-1e6;

int st[4]= {1,0,0,-1};

int nt[4]= {0,1,-1,0};

int n,m;

bool valid(int x,int y)

{

    return x>-1 && y>-1 && x<n && y<m;

}

ll mem[Max];

void init()

{

    memset(mem,-1,sizeof mem);

}

//////////////////////////////////////////////////////////



ll a[Max];



ll solve (int i )

{

    if(i==n-1)return 0;

    if(i>=n)

        return Max;



    ll &ret=mem[i];

    if(~ret)return ret;

    ret=1e9;

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

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



     return mem[i]=ret;

}



void Fast()

{

    std::ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    cerr.tie(0);

}

int main()

{

   init();

   n =D();

m =D();

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

   {

       a[i]=llD();

   }

   printf("%lld",solve(0));



}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#include <set>

using namespace std;

#define mp             make_pair

#define pb             push_back

#define fi             first

#define se             second

#define sz(x)          (int)((x).size())

#define fill(x, y)     memset(x, y, sizeof(y))

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

typedef long long      ll;

typedef pair<int, int> ii;

typedef pair<ii, int>  iii;

typedef vector<int>    vi;

typedef vector<ii>     vii;

typedef vector<iii>    viii;



const int INF = 1e9 + 5;



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  int N, K;

  cin >> N >> K;

  vi h(N+1);

  h[0] = INF;

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

    cin >> h[i];

  }

  vi dp(N+1, INF);

  dp[0] = 0;

  dp[1] = 0;

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

    for (int j = 1; j <= min(i-1,K); j++) {

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

    }

  }

  cout << dp[N] << "\n";

  return 0;

}

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    long long n, k;

    cin >> n >> k;

    long long a[n + 1], dp[n + 1];

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

        cin >> a[i];

        dp[i] = INT_MAX;

    }

    if(n == 1) {

        cout << 0 << endl;

    }

    else {

        dp[1] = 0;

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

        for(long long i = 3; i <= min(n, k); i++) {

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

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

            }

        }

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

            for(long long j = i - k; j < i; j++) {

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

            }

        }

        cout << dp[n] << endl;

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;



#define ll long long 



#define file_io 0



ll i,t,j;

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



int main(){

	if (file_io){

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

		freopen(".out","w",stdout);

	}

	

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

	

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

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

	}

	

	dp[1]=0;

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

		dp[i]=2e9;

		for (t=max(1ll,i-k); t<i; ++t){

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

		}

	}

	

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

	

	return 0;

}

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

int main()

{

    ll i,j,n,k,m;

    cin>>n>>m;

    for(i=m-n+1;i<=m;i++)

    {

        cout<<i<<" ";

    }

    for(i=m+1;i<=m+n-1;i++)

    {

        cout<<i<<" ";

    }



    cout<<endl;

}


// Optimized solution to the same problem (faster):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

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



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

        printf("%d ",i);

    }

    return 0;

}

// Fastest known solution for this problem:
#include <bits/stdc++.h>

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

using namespace std;



int main(void) {

	int k, x;

	cin >> k >> x;

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

		if (i == x+k) cout << i << endl;

		else cout << i << " ";

	}

	return 0;

}
// Original C++ solution (slower):
#include <bits/stdc++.h>



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

#define for1(i, n) for (int i = 1; i <= int(n); ++i)

#define fore(i, l, r) for (int i = int(l); i <= int(r); ++i)

#define ford(i, n) for (int i = int(n)-1; i >= 0; --i)

#define pb push_back

#define eb emplace_back

#define ms(a, x) memset(a, x, sizeof(a))

#define F first

#define S second

#define endl '\n'

#define de(x) cout<<#x<<" = "<<(x)<<endl

#define de2(x,y) cout<<#x<<" = "<<(x) <<' '<< #y<<" = "<<y<<endl;



using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

	int n,k;

	cin>>n>>k;

	vector<int> a(n),dp(n,INF);

	for(auto& it:a) cin>>it;

	dp[0]=0;

	forn(i,n){

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

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

		}

	}

	cout<<dp[n-1];

    return 0;

}




// Optimized solution to the same problem (faster):
#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;

} 

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}