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

using namespace std;

typedef long long ll;

const int inf=0x3f3f3f3f;

int n,k;

int h[100000+10];

int dp[100000+10];

int cha(int x,int y)

{

    if(x>=y)    return x-y;

    else    return y-x;

    return  y-x;

}

int main()

{

    cin>>n>>k;

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

    dp[0]=0;

    dp[1]=cha(h[1],h[0]);

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

    {

        int least=inf;

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

        {

            least=min(least,dp[j]+cha(h[i],h[j]));

        }

        dp[i]=least;

    }

    cout<<dp[n-1];

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

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>



using namespace std;



const int Max = 1e9 + 7;

typedef long long ll;

ll ans;



int main(){

	int i, j, n, m;

	while(~scanf("%d", &n)){

		ans = 1;

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

			ans *= i;

			ans %= Max;

		}

		cout << ans << endl;

	}

	return 0;

}

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

const long long MOD=1e9+7;

int n;

long long ans=1;

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++) ans=(ans*i)%MOD;

	printf("%lld",ans);

}

// 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 fi first

#define se second

typedef long long ll;

const ll mod=1000000007;

ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}



int dp[100001];

vector<int> stone;

int k;

int solve(int n){

	if(dp[n] > -1) return dp[n];

	if(n == 0){

		return dp[n] = 0;

	}

	int minimum = INT_MAX;

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

		if(n - i >= 0){

			minimum = min(minimum, solve(n-i) + abs(stone[n] - stone[n - i]));

		}

	}

	return dp[n] = minimum;

}

int main()

{

	ios::sync_with_stdio(false);

  	cin.tie(0);

  	int n;

  	cin >> n >> k;

  	stone.resize(n);

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

  		int m;

  		cin >> m;

  		stone[i] = m;

  	}

  	for(int i = 0; i < 100001; i++) dp[i] = -1;

  	reverse(stone.begin(), stone.end());

  	cout << solve(n-1) << endl;

	return 0;

}


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

using namespace std;



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

#define trav(a, x) for(auto& a : x)

#define pb push_back

#define mp make_pair

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

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

#define endl '\n'



typedef long long ll;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;

typedef vector<int> vi;

typedef vector<ll> vll;



const ll mod=1000000007;

//head



int main()

{

    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);



    int n, k;

    cin >> n >> k;

    vi a(n);

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

    vi dp(n,1 << 30);

    dp[0] = 0;



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

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

            if(i+j < n)

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



signed main()

{

	int k;cin>>k;

	string a;cin>>a;

	if(a.length()<=k)cout<<a<<endl;

	else{

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

		cout<<a[i];

		cout<<"..."<<endl;

	}

	return 0;

}

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

#include<string.h>

int main(){

	int k;

	char s[105];

	scanf("%d",&k);

	getchar();

	scanf("%s",&s);

	if(strlen(s)>k){

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

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

		}

		printf("...");

		printf("\n");

	}

	else{

		int l = strlen(s);

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

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

		printf("\n");

	}

}

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

#include <algorithm>

#include <string.h>

using namespace std;

#define ll long long

ll n, k, h[100009], dp[100009];

ll solve(ll ind)

{

	if (ind == n - 1)

		return 0;

	ll jump = 1e18;

	if (dp[ind] != -1)

		return dp[ind];

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

	{

		if (i + ind < n)

			jump = min(jump, abs(h[ind] - h[ind + i]) + solve(ind + i));

	}

	return dp[ind] = jump;

}

int main()

{

	cin >> n >> k;

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

		cin >> h[i];

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

	cout << solve(0);

}

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

#define ull unsigned long long

#define ll long long

#define pb push_back

#define endl '\n'

#define line cout<<"----------------------"<<endl

#define mp make_pair

#define mod 1000000007

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

#define filein freopen("input.txt","r",stdin)

#define fileout freopen("output.txt","w",stdout)

using namespace std;

const ll  mx=300009;

ll  ara[mx];

ll  cost[mx];

int main()

