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

#include <unordered_map>

#include <vector>

#include <stack>

#include <algorithm>

#include <list>

#include <set>

#include <climits>

#include <cmath>

#define lli long long int

using namespace std ;



int eval(vector<int> &height, vector<int> &dp, int i, int k)

{

	if(i == 0) return 0 ;

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



	dp[i] = INT_MAX ;



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

	{

		if(i-j < 0) break ;

		dp[i] = min(dp[i], eval(height,dp,i-j,k) + abs(height[i] - height[i-j])) ;

	}



	return dp[i] ;

}



int main(int argc, char const *argv[])

{

	int n,k ;

	cin>>n>>k ;



	vector<int> height(n) ;

	vector<int> dp(n,-1) ;

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



	cout<<eval(height,dp,n-1,k)<<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>

using namespace std;

typedef long long int ll;

vector<int> memo(1000000, INT_MAX);

void solve(){

	int n, k; cin>>n>>k;

	vector<int> a(n);

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

	{

		cin>>a[i];

	}



	memo[0] = 0;

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

	{

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

			if(j<n){

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

			}

		}

	}

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

}

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    // ll t; cin>>t;

    // while(t--)

    solve();

    return 0;

}


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

using namespace std;

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

#define  ll  long long

int main()

{

    ft

    ll t,i,j,n,m,l,r;

    cin>>n>>m;

    ll a[n+2],d[n+2];

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

    {

        cin>>a[i];

    }

    d[1]=0;

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

    {

        d[i]=LLONG_MAX;

        for(j=1;j<=m;j++)

        {

            if((i-j)>0)

            {

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

            }

        }

    }

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

using ll = long long;

using ii = pair<int, int>;

using iii = pair<ii, int>;

#define vt vector

#define pq priority_queue

#define pb push_back

#define pf push_front

#define mp make_pair

#define fi first

#define se second

#define __lcm(a, b) a * b / __gcd(a, b)

const int MOD = 1e9 + 7;

const int INF = 2e9;

const int N = 1e5 + 5;



int n, k;

int a[N], dp[N];



int f(int idx) {

    if(idx == n) return 0;

    int &sol = dp[idx];

    if(sol != -1) return sol;

    sol = INF;

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

        if(idx + i <= n)

            sol = min(sol, f(idx + i) + abs(a[idx] - a[idx + i]));

    }

    return sol;

}



void solve() {

    cin >> n >> k;

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

        cin >> a[i];

    }

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

    cout << f(1) << "\n";

}



int main() {

 

    ios_base::sync_with_stdio(0);

    cin.tie(0); cout.tie(0);

    

    solve();

    

}

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

using namespace std;

#define LL  long long

#define LIM 100000

#define mx  1000000007



int dp[LIM+5], vis[LIM+5];

int n, k, ara[LIM+5];



int f(int pos){

    if(pos == n) return 0;

    if(pos > n) mx;

    if(vis[pos]) return dp[pos];

    vis[pos] = 1;



    int mn = mx;

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

        if(pos+i > n) break;

        mn = min(mn, abs(ara[pos]-ara[pos+i]) + f(pos+i));

    }

    return dp[pos] = mn;

}



int main(){

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

    for(int i = 1; i <= n; i++) scanf("%d", ara+i);

    int res = f(1);



    printf("%d\n", res);

}


// 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):
/*D - ... (Triple Dots)*/



#include <bits/stdc++.h>

using namespace std;



int main(){



	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);



	int k;

	string cad;

	

	cin >> k >> cad;



	int tam = cad.size();



	if(tam > k){



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

			cout << cad[i];

		

		cout << "...";

	}

	else

		cout << cad;



	return 0;

}

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



