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



// #include <ext/pb_ds/assoc_container.hpp>

// #include <ext/pb_ds/tree_policy.hpp>

// #include <ext/pb_ds/detail/standard_policies.hpp>



using namespace std;

// using namespace __gnu_pbds;



#define int ll

#define endl "\n"

#define pb push_back

#define mp make_pair

#define ff first

#define ss second

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

#define vi vector<int>

#define pii pair<int,int>

#define For(i,s,e) for (ll i=(s); i<(e); i++)

#define MAX 500005

#define initialize(arr,elem) memset(arr,elem,sizeof(arr))



typedef long long ll;

// typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int memo[MAX], arr[MAX];

int k = 2;

int solve(int i){

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

	if (i==1) return memo[i] = 0;

	int dp = 100000000000000;

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

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

	}

	return memo[i] = dp;

}

signed main()

{

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

		

	

	int n;

	cin>>n>>k;

	initialize(memo,-1);

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

	cout<<solve(n)<<endl;

	// For(i,1,3) cout<<memo[i]<<" ";







	return 0;

}


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

 

using namespace std;



typedef long long ll;

#define int ll

#define pb push_back

const int INF = 1e18;



signed main(){



	ios_base::sync_with_stdio(false);

	cin.tie(0);



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

	int cost[n+1];

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

		cin>>cost[i];



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

	dp[1] = 0;

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

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

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

using namespace std;





int n,k, heights[100005];

const int INF = 2000000000;

int main(){

    ios_base::sync_with_stdio(), cin.tie(nullptr), cout.tie(nullptr);

    cin >> n >> k;

    int dp[n];

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

        cin >> heights[i], dp[i] = INF;

    dp[0] = 0;

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

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

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

        }

    }

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



using namespace std;



int dp[(int)1e5+105];

int main()

{



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







    fill(dp, dp+(int)1e5+105 , INT_MAX );



    dp[1] = 0;

    int n,k;

    cin>>n>>k;

    vector <int> v(n+k+5,0);

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



        cin>>v[i];



    }

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

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

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

    }

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

}


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

/**

*   author: j o s e m a g o

*   date:

**/

using namespace std;

#pragma GCC optimize ("Ofast")

#define f first

#define s second

#define mp make_pair

#define lb lower_bound

#define ub upper_bound

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

#define mset(a,v) memset(a,v,sizeof(a))

#define pb push_back

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

#define rsz resize

#define ln "\n"



typedef double db;

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

const ld PI = 2*acos(0.0);

const int MOD = 1000000007;

const int JJJ = 100009;









int main(){



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

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



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



    int N,k;

    cin >> N >> k;

    vector<int>h(N+k+1);

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

        cin >> h[i];

    }

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

    dp[0] = 0;

    for(int i = 1;i < k;i++) dp[i] = abs(h[i]-h[0]);

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

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

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

        }

    }



//    for(int i = 0;i < N;i++) cerr<<dp[i]<<" ";

    cout<<dp[N-1];









    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 = 100010;

const int oo = 1000000000;

int n, k, v[N], dp[N];

int f(int pos) {

	if(dp[pos] != -1)

		return dp[pos];

	if (pos == (n - 1))

		return 0;

	if (pos > n)

		return oo;

	int answer = oo;

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

		answer = min(answer, f(pos + i) + abs(v[pos] - v[pos + i]));

	return dp[pos] = answer;

}

int main(){

	cin >> n >> k;

	memset(dp, -1, sizeof dp);

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

		cin >> v[i];

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

    return 0;

}


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

using namespace std;



int main() {

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

    int n, k;

    cin >> n >> k;

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

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

        cin >> h[i];

    }

    dp[0] = 0;

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

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

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

        if( j <= i ) {

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

        }

    }

    cout << dp[n-1] << '\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):
/*

	author: Ibrahim-Elsayed (Codeforces Handle)

	date: 8/28/2020

*/



#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back

const double PI = acos(-1);

const int MOD = 1000000007;



const int mxN = 1e5;

int n, k;

int dp[mxN+1];

int arr[mxN+1];



int go(int i) { // min cost starting from stone i to reach stone n;

	if(i == n) {

		return 0;

	}

	if(i > n) {

		return int(1000000026);

	}

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

		return dp[i];

	}

	int ans = 1000000025;

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

		if(i+kk <= n) ans = min(ans, go(i+kk)+abs(arr[i] - arr[i+kk]));

	}

	return dp[i] = ans;

}



