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

using namespace std;





int main()

{

    int n,k;

    cin>>n>>k;



    int ar[n+10],ans[n+10];



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

    {

        cin>>ar[i];

        ans[i]=INT_MAX;

    }



    ans[0]=0;





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

    {

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

        {

            int r1,r2;



            r1=abs(ar[j]-ar[i])+ans[i];

            ans[j]=min(r1,ans[j]);

        }



    }



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



    return 0;

}


// Optimized solution to the same problem (faster):
//才不露白

//还是青蛙跳还是动态规划

//唯一的是你不知道青蛙它从哪跳到了现在的石头上 如果你知道 那就好办了

//比如从k上面跳过来

//dp[i] = dp[k] + abs(h[i] - h[k])

//关键是你k不知道啊

//有想法就好 不用先急着自我否认

//枚举

//dp[1] = 0

//dp[2] = abs(h[2] - h[1])

//写个函数来算看起来不错

//dp[3] = min(从1跳过来,从2跳过来)

//dp[4] = min(从1跳,从2跳，从3跳)用循环 最后求min

//差不多2层吧 先写了试试看

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <queue>

#include <stack>

#include <vector>

#include <algorithm>



using namespace std;



const int MAXN = 1e5;

int n,k;

int h[MAXN + 5],dp[MAXN + 5];//



int CHANGE(int i,int k)//

{

	if(i <= k + 1)//一跳就到

	{//从一开始选

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

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

		{

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

			{

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

			}

		}

		return min;

	}

	else

	{

		int min = dp[i - k] + abs(h[i] - h[i - k]);

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

		{

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

			{

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

			}

		}

		return min;

	}

}



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++)

	{

		if(i == 1)

		{

			dp[i] = 0;

		}

		else if(i == 2)

		{

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

		}

		else

		{

			dp[i] = CHANGE(i,k);

		}

	}

	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):
/**

*Coder: Yeshayah

**/



#include <bits/stdc++.h>

using namespace std;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

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

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

    int k;

    string s;

    cin >> k >> s;

    if (s.size() <= k) cout << s << "\n";

    else cout << s.substr(0,k) << "...\n";

    return 0;

}

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

#include<string.h>



int main()

{

	int n;

	scanf("%d",&n);

	char str[102];

	scanf("%s",&str);

	int len=strlen(str);

	if(len>n)

	{

		str[n]='.';

		str[n+1]='.';

		str[n+2]='.';

		str[n+3]='\0';

	}

	printf("%s",str);

	return 0;

}

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

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const double PI = acos(-1);



int main()

