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

using namespace std;

int main()

{

    int n,i,k,j;

    cin>>n>>k;

    vector<int>v(200000,0);

    vector<int>pre(200000,0);

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

    {

        cin>>v[i];

    }

    int dp[200000];

    for(i=1;i<200000;i++)

    dp[i]=INT_MAX;

    dp[1]=0;

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

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

    {

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

        {

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

        }

    }

    cout<<dp[n];

}

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

// Created by rajshah9914 on 29/03/20.

//



#include <bits/stdc++.h>

using namespace std;

const int INF=1e9+5;

int main() {

    long n,k,h[100000];

    cin>>n>>k;

    vector<long>dp(n,INF);

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

        cin>>h[i];

    dp[0]=0;

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

    {

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

        {

            if(j<n)

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

        }

    }

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

}


// 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 <queue>

#include <string>

#include <map>

#include <stdio.h>

#include <iomanip>

#include <math.h>

#include <vector>

#include <stack>

#include <algorithm>

#include <string.h>

#include <set>

#include <cstdlib>

#include <iostream>

#include <bits/stdc++.h>

#define pii pair<int,int> 

#define ll long long int 

#define sl(n) scanf("%lld",&n)

#define s(n) scanf("%d",&n)

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

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

#define fnm(n,m) for(ll i=n;i<m+1;++i)

#define pb push_back

#define pf printf

#define F first

#define S second

#define srt(v) sort(v.begin(),v.end());

#define srtc(v,cmp) sort(v.begin(),v.end(),cmp);

#define srta(v,n) sort(v,v+n);

const ll LL_INF=0x3f3f3f3f3f3f;

const ll MOD=1073741824;





using namespace std;





int T;

int n,k;

int arr[100005];

ll dp[100005];



ll gen(int i){

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

    ll &ret = dp[i];

    if(~ret)return ret;

    ret =LL_INF;

    fo(j,k){

        if(i+j<n)ret=min(gen(i+j)+abs(arr[i]-arr[i+j]),ret);



    }

    // ret=gen(i+1)+abs(arr[i]-arr[i+1]);

    return ret;

}





int main(){

    // pf("\t\t\t***************************************************************************\n\n");

    // pf("\t\t\t***************************************************************************\n\n");

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

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

    memset(dp,-1,sizeof dp);

    s(n);

    s(k);

    f(i,n)s(arr[i]);



    pf("%lld\n",gen(0));



    return 0;

} 

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

#define x first

#define y second

#define pii pair<int,int>

#define ll long long

#define pll pair<ll,ll>

#define mp make_pair

#define pb push_back

#define pf push_front

#define popb pop_back

#define popf pop_front

#define xmod 100000007

using namespace std;



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

int main (){

	ios::sync_with_stdio(false);

	cin >> n >> m;

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

		cin >> a[i];

	}

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

		dp[i]=1e9;

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

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

		}

	}

	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;

const int N=100005;

int a[N],dp[N];

int main()

{

	int n,k;

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

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

		dp[i]=INT_MAX;

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

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

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

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

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

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

	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;

//hari hari

int main(){

	int n,k;

	cin>>n>>k;

	vector<int>arr(n+1);

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

		cin>>arr[i];

	}

	vector<int>dp(n+1,INT_MAX);

	dp[1]=0;

	for(int i=2;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]<<endl;

	return 0;

}

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

}


// 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 INF INT_MAX

#define MINF INT_MIN

#define ll long long

#define PB push_back 

#define PF push_front

#define P_F pop_front

#define P_B pop_back

#define F front

#define B back

#define MP make_pair

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

#define forit(i, s) for(__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)

#define foritr(i, s) for(__typeof ((s).rend ()) i = (s).rbegin (); i != (s).rend (); ++i)

 

using namespace std;



int datos = 100000;

vector<int> all(datos+1);



int main() {

  int n, k, aux, peNe;

  cin >> n>> k; 

  vector<int> peso;

  vector< map<int, int> > grafo(n);

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

    cin>>aux;

    peso.PB(aux);

  }



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

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

      peNe = abs(peso[i]-peso[j]);

      grafo[i][j] = peNe;

    }

  }



  int nodo;

  deque<int> dq;

  vector<int> seen(n, INF);

  dq.PB(0);

  seen[0] = 0;



  while(!dq.empty()){

    nodo = dq.F();

    dq.P_F();

    

    if(nodo != n-1){

      forit(x, grafo[nodo]){

        if(seen[x->first] == INF){

          seen[x->first] = x->second + seen[nodo];

          dq.PB(x->first);

        }else

          seen[x->first]= min(seen[x->first], seen[nodo] + x->second);

      }

    }

  }

  

  cout << seen[n-1] << "\n";



  return 0;

}

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

using namespace std;

#define MAXN 100000

#define INF 2000000000



int h[MAXN];

int dp[MAXN];

int n,k;



int main() {

	int i,j;

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

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

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

	}

	dp[n-1]=0;

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

	for (i=n-3; i>=0; i--) {

		dp[i]=INF;

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

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

		}

	}

	

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

	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 vl vector<ll int>

#define vs vector<string>

#define v vector<int>

#define vd vector<double>

#define mi map<int,int>

#define mc map<char,int>

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



using namespace std;



int main(){

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

	int k,x;

	cin>>k >>x;

	v A;

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

		A.push_back(x-i);

		}

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

			A.push_back(x+i);

			}

			sort(A.begin(),A.end());

			for(auto e:A)cout<<e <<" ";

			cout<<"\n";

			

}


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