int main() {

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	cin >> n >> k;

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

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

	cout << go(1);



	return 0;

}


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

	author: Ibrahim-Elsayed (Codeforces Handle)

	date: 9/17/2020

*/

//#pragma GCC optimize("Ofast")

//#pragma GCC target("avx,avx2,fma")

//#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define pb push_back

typedef long long ll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const double PI = acos(-1);



int main() {

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int64_t n, k;

	cin >> n >> k;

	vector<int64_t> arr(n);

	for(int64_t& el : arr) cin >> el;

	vector<int64_t> dp(n, int(1e18));

	dp[n-1] = 0;

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

		for(int kk = 0; kk <= k; kk++) {

			if((i+kk) < n) dp[i] = min(dp[i], dp[i+kk] + abs(arr[i] - arr[i+kk]));

		}

	}

	cout << dp[0] << "\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<cstdio>

#include<algorithm>

#include<cmath>

#include<cstring> 

#include<queue>

#include<stack> 

#include<string.h>

#include<set>

#define ll long long

using namespace std;

int main()

{

	int a,b,c;

	while(~scanf("%d%d%d",&a,&b,&c))

	{

		int flag=0;

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

		{

			if((i*a)%b==c%b)

			{

				printf("YES\n");

				flag=1;

				break;

			}

		}

		if(!flag)

		{

			printf("NO\n");

		}

	}

	return 0;

}

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

#include<cmath>



int a,b,c;



int main()

{

	scanf("%d %d %d",&a,&b,&c);

	int flag = 0;

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

		if((b * k + c) % a == 0) {

			printf("YES\n");

			flag = 1;

			break;

		}

	}

	if(!flag) printf("NO\n");

	return 0;

}

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

using ll = long long;

using namespace std;



int main() {

  int A,B,C;

  cin>>A>>B>>C;

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

    if ((A*i-C)%B==0&&(A*i-C)>=0){

      cout << "YES"<<endl;

      return 0;

    }

  }

  cout << "NO"<<endl;

  return 0;

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

using namespace std;



int n, k, x, ans;

int main () {

	

	cin >> n >> k;

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

		cin >> x;

		

		if (x>=k) ans++;

	}

	cout << ans;



return 0;

}

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

int main()

{

	int a,n,k,h[100000],cnt=0;

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

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

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

		if(h[a]>=k){

			cnt++;

		}

	}

	printf("%d",cnt);

}

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

using namespace std ;

 

int main()

{

  int n , k , a , answer ;

  cin >> n >> k ;

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

  {

    cin >> a ;

    if(a >= k)

      answer++ ;

  }

  cout << answer << endl ;

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

using namespace std;



#define clr(arr,val) memset(arr,val ,sizeof arr)

//#define first f

//#define second s



typedef  long long ull;

typedef long long ll;

int const oo=(int)1e9 ;



int const SZ= (int)1e5+1;

//const ll mod=(ll)1e9+7;

int arr[SZ];

int dp[SZ];

int n,k;

//int mn=oo;

int solve(int i){

    

    if(i==n-1)

        return 0;

    if(i>=n)

        return oo;

    

    int &ret=dp[i];

    

    if(ret!=-1)

        return ret;

    ret=oo;

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

        int ch1=abs(arr[i]-arr[i+j])+solve(i+j);

        ret=min(ret,ch1);

        

    }

    return ret;

    

}

int main() {

//    int t ;

//    cin>>t;

//    while(t--){

        cin>>n>>k;

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

                  cin>>arr[i];

        clr(dp,-1);

        cout<<solve(0)<<endl;

//    }

    return 0;

}

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

using namespace std;

int f[200005];

int n,k;

int h[200005];

int main()

{

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

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

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

	for(int i=1;i<=n;++i) f[i]=1e9;

    f[1]=0;	

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

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

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

	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<cstdio>

#include<cstring>

#include<algorithm>

struct node{

	int mins,id1;

	int smin,id2;

}dp[262149];

using std::min;

using std::max;

int n,a[262149];

void merge(int i,int j){

	if(dp[i].mins<dp[j].mins){

		dp[i].smin=max(dp[i].mins,dp[j].smin);

		dp[i].id2=dp[i].mins>dp[j].smin?dp[i].id1:dp[j].id2;

		dp[i].mins=dp[j].mins; dp[i].id1=dp[j].id1;

	}

	else if(dp[i].smin<dp[j].mins&&dp[j].id1!=dp[i].id1)

		dp[i].smin=dp[j].mins,dp[i].id2=dp[j].id1;

	return ;

}

int main(){

	scanf("%d",&n);

	memset(dp,0,sizeof dp);

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

	dp[0]=(node){a[0],0,-1,-1};

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

		if(dp[i].smin) continue;

		dp[i]=(node){a[i],i,-1,-1};

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

			if((1<<j)&i) merge(i,i^(1<<j));

	}

	int lans=-1;

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

		printf("%d\n",lans=max(lans,dp[k].smin+dp[k].mins));

	}

	return 0;

}

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