int main(){

  int k, len = 0;

  scanf("%d", &k);

  char str[100];

  scanf("%s", 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;

}


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

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const double PI = acos(-1);



int main()

{

	int k;

	string s;

	cin >> k >> s;

	if (s.size() <= k) cout << s << endl;

	else cout << s.substr(0, k) + "..." << endl;

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

using namespace std;

long long dif(long long a, long long b)

{

    long long ans=a-b;

    if(ans<0) ans*=-1;

    return ans;

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    long long N, h[100005],i,ll,K,temp,mini,j;

    long long f1,f2;

    cin>>N>>K;

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

        cin>>h[i];

    long long M[100005]={0};

    M[N-2]=dif(h[N-1],h[N-2]);

    for(i=N-3;i>=0;i--)

    {

        mini=2000000000000;

        temp=K;

        for(ll=i+1;ll<N&&temp--;ll++)

        {



        f1=dif(h[ll],h[i])+M[ll];

        M[i]=min(mini,f1);

        mini=M[i];

        }

    }

    cout<<M[0]<<endl;

    return 0;

}




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

#include<cstdio>

#include<cstring>

#include<cmath>

using namespace std;

typedef long long LL;

const int maxn = 1000010;

const int inf = 0x3f3f3f3f;

LL n;

LL dp[maxn];

LL a[maxn];

LL k;

inline LL max(LL x, LL y) {

	return x > y ? x : y;

}

inline LL min(LL x, LL y) {

	return x < y ? x : y;

}

int main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	cin >> n >> k;

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

		cin >> a[i];

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

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

		dp[i] = inf;

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

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

			dp[i] = min(dp[i], dp[j] + abs(a[i] - a[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>

#define ll long long

using namespace std;



ll n;

std::vector<ll> ara(100010);

ll dp[100010];

ll k;



ll fnc(ll i){

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

  if(i >= n)return 1<<30;

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

  ll res1 = 1<<30;

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

    res1 = min(res1, abs(ara[i]-ara[i+j]) + fnc(i+j));

  }

  dp[i] = res1;

  return dp[i];

}



int main(int argc, char const *argv[]) {

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

  cin >> n >> k;

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

    cin >> ara[i];

  }

  cout << fnc(0) << endl;



  return 0;

}


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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

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

#define     bug(a,b)        cout<<a<<"\t"<<b<<"\n";

#define     Case(a,b)       cout<<"Case "<<a<<": "<<b<<"\n";

#define     watch(a)        cout<<fixed<<setprecision(a)

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

#define     Endl            "\n";

#define     tab             "\t"

#define     reset(a,b)      memset(a,b,sizeof(a));

#define     sf(a)           scanf(" %d", &a);

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

#define     pi              2*acos(0.0)

#define     inf             INT_MAX

const int sz=1e5+9;

int n,x,arr[sz],dp[sz],k;



int clac(int i){

    if(i==n) return 0;

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

    int ret1=inf;

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

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

    }

    return dp[i]=ret1;

}



void solve(){

    cin>>n>>k;

    reset(dp,-1);

    lp(i,1,n+1)cin>>arr[i];

    cout<<clac(1)<<"\n";

}



int main(){

    //FasterIO;

    #ifndef ONLINE_JUDGE

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

    #endif

    // int t;

    //cin>>t;

    //while(t--)

        solve();

}


// 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<stdio.h>

#include<string.h>

#include<time.h>

#include<math.h>

#include<stdlib.h>

#include<iostream>

#include<iomanip>

#include<utility>

#include<algorithm>

#include<stack>

#include<queue>

#include<map>

#include<set>

#define ll long long

#define fi first

#define sec second

#define pb push_back

#define mp make_pair

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

#define repp(a,b,c,d) for(int a=b;a<=c;a+=d)



const double PI=acos(-1);

const double EPS=1e-12;

const int MOD=1e9+7;

const int INF=1<<30;





using namespace std;



int dat[100005];

int minn[100005];



int main(){

	int in;

	int t,k,min_k;

	cin>>in>>k;

	rep(i,in,1){

		cin>>dat[i];

	}

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

		t=INF;

		min_k=min(k,in-i);

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

			t=min(t,abs(dat[i]-dat[i+j])+minn[i+j]);

		}

		minn[i]=t;

	}

	cout<<minn[1]<<endl;	

	return 0;

}

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

using namespace std;

#define MAX(a,b) (a>b?a:b)

#define MOD(a) (a>0?a:-(a))

#define custoate(i)

int main(){

  int npedras, maxpulo;

  scanf("%d %d",&npedras,&maxpulo);

  int hpedra[npedras],custoate[npedras];

  for (size_t i = 0; i < npedras; i++)

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

  custoate[0] = 0;

  int tempcost = 2147483647,k,tempcost2;

  for (size_t i = 1; i < npedras; i++,tempcost = 2147483647) {

    // printf("i = %d, tempcost = %d\n", i,tempcost);

    for (k= (maxpulo>i ? i : maxpulo); k > 0; k--) {

      // printf("k = %d ",k);

      tempcost2 = custoate[i-k] + MOD(hpedra[i-k]-hpedra[i]);

      if (tempcost > tempcost2)

        tempcost = tempcost2;

    }

    // printf("custo[%d] = %d",i,tempcost);

    custoate[i] = tempcost;

    // printf("\n\n");

  }

  printf("%d\n", custoate[npedras-1]);



}

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

#include <stack>

#include <vector>

#include <map>

#include <string>

#include <string.h>

#include <cstring>

#include <queue>

#include <algorithm>

#include <math.h>



using namespace std;



typedef long long ll;



int N, K;

int stonez[100001];

int DP[100001];



int calculate(int index) 