{

	int k;

	string s;

	cin >> k >> s;

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

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

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

using namespace std;



int main(){

	int N;

	cin >> N;



	int temp, numbers[100001] = {0};

	long long sum = 0;

	while(N-- > 0){

        cin >> temp;

        numbers[temp]++;

        sum += temp;

	}



    int Q, B, C;

    cin >> Q;

    while(Q-- > 0){

        cin >> B >> C;

        sum += (C - B) * numbers[B];

        numbers[C] += numbers[B];

        numbers[B] = 0;

        cout << sum << endl;

    }

	return 0;

}


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

#define rush() int T;scanf("%d",&T);int kase=1;while(T--)

#define pb(x) push_back(x)

#define pr pair<int,int>

#define mem(a) memset(a,0,sizeof(a))

#define  fi first

#define  se second

using namespace std;

typedef long long ll;

const ll maxn=2e6+5;

const ll mod=1e9+7;

ll quickpow(ll x,ll y,ll mod){ll ans=1;while(y){if(y&1)ans=ans*x%mod;x=x*x%mod;y>>=1;}return ans;}



int a[maxn]={0};



int main()

{

    int n;

    cin>>n;

    ll sum=0;

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

    {

        int x;

        cin>>x;

        sum+=x;

        a[x]++;

    }

    int q;

    cin>>q;

    while(q--)

    {

        int b,c;

        cin>>b>>c;

        sum+=(c-b)*a[b];

        a[c]+=a[b];

        a[b]=0;

        cout<<sum<<endl;

    }

    return 0;

}


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

using namespace std;

#define ll long long

#include <string>

const int mxN=1e5;

const int maxN=5e3;

#define ld long double

#define pb push_back

#define mp make_pair

#define ins insert

#define vi vector<int>



int main()

{

    int n;

    cin >> n;

    int cnt[100000]={};

    ll s=0;

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

    {

        int x;

        cin >> x;

        cnt[x-1]++;

        s+=x;

    }

    int q;

    cin >> q;

    while(q--)

    {

        int b,c;

        cin >> b >> c;

        s+=cnt[b-1]*(c-b);

        cnt[c-1]+=cnt[b-1];

        cnt[b-1]=0;

        cout << s << "\n";

    }

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

#include<algorithm>

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

#define Z 20000

using namespace std;

int d[Z+1];

int main(){

	int n,m,k,r,l;

	while(true){

		cin>>n>>k>>m>>r;

		if(!(n+m+k+r))break;

		rep(i,0,Z)d[i]=0;

		k=1,l=n;

		rep(i,0,Z)d[i]+=k/l,k%=l,k*=10;

		if(m)rep(i,1,n){k=1,l=i*n;rep(j,0,Z)d[j]+=k/l,k%=l,k*=10;}

		for(int i=Z;i>0;i--)d[i-1]+=d[i]/10,d[i]%=10;

		cout<<d[0]<<".";

		rep(i,1,r+1)cout<<d[i];

		cout<<endl;

	}

	return 0;

}

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

#include<algorithm>

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

#define Z 15000

using namespace std;

int d[Z+1];

int main(){

	int n,m,k,r,l;

	while(true){

		cin>>n>>k>>m>>r;

		if(!(n+m+k+r))break;

		rep(i,0,Z)d[i]=0;

		k=1,l=n;

		rep(i,0,Z)d[i]+=k/l,k%=l,k*=10;

		if(m)rep(i,1,n){k=1,l=i*n;rep(j,0,Z)d[j]+=k/l,k%=l,k*=10;}

		for(int i=Z;i>0;i--)d[i-1]+=d[i]/10,d[i]%=10;

		cout<<d[0]<<".";

		rep(i,1,r+1)cout<<d[i];

		cout<<endl;

	}

	return 0;

}

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

#include<vector>

using namespace std;

typedef vector<int> BigDecimal;



int n,k,m,r;

BigDecimal tmp(10010);



void Div(int x){

  int now = 1;

  for(int i=0;i<r+10;i++){

    tmp[i] = now/x;

    now %= x;

    now *= 10;

  }

}



void print(const BigDecimal &b){

  cout << b[0] << ".";

  for(int i=1;i<=r;i++)cout << b[i];

  cout << endl;

}



int main(){

  while(cin >> n >> k >> m >> r,n){

    Div(n);

    BigDecimal res = tmp;

    if(m==1){

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

	Div(i*n);

	for(int j=0;j<r+10;j++)res[j] += tmp[j];

      }

      for(int j=r+10-1;j>0;j--){

	res[j-1] += res[j]/10;

	res[j] %= 10;

      }

    }



    print(res);

  }

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

using namespace std;

typedef long long ll;

const int maxn=1000010;

const int inf=0x3f3f3f3f;



ll n;

ll dp[maxn];

ll a[maxn];

ll k;

void dfs()

{

    

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

    dp[0]=0;

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

    {

        dp[i]=inf;

    }

    

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

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

        {

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

        }

    }

    cout<<dp[n-1];

    return ;

}



int main()

 { 

      cin>>n>>k;

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

      cin>>a[i];

      dfs();

 }

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

#include<algorithm>



int main(){

  int s,k;

  long long st[100001],p[100001];

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

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

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

  }

  p[0]=0;

  p[1]=0;

  p[2]=(st[2]-st[1] < 0) ? st[1]-st[2] : st[2]-st[1];

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

    p[i] = 1000000001;

  }

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

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

      if(i-j>=1){

	long long tmp;

	tmp = (st[i]-st[i-j] < 0) ? st[i-j]-st[i] : st[i]-st[i-j];

	if(p[i] > p[i-j]+tmp){

	  p[i] = p[i-j]+tmp;

	}

	if(tmp == 0) break;

      }else{

	break;

      }

    }

  }

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

  return 0;

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

using namespace std;

int main() {

    int k,x,a,b;

    cin>>k>>x;

    a=x-k+1;

    b=x+k-1;

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

        cout<<i<<" ";

    }

return 0;

}


// Optimized solution to the same problem (faster):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

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



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

        printf("%d ",i);

    }

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

#include <algorithm>



using namespace std;