#include<stdio.h>

int main(){



int k,x,i,p,q,r;

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

p=(x-(k-1));

q=(x+(k-2));

r=(x+(k-1));

if(k==1)

{

    printf("%d",x);

}

else{

    for(i=p;i<=q;i++)    

    {

        printf("%d ",i);

    }

    printf("%d",r);

}



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>

using namespace std;



template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



using ll = long long;



constexpr int MAX_N = 1e5;

constexpr int MAX_K = 1e2;

constexpr ll INF = 1LL << 60;



int h[MAX_N];

ll dp[MAX_N+MAX_K];



int main() {

  int N, K;

  cin >> N >> K;

  for (int i{}; i < N; ++i)

    cin >> h[i];



  fill_n(dp, MAX_N+MAX_K, INF);

  dp[0] = 0;

  for (int i{}; i < N-1; ++i) {

    for (int j{1}; j <= K; ++j) {

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

    }

  }



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

}


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

using namespace std;



template<typename T>

inline bool chmin(T &a, T b) {

  if (a > b) {

    a = b;

    return true;

  }

  return false;

}

template<typename T>

inline bool chmax(T &a, T b) {

  if (a < b) {

    a = b;

    return true;

  }

  return false;

}



constexpr int MAX_N = 1e5 + 9;

constexpr int MAX_K = 1e2;

constexpr int INF = 1e9 + 9;



int N, K;

int h[MAX_N];

int dp[MAX_N + MAX_K];



int main() {

  fill_n(dp, MAX_N + MAX_K, INF);

  cin >> N >> K;

  for (int i{}; i < N; ++i) cin >> h[i];



  dp[0] = 0;

  for (int i{0}; i < N; ++i) {

    for (int j{1}; j <= K; ++j) {

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

    }

  }



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

}


// 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;



const int N=1e5+30;

typedef long long ll;

int n,k,arr[N];

int dp[N];

ll solve(int i){

    if(i>=n)return 1e15;

    if(i==n-1){

        return 0;

    }



    if(dp[i]!=-1)return dp[i];

    ll mn=1e18;

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

        ll path1=abs(arr[i]-arr[i+j])+solve(i+j);

        mn=min(path1,mn);

    }

    return dp[i]= mn;

}

int main()

{

    memset(dp,-1,sizeof dp);

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

    for(int i=0;i<n;i++)scanf("%d",arr+i);

    cout<<solve(0)<<endl;

}




// Optimized solution to the same problem (faster):
#define _CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<functional>

#include <bitset>

#include<algorithm>

#include<fstream>

#include<string>

#include <cstring>

#include<stdio.h>

#include<iomanip>

#include<sstream>

#include<cmath>

#include<vector>

#include<assert.h>

#include<queue>

#include<deque>

#include<stack>

#include<set>

#include<map>

// never lose hope ..

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

#define eps 1e-8

using namespace std;

double const  PI = 3.141592653;

const int OO = 1000000000 + 9;



void fast() {

	ios_base::sync_with_stdio(0);

	cin.tie(NULL), cout.tie(NULL);

}





int main()

{

	fast();



	int n, k;

	cin >> n >> k;

	vector<int>v(n + 1, 0), dp(n + 1, OO);

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

		cin >> v[i];

	}

	dp[1] = 0;

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

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

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

		}

	}

	cout << 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;



int a[100001];



int minCost(int arr[], int& n, int k, int i, int sum){

    if(i==n-1){

        return sum;

    }

    int x = INT_MAX, temp = sum;

    sum = 0;

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

        sum += abs(arr[i+j]-arr[i]);

        if(a[i+j]!=-1) x = min(x, sum + a[i+j]);

        else x = min(x, minCost(arr, n, k, i+j, sum));

        sum -= abs(arr[i+j]-arr[i]);

    }

    // if(i+k>n-1) x = min(x, minCost(arr, n, k, n, sum));

    a[i] = x;

    return temp + a[i];

}



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    

    int n, k;

    cin >> n >> k;

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

    int m = 0;

    int arr[n];

    

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

        cin >> arr[i] ;

    }

    

    m = minCost(arr, n, k, 0, m);

    

    cout << m;

    

    return 0;

}

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

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

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

#define N_MAX 100005

using namespace std;

typedef long long ll;

typedef pair<int,int> pi;

typedef vector<int> vi;

int n,k;

vi v;

vi dp;

void leer(){

    si(n); 

    si(k);//B

    //k=2; //A

    v.assign(n+1,0);

    dp.assign(n+1,INT_MAX);

    fore(i,1,n+1)

        si(v[i]);

    int x;

    x = v[1];

    if(k>n)

        k = n;

    fore(i,1,k+1)

        dp[i] = abs(x-v[i]);

    fore(i,k+1,n+1){

        fore(j,1,k+1){

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

        }

    }

}

void solve(){

	leer();

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

}

int main(){

	solve();

	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>



#define ok printf("ok\n");









long min(long a, long b){

    if(a>b)

        return b;

    else

        return a;

}



long abs(long a){

    if(a>=0)

        return a;

    else

        return 0-a;

}

long h[110000];

long dp[110000];



int main(){

    long a , b;

    scanf("%ld%ld", &a, &b);



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

        scanf("%ld", h+i);

    }



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

        dp[i]=2147483647;

    }

    dp[0]=0;



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

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

            if(i+j<a)

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

        }

    }



    printf("%ld\n", dp[a-1]);



    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;





}