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

#include<climits>

using namespace std;



int dp_compute(int a[],int n,int k){

	int dp[100009]={};

	dp[0]=0;



	int best=INT_MAX;

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

		best=INT_MAX;

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

			if(i-j>=0){

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

			}

		}

		dp[i]=best;

	}

	return dp[n-1];

}

int main(){

	int n,k;

	cin>>n>>k;

	int a[n];

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

		cin>>a[i];

	}

	int ans=dp_compute(a,n,k);

	cout<<ans<<endl;

	return 0;

}


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

using namespace std;

#define ll  long long

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define endl '\n'

#define mod 1000000007



void inp()

{

    #ifndef ONLINE_JUDGE

    // for getting input from input.txt

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

    // for writing output to output.txt

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

    #endif

} 

ll min(ll a,ll b)

{

    if(a<b)

        return a;

    else

        return b;

}

 

ll max(ll a,ll b)

{

    if(a>b)

        return a;

    else

        return b;

}

int main() {

    //inp();

    fio;

    ll n,k;

    cin>>n>>k;



    vector<int> nums(n);

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

    {

    	cin>>nums[i];

    }



    vector<int> dp(n,INT_MAX);

    dp[0]=0;

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

    {

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

        {

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

        }

    }

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



using namespace std;



typedef long long ll;

typedef vector <int> vi;

typedef vector <ll> vl;

typedef pair <ll, ll> dl;

typedef pair <int, int> ii;

typedef vector <ii> vii;

typedef vector <vi> graph;



int n, k;

ll arr[100010];

ll dp[100010];



ll piedra(int id) {

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

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

	ll temp, menor = 1000000000;

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

	{

		temp = abs(arr[id] - arr[id + i]) + piedra(id + i);

		menor = min(temp, menor);

	}

	return dp[id] = menor;

}



int main() {

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



	cin >> n >> k;

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

	{

		cin >> arr[i];

	}

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

	{

		dp[i] = -1;

	}

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

    return 0;

}


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

#define tam 100010

#define inf 1000000000



using namespace std;



typedef long long ll;

typedef vector <int> vi;

typedef vector <ll> vl;

typedef pair <ll, ll> dl;

typedef pair <int, int> ii;

typedef vector <ii> vii;

typedef vector <vii> graph;



bool comp(int a, int b) {

	return a > b;

}



int main() {

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



	int n, k;

	cin >> n >> k;

	int arr[n];

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

	{

		cin >> arr[i];

	}

	vi dp(n, inf);

	dp[0] = 0;

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

	{

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

		{

			dp[i + j] = min(dp[i + j], dp[i] + abs(arr[i] - arr[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):
#include<iostream>

using namespace std;

int main()

{

	

	int k;

     string s;

     cin>>k>>s;

     if(s.length()>k)

	 {

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

	 	cout<<s[i];

	 	cout<<"...";

	 }

	else

	cout<<s;

	

	

	

	

	return 0;

 } 

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

	# include <string.h>

	int main () {

		int a = 0;

		int c =0;

	char name [100];

		scanf ("%d",&a);

		scanf("%s%[^\n]",&name);

		c = strlen (name);

		if (c<=a) {

			printf ("%s",name);

		} else if (c>a) {

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

				printf ("%c",name[i]);

			}

			printf("...");

		}

		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;

int arr[200005],k,n,dp[200005];

int solve(int index)

{

    if(index==n-1)

        return 0;

    if(dp[index]!=-1)

        return dp[index];

    int ans=1e9;

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

        if(index+i<n)

            ans=min(ans,abs(arr[index]-arr[index+i])+solve(index+i));





    return dp[index]=ans;

}

int main()

{

    cin>>n>>k;

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

        cin>>arr[i];

    memset(dp,-1,sizeof dp);



    cout << solve(0);

    return 0;

}


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



using namespace std;



int v[100007];

int main() {

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

vector<int> dp(n+1);

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

dp[n] = 0;

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

    dp[i] = 0x3f3f3f3f;

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

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

}



cout << dp[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):
/// TOP-DOWN - RECURSIVA;



#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

long long dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1e9;



int frog2(int i,int j){



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

        return dp[i];

    }



    int menor = inf;



    if(i>=j) return dp[i]=0;



    ///**OUU ASSIM COM ESSE MEU FOR ,,

    /*

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

        if(i+x <= j){

            long long cost = (frog2(i+x,j)+ abs(pedras[i]-pedras[i+x]));



            if(cost < menor){

                menor = cost;

            }

        }

    }*/



    ///**OUU ASSIM COM OQ VI NA NET,,



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

        if(i+x <= j){

            int cost = (frog2(i+x,j)+ abs(pedras[i]-pedras[i+x]));

            menor = min(menor,cost);

        }

    }



    return dp[i] = menor;

}



int main(){



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



    cin >>n>>k;



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

        cin >> pedras[i];

    }



    cout<<frog2(0,n-1)<<endl;

}

/**/


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

#include <algorithm>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

int dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1000000/*1e9*/;



int main(){



    memset(dp,inf,sizeof(dp));

    cin >>n>>k;



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

        cin >> pedras[i];

    }



    ///Comeca aki a bagaca

    dp[n-1]=0;



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



        ///int menor=inf;



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

            if(i+j<=n-1){

                int cost = abs(pedras[i]-pedras[i+j]);

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

                /*menor = min(menor,dp[i+j]+cost);*/

            }

        }



        ///dp[i]=menor;

    }



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



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

#define ll long long

#define endl '\n'



int main()

{

	int n,k,coun = 0,a;

	cin >> n >> k;

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

	{

		cin >> a;

		if(a >= k) coun++;

	}

	cout<< coun << endl;

	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 forl(V, N) for(int V = 0; V < N; V++)

#define forll(V, S, E) for(int V = S; V < E; V++)

#define forlc(V, S, E, I) for(int V = S; V < E; V += I)

#define fore(E, X) for(auto &E:X)

#define print(X) cout << X

#define printsp(X) cout << X << " "

#define println(X) cout << X << endl

#define rem(X) X.top(); X.pop()

#define mid(A, B) ((A+B) / 2)

#define setmin(A, B) A = min(A, B)

#define graph unordered_map<int, unordered_set<int>>

#define vi vector<int>

#define pb push_back

#define ll long long



int N, K;

int H[100101];

int DP[100101];



int main() {

    cin >> N >> K;

    forl(i, N) cin >> H[i];

    fill_n(DP, 100000, INT_MAX);

    DP[0] = 0;

    forl(i, N-1) {

        for(int j = i+1; j < i+K+1; j++) setmin(DP[j], abs(H[i]-H[j]) + DP[i]);

    }

    println(DP[N-1]);

}


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

#include <bits/stdc++.h>

using namespace std;



string FILENAME = "template";

#define io ifstream fin(FILENAME + ".in"); ofstream fout(FILENAME + ".out")

#define fwrite(X) fout << X;

#define fwriteln(X) fout << X << endl;



#define forl(V, N) for(int V = 0; V < N; V++)

#define fore(E, X) for(auto &E:X)

#define print(X) cout << X

#define println(X) cout << X << endl

#define printe(X) for(auto &E:X) cout << E << " "; cout << endl

#define rem(X) X.top(); X.pop()

#define mid(A, B) ((A+B) / 2)

#define setmin(A, B) A = min(A, B)

#define setmax(A, B) A = max(A, B)

#define graph unordered_map<int, unordered_set<int>>

#define vi vector<int>

#define pb push_back

#define ll long long



int main() {

    int N, K;

    cin >> N >> K;

    vi stones(N);

    forl(i, N) cin >> stones[i];

    vector<ll> dp(2*N+K+10);

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

    dp[0] = 0;

    forl(i, N) {

        forl(j, K) setmin(dp[i+j+1], dp[i]+abs(stones[i]-stones[i+j+1]));

    }

    println(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 ll long long

int n, k, h[100000], dp[100000];



int cost(int i){

	//base case

	if(i>= n-1){

		return 0;

	}

	//recursive case

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



	int ans=INT_MAX;



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

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

	}



	dp[i]=ans;

	return dp[i];

}



int main(){

	ios_base::sync_with_stdio(false);

    cin.tie(NULL);

	cin>>n>>k;

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

		cin>>h[i];

	}

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

	cout<<cost(0);

}

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



*/

#include <bits/stdc++.h>



using namespace std;



#define ll           long long int

#define mp           make_pair

#define pb           push_back

#define fi           first

#define si           second

#define fastIO       ios_base::sync_with_stdio(false); cin.tie(NULL);

#define F(a,n)       for(int i=0;i<n;i++){cin>>a[i];}

#define F1(a,n)      for(int i=1;i<=n;i++){cin>>a[i];}

#define P(a,n)       for(int i=0;i<n;i++){cout<<a[i]<<' ';}cout<<endl;

#define P1(a,n)      for(int i=1;i<=n;i++){cout<<a[i]<<' ';}cout<<endl;

#define NF(a,n,m)    for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>a[i][j];}}

#define NF1(a,n,m)   for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>a[i][j];}}

#define PNF(a,n,m)   for(int i=0;i<n;i++){for(int j=0;j<m;j++){cout<<a[i][j]<<' ';}cout<<endl;}cout<<endl;

#define PNF1(a,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<a[i][j]<<' ';}cout<<endl;}cout<<endl;

const int nax = 1e7;

const int mod = 1e9+7;







int main(){

fastIO

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

	ll *h = new ll[n];

	F(h, n);

	ll *dp = new ll[n];

	dp[0] = 0;

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

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

		dp[i] = INT_MAX;

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

			if(i-j >= 0){

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

			}

			else{

				break;

			}

		}

	}

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

#include <sstream>

#include <cstring>

#include <fstream>

using namespace std;

const int N = 50000;

bool prime[N+1];



void make_prime(void)

{

	int i,j;

	memset (prime, true , sizeof(prime ) );



	prime[0] = false;

	prime[1] = false;



	for (j = 0; j <= N; ++j){

		if (prime[j]){

			int p = j;

			for (i = 2*p; i <= N; i += p ){

				if (prime[i] ){

					prime[i] = false;

				} // end if

			} // end for

		} // end if	

	} // end for

}



int main()

{

//	cut here before submit 

//	freopen ("testcase.goldbach", "r", stdin);

	string str = "";

	make_prime();



	while (getline (cin, str ) ){

		int n;

		stringstream ss(str);

		ss >> n;

		if (n == 0 ){

			break;

		} // end if

		int res = 0;

		int i;



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

			if (prime[i] && prime[n-i] ){

				++res;

			} // end for

		} // end for

		cout << res << endl;

	} // end loop

		

	return 0;

}

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