{

    ll n,k;

    cin>>n>>k;

    for(ll  i=1;i<=n;i++) cin>>ara[i];

    cost[1]=0;

    cost[2]=abs(ara[2]-ara[1]);

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

    {

        cost[i]=LLONG_MAX;

        for(ll  j=1;j<=min(i-1,k);j++)

        {

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

        }

    }

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;

const int mod = 1e9+9;

/*ll gcd(ll x, ll y){return (y ? gcd(y, x%y) : x); }

*ll lcm(ll x, ll y){return x*(y/gcd(x,y));}

*void exEuclid(int a, int b){

	if(b==0){x = 1; y=0; d=a; return ;}

	exEuclid(b, a%b);

	int x1=y;

	int y1=x-(a/b)*y;

	x=x1;

	y=y1;

} 

* */

//===========================================================//

const int nax = 1e5+103;

int n, k;

int dp[nax];

int a[nax];

int f(int i){

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

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

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

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

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

	}

	return dp[i]=ans;

}

int main(){

	cin>>n>>k;

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

	memset(dp, -1, sizeof dp);

	cout<<f(0)<<endl;

	return 0;

}

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

#include <fstream>

#define inf INFINITY

#define file freopen( "input.txt" , "r" , stdin) ; freopen( "output.txt" , "w" , stdout) ; 

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

#define _3102_ main()

#define endl '\n'

typedef long long ll ;

typedef unsigned long long ull ; 

typedef long double ld ; 

using namespace std ;

const int N = 1e5+100  ; 

const ll mod = 1e9 + 7  ;







int _3102_ {

    Gamed

    ll n , k , arr[N] , dp[N] ; 

    cin >> n >> k ; 

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

        cin >> arr[i] ; 

    }

    dp[0] = 0 ;  

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

        ll ans = inf , cnt = k; 

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

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

        }

        dp[i] = ans ; 

    }

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

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

#define rep(i,a) loop(i,0,a)

#define INF 100000

using namespace std;



struct place{

	int x,y;

};



int main(){

	int N;

	while(1){

		cin>>N;

		if(N==0)break;

		int n[N],p[N];

		loop(i,1,N)cin>>n[i]>>p[i];

		bool dt[401][401];

		rep(i,401)rep(j,401)dt[i][j]=false;

		dt[200][200]=true;

		place basyo[N];

		basyo[0].x=200;

		basyo[0].y=200;

		loop(i,1,N){

			int dirx,diry;

			if(p[i]==0){

				dirx=-1;

				diry=0;

			}else if(p[i]==1){

				dirx=0;

				diry=1;

			}else if(p[i]==2){

				dirx=1;

				diry=0;

			}else{

				dirx=0;

				diry=-1;

			}

			int nx=basyo[n[i]].x+dirx;

			int ny=basyo[n[i]].y+diry;

			dt[ny][nx]=true;

			basyo[i].x=nx;

			basyo[i].y=ny;

		}

		int minix,miniy,maxx,maxy;

		minix=miniy=maxx=maxy=INF;

		rep(i,401){

			rep(j,401){

				if(dt[i][j]){

					miniy=i;

					break;

				}

			}

			if(miniy!=INF)break;

		}

		rep(i,401){

			rep(j,401){

				if(dt[j][i]){

					minix=i;

					break;

				}

			}

			if(minix!=INF)break;

		}

		for(int i=400;i>=0;i--){

			for(int j=400;j>=0;j--){

				if(dt[i][j]){

					maxy=i;

					break;

				}

			}

			if(maxy!=INF)break;

		}

		for(int i=400;i>=0;i--){

			for(int j=400;j>=0;j--){

				if(dt[j][i]){

					maxx=i;

					break;

				}

			}

			if(maxx!=INF)break;

		}

		int ansx=maxx-minix+1;

		int ansy=maxy-miniy+1;

		cout<<ansx<<" "<<ansy<<endl;

	}

	return 0;

}

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

#include<vector>

#include<algorithm>

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

#define rep(i,a) loop(i,0,a)

using namespace std;

typedef pair<int,int> pii;



int dx[4]={-1,0,1,0};

int dy[4]={0,-1,0,1};



int main(){

	int n;

	while(cin>>n,n){

		vector<pii> vec;

		vec.push_back(pii(0,0));

		int m,d;

		loop(i,1,n){

			cin>>m>>d;

			pii now=vec[m];

			now.first+=dx[d];

			now.second+=dy[d];

			vec.push_back(now);

		}

		int wmi=1e6,wma=-1*1e6,hmi=1e6,hma=-1*1e6;

		rep(i,vec.size()){

			wma=max(wma,vec[i].first);

			wmi=min(wmi,vec[i].first);

			hmi=min(hmi,vec[i].second);

			hma=max(hma,vec[i].second);

		}

		cout<<wma-wmi+1<<" "<<hma-hmi+1<<endl;

	}

	return 0;

}

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

