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

using namespace std;



#define y1 as214

#define ii pair < int , ll >

#define iii pair < int , ii >

#define iv pair < ii , ii >



#define fi first

#define se second

#define fr front()

#define pb push_back



#define FOR(i , x , n) for(int i = x ; i <= n ; ++i)

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



#define ll long long

#define oo INT_MAX



/// dp[i] là quãng đường dài nhất để xuất phát từ 1 và kết thúc tại i.



const int N = 1e5 + 5;

ll n , k;

ll dp[N] , f[N];



int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cin >> n >> k;

    FOR(i , 1 , n)

        cin >> f[i];

    memset(dp , 0x1f , sizeof(dp));

    dp[1] = 0;

    FOR(i , 2 , n)

    {

        FOR(j , max((ll)1 , i - k) , i - 1)

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

    }

    cout << dp[n] << endl;

}


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

using namespace std;



#define y1 as214

#define ii pair < int , int >

#define iii pair < int , ii >

#define iv pair < ii , ii >



#define fi first

#define se second

#define fr front()

#define pb push_back

#define t top()



#define FOR(i , x , n) for(int i = x ; i <= n ; ++i)

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

#define FORD(i , x , n) for(int i = x ; i >= n ; --i)



#define ll long long

#define oo 1e17

#define int long long



const int N = 1e5 + 5;

int n , k;

int dp[N] , a[N];

/// dp[i] là giá nhỏ nhất để tới được hòn đá thứ i.



main()

{

    //freopen("B.inp","r",stdin);

    ios::sync_with_stdio(0);

    cin.tie(0);

    cin >> n >> k;

    FOR(i , 1 , n)

        cin >> a[i];

    FOR(i , 2 , n)

    {

        dp[i] = 1e16;

        FOR(j , max((ll)1 , i - k) , i - 1)

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

    }

    cout << dp[n];

}


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

long long n,h[100010],dp[100010],k;



int main()

{

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

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

	{

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

	}

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

	{

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

		{

			if(i>j)

			{

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

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

			}

		}

	}

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

	return 0;

}

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

#include <algorithm>

#include <cmath>

using namespace std;



int h[(int)1e5], dp[(int)1e5], K, N;





int main()

{

	K = N = 0;

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

	{

		dp[j] = 1e9;

	}

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

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

	{

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

	}

	dp[1] = 0;

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

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

	{

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

		{

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

#include <algorithm> 

#include <vector> 



const int inf=0x3f3f3f3f;

using namespace std; 

int main()

{

  ios::sync_with_stdio(false); 

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

  int n,k; 

  cin>>n>>k;

  vector <int> a(n);  

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

  vector <int> dp(n, inf); dp[0]=0; 

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

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

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

    }

  }

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

  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 "bits/stdc++.h"

using namespace std;



#define MIN(x, y) ((x) < (y) ? (x) : (y))

#define MAX(x, y) ((x) > (y) ? (x) : (y))



#define DIVUP(x, y) (((x) + ((y) - 1)) / (y))

#define DIVOFF(x, y) (((x) + ((y) / 2)) / (y))

#define REMAINDER(x, y) ((x) % (y))



int N;

long ans{1};



int main() {

  scanf("%d", &N);

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

    ans *= i;

    ans %= 1000000007;

  }



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

  return 0;

}


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

//using namespace std;

#include <stdio.h>



#define MIN(x, y) ((x) < (y) ? (x) : (y))

#define MAX(x, y) ((x) > (y) ? (x) : (y))



#define DIVUP(x, y) (((x) + ((y) - 1)) / (y))

#define DIVOFF(x, y) (((x) + ((y) / 2)) / (y))

#define REMAINDER(x, y) ((x) % (y))



int N;

long ans{1};



int main() {

  scanf("%d", &N);

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

    ans *= i;

    ans %= 1000000007;

  }



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

  return 0;

}


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

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

using namespace std;

using ll = long long;

using P = pair<ll, ll>;

const string ln = "\n";

constexpr int INF = 1001001001;

constexpr int MOD = 1000000007;



int main() {

  cin.tie(nullptr);

  ios::sync_with_stdio(false);



  int n;

  cin >> n;



  ll ans = 1;



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

    ans *= i % MOD;

    ans %= MOD;

  }



  cout << ans << ln;



  return 0;

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

using namespace std;

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

const int N = 1e5;

int A[N+7], dp[N+7], n,k;

const int inf=1e9;



int solve(int i) {

	if(i==n) return 0;

	if(i>n) return inf;

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

	int mn = 2*inf;

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

		int a = solve(i+j) + abs(A[i]-A[i+j]);

		mn = min(a, mn);

	}

	return dp[i] = mn;

}

int main(){

	ios_base::sync_with_stdio(0);

	int a,b,T,cas=0;

	cin>>n>>k;

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

	fill(dp, dp+n+1, -1);

	int res = solve(1);

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

ll n, a[200009], dp[200009],k;

ll f(int i) {

	if (i > n)return 1e9;

	else if (i == n)return 0;

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

	ll ans= f(i + 1) + abs(a[i] - a[i + 1]);

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

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

	}

	return dp[i] =ans;

}

int main() {

	memset(dp, -1, sizeof dp);

	cin >> n>>k;

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

	cout << f(1) << endl;

}


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

using namespace std;

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

int main() {

    cin>>n>>k;

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

        cin>>a[i];

         if(i>=2){

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

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

                    if((i-j)==0)break;

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

            }

         }

    }

    cout<<dp[n]<<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>

using namespace std;