#include <cstring>	// require memset 



using namespace std;



const int N = 50000;

bool prime[N+1];



void make_prime (void)

{

	memset (prime, true , sizeof(prime ) );



	prime[0] = false;

	prime[1] = false;



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

		if (prime[i] ) {

			for (int j = i*i; j <= N; j += i ) {

				prime[j] = false;

			} // end for

		} // end if

	} // end for

}



int main()

{

	make_prime();



	int n;

	while (cin >> n && n ) {

		int res = 0;



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

			if (prime[i] && prime[n-i] ){

				++res;

			} // end for

		} // end for

		cout << res << endl;

	} // end loop

		

	return 0;

}

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

using namespace std;



int main()

{

  bool is_prime[100000];

  is_prime[0] = is_prime[1] = false;

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

    is_prime[i] = true;

  }



  for(int i=2; i*i<100000; i++){

    if(is_prime[i]){

      for(int j=i*i; j<100000; j+=i){

        is_prime[j] = false;

      }

    }

  }



  while(1){

    int n;

    cin >> n;



    if(n == 0)

      break;



    int cnt=0;



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

      if(is_prime[i] && is_prime[n-i])

        if(i+(n-i) == n) cnt++;

    }



    cout << cnt << endl;

  }

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


#include <bits/stdc++.h>