#include<vector>

#include<algorithm>

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

#define rep(i,a) loop(i,0,a)

using namespace std;

typedef pair<int,int> pii;



int dx[4]={-1,0,1,0};

int dy[4]={0,-1,0,1};



int main(){

	int n;

	while(cin>>n,n){

		vector<pii> vec;

		vec.push_back(pii(0,0));

		int m,d;

		loop(i,1,n){

			cin>>m>>d;

			pii now=vec[m];

			now.first+=dx[d];

			now.second+=dy[d];

			vec.push_back(now);

		}

		int wmi=1e6,wma=-1*1e6,hmi=1e6,hma=-1*1e6;

		rep(i,vec.size()){

			wma=max(wma,vec[i].first);

			wmi=min(wmi,vec[i].first);

			hmi=min(hmi,vec[i].second);

			hma=max(hma,vec[i].second);

		}

		cout<<wma-wmi+1<<" "<<hma-hmi+1<<endl;

	}

	return 0;

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

using namespace std;



int main(){

    long long s, i(1);

    scanf("%lld", &s);

    vector<long long> memo(1000100, -1);

    while(1){

        if(memo[s] >= 0)return 0 & printf("%lld", i);

        memo[s] = i;

        if(s & 1)s = 3 * s + 1;

        else s >>= 1;

        ++i;

    }

}

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

using namespace std;



int main(){

    long long s, i(1);

    scanf("%lld", &s);

    if(s == 4 || s == 2 || s == 1)return 0 & printf("%lld", 4);

    while(1){

        if(s == 4)return 0 & printf("%lld", i + 3);

        if(s & 1)s = 3 * s + 1;

        else s >>= 1;

        ++i;

    }

}


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

using namespace std;



#define oo (1L<<62)

#define ll long long int



int N;

int K;

int h[100005];

ll memo[100005];

char vis[100005];



ll elJoshua(int n){

	if(n < 0) return oo;

	if(!n) return 0;

	if(vis[n]) return memo[n];

	vis[n] = 1;

	ll mini = oo;

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

        if(n>=i)

            mini = min(abs(h[n]-h[n-i])+elJoshua(n-i), mini);

    return memo[n] = mini;

}   



int main() {

	cin>>N>>K;

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

		cin>>h[i];

	cout<<elJoshua(N-1)<<"\n";

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

#include<cmath>

#include<algorithm>

#include<cstring>

using namespace std;



int h[100055],dp[100055];

const int inf = 0x3f3f3f;

int main()

{

	int n,k;

	

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

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

	cin>>n>>k;

	//h[1]=inf;

	h[0]=inf;

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

		cin>>h[i];

		//dp[2]=

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

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

			{

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

			//	cout<<dp[i]<<"  "<<dp[i-j]+abs(dp[i]-dp[i-j])<<endl;

			}



	cout<<dp[n]<<endl;

	return 0;

} 

//max(h[i],h[i-2])-min(h[i],h[i-2])

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

using namespace std;

typedef long long ll;

const int inf=0x3f3f3f3f;

const int N=2e5+5;

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

int main(){

	cin>>n>>k;

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

		cin>>a[i];

	dp[1]=0;

    dp[0]=0;

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

    	dp[i]=inf;

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

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

        {

            if(i-j<1) break;

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

        }

		cout<<dp[n];

	return 0;

} 

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

#include <stdio.h>

using namespace std;

int n,k;

int main(){

    cin>>n>>k;

    int dem=0,num;

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

        cin>>num;

        if (num>=k) dem++;

    }

    cout<<dem;

}

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



int main()

{

    int n,k,ans=0;

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

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

    {

        int x;

        scanf("%d",&x);

        if(x>=k)ans++;

    }

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

    return 0;

}

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

#include<cstring>

#include<algorithm>





using namespace std;



const long long inf= 1e10+ 123;

const int mx=1e5+123;

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



int solve( int i)

{

    if(i==n)

        return 0;



    if(dp[i]!= -1)

        return dp[i];





    int ret= inf;



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

    {

        if(i+j<=n)

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

    }





    return dp[i]= ret;

}



int main()

{

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



    cin>> n >> k;

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

        cin>> h[i];



    cout<< solve(1)<< 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;





}