using namespace std;



int n, a[(1 << 18) + 5], ans[(1 << 18) + 5];



int main() {

	scanf("%d", &n);

	n = 1 << n;



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

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



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

		int x = a[0], y = 0;

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

			if(a[msk] > x) {

				y = x;

				x = a[msk];

			}



			else if(a[msk] > y)

				y = a[msk];

		}



		ans[mask] = x + y;

	}



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

		ans[i] = max(ans[i - 1], ans[i]);

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

	}



	return 0;

}

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

	#define mx 300005

	using namespace std;



	int a[mx], n;

	vector<vector<bool> > vis(mx, vector<bool> (20, false));

	vector<vector<pair<int,int> > > memo(mx, vector<pair<int, int> > (20, {0, 0}));



	void dp(int num, int pos) {

		if(pos == 0) {

			memo[num][0].first = a[num];

			vis[num][0] = true;

			return;

		}

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

 		vector<int> cons;

		dp(num, pos-1);

		cons.push_back(memo[num][pos-1].first);

		cons.push_back(memo[num][pos-1].second);

		if(num & (1<<(pos-1))) {

			dp(num ^ (1<<(pos-1)), pos-1);

			cons.push_back(memo[num ^ (1<<(pos-1))][pos-1].first);

			cons.push_back(memo[num ^ (1<<(pos-1))][pos-1].second);

		}

		sort(cons.begin(), cons.end(), greater<int>());

		memo[num][pos].first = cons[0];

		memo[num][pos].second = cons[1];

		vis[num][pos] = true;

	}



	int main() {

		ios_base::sync_with_stdio(false);

	    cin.tie(NULL);

	    cout.tie(NULL);



		cin >> n;

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

			cin >> a[i];

			

		}

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

			if(!vis[i][n]) {

				dp(i, n);

			}

		}

		int mxv = 0;

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

			mxv = max(memo[i][n].first + memo[i][n].second, mxv);

			cout << mxv << "\n";	

		}

		return 0;

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

#include<algorithm>

using namespace std;

int main() {

	int i=0,x,m,n=0;

	cin>>x>>m;

	x=x-1;

	int j=m;

	int s=(2*x)-1;

	int h[s];

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

		j--;

		h[n]=j;

		n++;

		}

	j=m;

	h[n]=j;

	n++;

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

		j++;

        h[n]=j;

        n++;

	}

	int k=n;

    sort(h,h+k);

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

        cout<<h[i]<<" " ;

	return 0;

}


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



int v[100005];

int ans[100005];

int n, k;



int main () {

  cin>>n>>k;

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

    int a;

    cin>>a;

    v[i] = a;

  }



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

  ans[0]=0;

  

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

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

      int valorNovo = ans[i] + abs(v[i]-v[i+j]);

      

      if (ans[i+j]==-1)

        ans[i+j] = valorNovo;

      else if (ans[i+j]>=valorNovo)

        ans[i+j] = valorNovo;



    }

  }

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



}




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

#include <cmath>

using namespace std;



int SW( int n, int k ){

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

    int menor = (1 << 30);

    

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

        cin >> h[i]; 

    }

    

    custo[1] = 0;

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



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

        menor = (1 << 30);

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

            if( i-j>0 ){

            custo[i]=custo[i-j] + abs(h[i]-h[i-j]);

            }

            if( custo[i]<menor ){

                menor = custo[i];

            }

        }

        custo[i] = menor;

    }



    return custo[n];

}





int main() {

int n,k;



cin >> n >> k;



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



#define F first

#define S second



int N, K;

int H[100005];

int dp[100001];



int main() {

	cin >> N >> K; 

	H[0] = INT32_MAX; // to avoid RTE

	for (int i = 1; i <= N; i++) cin >> H[i];

	for (int i = 0; i <= N; i++) dp[i] = INT32_MAX;

	int ans = 0;

	

	dp[N] = 0; // the base case

	K = min(N - 1, K);

	

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

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

			if (i - j < 0) break;

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

		}

	}



	cout << dp[1] << endl;



	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;





}