using namespace std;



const int N = 1e5 + 5;

const int INF = 2e9;



int h[N];

int n , k;



int mem[N];



int dp(int i){

    // base case

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



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



    int ans = INF;

    // try to jump 1 or 2 or ,,,, or k stones

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

            if(i + j <= n-1){

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

            }

    }



    mem[i] = ans;

    return mem[i];

}



main(){



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



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

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

    }



    memset(mem , -1 , sizeof mem);

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



    return 0;

}


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


#include <bits/stdc++.h>



using namespace std;



/*



    bottom up



*/



const int N = 1e5 + 5;

const int INF = 2e9;



int n , k;

int h[N];

int dp[N];



main(){



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



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

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

    }



    // initialize dp table

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

            dp[i] = INF;

    }

    dp[0] = 0;



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

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

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

            }

    }



    printf("%d\n" , 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 <iostream>

#include <bits/stdc++.h>

#define f first

#define se second

#define ll long long

#define mp make_pair

#define pb push_back

#define ull unsigned long long



using namespace std;

const int N = (int)(1e5 + 123);

const int INF = (int)(1e9 + 7);



int dp[N] , a[N]; 



int main() {

	int n , k;

	cin >> n >> k;

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

		cin >> a[i];

	}                     

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

		dp[i] = INF;

	}                    

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

		int rep = 1;

		while (i+rep <= n && rep <= k) {

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

			rep++;

		}

	}

	cout <<dp[n];

	return 0;

}

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

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

int n,k,a[100005],f[100005];

int main()

{

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

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

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

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

	f[1]=0;

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

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

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





}