{

	if (index == N - 1)

		return DP[index] = 0;



	if (DP[index] != -1)

		return DP[index];



	int ans = abs(stonez[index] - stonez[index+1]) + calculate(index+1);

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

	{

		if(index + i < N)

			ans = min(ans, abs(stonez[index] - stonez[index + i]) + calculate(index + i));

	}

	return DP[index] = ans;

}



int main()

{

	cin >> N >> K;

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

	{

		cin >> stonez[i];

	}

	memset(DP,-1,sizeof DP);

	cout << calculate(0);

}

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

 

using namespace std;

 

#define mp make_pair

#define fi first

#define se second

#define pb push_back

typedef long long ll ;

typedef long double ld ;

typedef pair<ll, ll> pairs;

 

 

ll h[100009] ;

ll v[100009] ;

 

int main()

{

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

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

    ios_base::sync_with_stdio(0);

    cin.tie(NULL);

    cout.tie(NULL);

 

    ll n , k ; cin >> n >> k ;

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

    for ( ll i = 1 ; i < min( n , k ) ; i++ ) v[i] = abs( h[i] - h[0] ) ;

    for ( ll i = k ; i < n  ; i++ ){ v[i] = 1000000009 ;

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

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

            //cout << x << " " << y << " " << v[x] <<  "\n\n" ;

        }

    }  cout << v[n-1] ;

}

// 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):
// Author : Mohamed Sameh

#include<iostream>

#include<vector>

#include<map>

#include<set>

#include<algorithm>

#include<cmath>

#include<queue>

#include<list>

#include<stack>

#include<deque>

#include <cstring>

#include <iomanip>    

using namespace std ;

typedef long long ll ;



#define pb push_back

#define f first

#define s second

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

#define rall(v) v.rbegin(),v.rend()

#define PI  3.141592 

#define SZ(a) (int)a.size()

void READ (string INPUT = "NO" , string OUTPUT = "NO" )

{

    if (INPUT != "NO")

    freopen(INPUT.c_str() , "r" , stdin );

    if (OUTPUT!="NO")

    freopen ( OUTPUT.c_str() , "w" , stdout);

} 

int n ,k ; 

int a[100001]; 

int dp[100001]; 

int solve (int i)

{

    if (i==n-1)

        return 0 ; 

    if (i >= n)

        return 2e9 ; 

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

    int &ret = dp[i];ret = 2e9 ;  

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

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

    

    return ret ; 

}

int main ()

{

    memset (dp,-1,sizeof dp );

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

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

        scanf ("%d" , a+i); 

    printf ("%d\n" , solve(0)); 

    return 0 ; 

}

// Optimized solution to the same problem (faster):
// Author : Mohamed Sameh

#include <bits/stdc++.h>

typedef long long ll ;

#define pb push_back

#define f first

#define s second

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

#define rall(v) v.rbegin(),v.rend()

#define SZ(a) (int)a.size()

#define Flush fflush(stdout);

using namespace std ;

const int M = 1e5 ;

int n , k ;

int a[M];

int dp[M];

int main()

{

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

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

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

    dp[0] = 0 ;

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

    {

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

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

        {

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

        }

    }

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

}


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



#define INF (1ll << 62)

#define pb push_back

#define ii pair<int,int>

#define OK cerr <<"OK"<< endl

#define debug(x) cerr << #x " = " << (x) << endl

#define ff first

#define ss second

#define int long long

#define tt tuple<int, int, int>

#define endl '\n'



signed main () {

  

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);



  int n, k;



  cin >> n >> k;



  vector<int> dp(n + 1);

  vector<int> arr(n + 1);



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

  	cin >> arr[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], abs(arr[i] - arr[i - j]) + dp[i - j]);

  		} else {

  			break;

  		}

  	}

  }



  cout << dp[n] << endl;

}


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

using namespace std;

#define MAX(a,b) (a>b?a:b)

#define MOD(a) (a>0?a:-(a))

#define custoate(i)

int main(){

  int npedras, maxpulo;

  scanf("%d %d",&npedras,&maxpulo);

  int hpedra[npedras],custoate[npedras];

  for (size_t i = 0; i < npedras; i++)

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

  custoate[0] = 0;

  int tempcost = 2147483647,k,tempcost2;

  for (size_t i = 1; i < npedras; i++,tempcost = 2147483647) {

    // printf("i = %d, tempcost = %d\n", i,tempcost);

    for (k= (maxpulo>i ? i : maxpulo); k > 0; k--) {

      // printf("k = %d ",k);

      tempcost2 = custoate[i-k] + MOD(hpedra[i-k]-hpedra[i]);

      if (tempcost > tempcost2)

        tempcost = tempcost2;

    }

    // printf("custo[%d] = %d",i,tempcost);

    custoate[i] = tempcost;

    // printf("\n\n");

  }

  printf("%d\n", custoate[npedras-1]);



}

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





}