template<class T>

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

  if (a > b) {

    a = b;

    return true;

  }

  return false;

}



constexpr int MAX_N = 1e5+100+7;

constexpr int INF = 1e9+7;



int h[MAX_N];

long long dp[MAX_N];



int main() {

  int N, K;

  cin >> N >> K;

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

    cin >> h[i];

  }



  fill(dp, dp+MAX_N, INF);

  dp[0] = 0;

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

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

      if (i+j >= N)

        break;

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

    }

  }



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

  return 0;

}


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

}



static const int MAX_N = 1e5+107;

static const int INF = 1e9+7;



int N, K;

int h[MAX_N];

int dp[MAX_N];



int main() {

  cin >> N >> K;

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

    cin >> h[i];

  }



  fill(dp, dp+MAX_N, INF);

  dp[0] = 0;

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

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

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

  }



  cout << dp[N-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>

#include <vector>

#include <cmath>

#define inf 999999999

using namespace std;

int num;

int main()

{

    int a,b;

    int temp;

    vector<int> cost(1e5+1);

    vector<int> dp(1e5+1);

    while(cin>>a>>b){

        fill(cost.begin(),cost.end(),0);

        fill(dp.begin(),dp.end(),inf);

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

            cin>>cost[i];

        }

        dp[0]=0;

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

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

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

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

            }

        }

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

    }

}


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

using namespace std;

int a[100001],dp[100001];

int solve(int n,int k)

{

    dp[0]=0;

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

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

    {

        int c;

        int d=INT_MAX;

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

        {

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

            d=min(c,d);

        }

        dp[i]=d;

    }

    return dp[n-1];

}

int main()

{

    int n,k;

    cin >> n >> k;

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

    {

        cin >> a[i];

    }

    cout << solve(n,k) << 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;

int dp[100100];

int v[100100];

int n, k;

int INF = 1e9 + 100; 



int run(int i) {

	if(i > n - 1) return 0;

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

	

	int mn = INF;

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

		if (i + j < n - 1) {

			int val = run(i + j);

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

		} else if(i + j == n - 1) {

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

		}

	}

	return dp[i] = mn;

}



int main() {

	cin>>n>>k;

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

		cin>>v[i];

	}

	memset(dp, -1, sizeof dp);

	

	cout<< run(0) <<endl;

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

		cout<<dp[i]<<" ";

	} cout<<endl;*/

	

	return 0;

}

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

using namespace std;

const int N = 1e5 + 10;

const int INF = 1e9;

int n,k;

int h[N];

int dp[N];

int main() {

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

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

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

		dp[i] = INF;

	}

	dp[n - 1] = 0;

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

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

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

using namespace std;



int main(){

  int s;

  cin >> s;

  vector<int> a(1000000, 0);

  a.at(s)++;

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

    if(s%2 == 0){

      s /= 2;

      a.at(s)++;

    }else{

      s *= 3;

      s++;

      a.at(s)++;

    }

    if(a.at(s) == 2){

      cout << i << endl;

      break;

    }

  }

  return 0;

}


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

using namespace std;



int main(){

  int n;

  cin >> n;

  vector<int> x(10000);

  x.at(0) = n;

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

    if(n%2 == 0){

      n /= 2;

    }else{

      n = 3*n+1;

    }

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

      if(n == x.at(j)){

        cout << i+1 << endl;

        return 0;

      }

    }

    x.at(i) = n;

  }

  return 0;

}


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



int main()

{

    int s;

    std::cin >> s;

    int list[1000000];

    int index = 0;

    int a_i;

    int a_pre = s;

    list[0] = s;

    index = index + 1;

    bool search_flag = false;

    while( true){

        if ( a_pre % 2 == 0){

            a_i = a_pre / 2;

        } else {

            a_i = 3 * a_pre + 1;

        }

        list[index] = a_i;

        index = index + 1;

        for ( int i = 0; i< index-1; ++ i ){

            if ( list[i] == a_i) {

                search_flag = true;

                break;

            }

        }

        if (search_flag==true){

            break;

        } 

        a_pre = a_i;



    }

    std::cout << index << std::endl;

    return 0;

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

//#define int long long

#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

#define endl '\n'

#define fori(s,n) for(int i=s;i<n;i++) 

#define forj(s,n) for(int j=s;j<n;j++) 

int OO = 1e9 + 9;

#define vii vector<int>

int n, k;

vii v;

int maxi = 1061109567;

int dp[100000 + 9];

int solve(int x) {



	if (x == n)

		return 0;

	if (x > n)

		return OO;

	int& ret = dp[x];

	if (ret != maxi) return ret;

	int r1 = abs(v[x] - v[x + 1]) + solve(x + 1);

	fori(2, k + 1)

		r1 = min(r1, abs(v[x] - v[x + i]) + solve(x + i));

	return ret = r1;

}

signed main()

{

	fast;

	memset(dp, '?', sizeof(dp));



	cin >> n >> k;

	v = vii(n + 101);

	fori(1, n + 1) cin >> v[i];

	cout << solve(1) << endl;

}


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

 ALLAH is Almighty....

*/

#include <bits/stdc++.h>



#define ll  long long

using namespace std;



const ll mx = 10e5 + 9;

ll n, k;

ll ar[ mx ];

ll dp[ mx ];



void solve( )

{

    cin >> n >> k;

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

        dp[ i ] = 1e12;

        cin >> ar[ i ];

    }



    dp[ 0 ] = 0;

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

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

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



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

}



int main()         // Md. Inzamam-ul Haque

{

    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;





}