int main(){

    int N, K;

    cin >> N >> K;



    int p1, px, mins[N], alturas[N];



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

        if(i < N){

            cin >> px;

            alturas[i] = px;

        }

        else {

            cout << mins[N-1];

        }

        if(i == 0){

            p1 = px; 

        }

        else if(i == 1){

            mins[1] = abs(p1 - px);

        }

        else {

            int saltos = min(K, min(N, i));

            mins[i] = 1000000000; 

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

                if(j == i-1){

                    mins[i] = min(mins[i], abs(p1 - px));

                }

                else {

                    int ult_salto = abs(px - alturas[i-j-1]);

                    mins[i] = min(mins[i], ult_salto + mins[i-j-1]); 

                }

            }

        }

    }

    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;



long dp[100001], a[100001], n, k;



long magic(int idx){

	if(idx == n)

		return 0;

	if(idx > n)

		return 1e9 + 7;

	long &ret = dp[idx];

	if(~ret)

		return ret;

	ret = INT_MAX;

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

		ret = min(ret, abs(a[idx] - a[idx + i]) + magic(idx + i));

	}

	return ret;

}



int main(){

 

	ios::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);



	memset(dp, -1, sizeof dp);

	cin >> n >> k;

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

		cin >> a[i];

	cout << magic(1);

	return 0;

}

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

using namespace std;



int main(){ 

    

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    

    int n, k, a[100001];

    cin >> n >> k;

    vector<int> dp(n, INT_MAX);

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

        cin >> a[i];

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

            else

                break;

        }

    }

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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

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

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

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

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

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

#define     Endl            "\n";

#define     tab             "\t"

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

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

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

#define     pi              2*acos(0.0)

#define     inf             INT_MAX

const int sz=1e5+9;

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



int clac(int i){

    if(i==n) return 0;

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

/*    int left=abs(arr[i]-arr[i+1])+clac(i+1);

    int right=inf;

    if(i+2<=n){

        right=abs(arr[i]-arr[i+2])+clac(i+2);

    }

    */

    int ret1=inf;

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

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

    }

    return dp[i]=ret1;

}



void solve(){

    cin>>n>>k;

    reset(dp,-1);

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

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

}



int main(){

    //FasterIO;

    #ifndef ONLINE_JUDGE

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

   #endif

   // int t;

    //cin>>t;

    //while(t--)

        solve();

}


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



using namespace std;



#define endl '\n'



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);



  int n, k;

  cin >> n >> k;

  vector<int> stones(n);

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

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

    cin >> stones[i];

  }

  dp[0] = 0;

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

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

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

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



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

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

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

#define M_PI 3.14159265358979323846

#define mod 1000000007

#define endl "\n"

using namespace std;

using ll = long long;

using lli = long long int;

using ld = long double;





const int N = 5e5 + 6, inf = 2e9;

int n,k;

int arr[N], dp[N];

int main()

{

	MEGA_MIND



		cin >> n >>k;

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

	{

		dp[i] = inf;

		cin >> arr[i];

	}



	dp[0] = 0;

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

	{

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

		{

			if(j<n)

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

		}

	}

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





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

#include <math.h>

#include <string.h>

#include <vector>

#include <algorithm>

#include <stack>



using namespace std;

typedef long long ll;







int main(){

    ll n,k,h[1000000];cin>>n>>k;

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

    ll dp[n];

    dp[0]=0;

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

        dp[i]=10000000000;

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

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

        }

    }

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

    

    return 0;

}




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

using namespace std;

typedef long long ll;

//#include <boost/multiprecision/cpp_ll.hpp>

//typedef boost::multiprecision::cpp_ll ll;

typedef long double dd;

#define i_7 (ll)(1E9+7)

//#define i_7 998244353

#define i_5 i_7-2

ll mod(ll a){

    ll c=a%i_7;

    if(c>=0)return c;

    return c+i_7;

}

typedef pair<ll,ll> l_l;

ll inf=(ll)1E16;

#define rep(i,l,r) for(ll i=l;i<=r;i++)

#define pb push_back

ll max(ll a,ll b){if(a<b)return b;else return a;}

ll min(ll a,ll b){if(a>b)return b;else return a;}

void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);

void Min(ll &pos,ll val){pos=min(pos,val);}

void Add(ll &pos,ll val){pos=mod(pos+val);}

dd EPS=1E-9;

string alp="abcdefghijklmnopqrstuvwxyz";

#define endl "\n"

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







int main(){fastio

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

    ll h[n];rep(i,0,n-1)cin>>h[i];

    ll dp[n];

    dp[0]=0;

    rep(i,1,n-1){

        dp[i]=inf;

        rep(j,max(i-k,0),i-1){

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





}