stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
/**

 *  Author1: low-equipped w_udixixi

 *  Author2: Sher丶lock

 *  Date :2019-08-20

 **/

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<iostream>

#include<string>

#include<vector>

#include<stack>

#include<bitset>

#include<cstdlib>

#include<cmath>

#include<set>

#include<list>

#include<deque>

#include<queue>

#include<map>

#define ll long long

#define pb push_back

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

#define repp(x,a,b) for (int x=a;x<b;x++)

#define pi 3.14159265358979323846

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

#define lson rt<<1,l,mid

#define rson rt<<1|1,mid+1,r

using namespace std;

const int maxn=2e6+7;

const int INF=1e9;

const ll INFF=1e18;

int a[maxn];

int dp[maxn];

int n,k;

int main()

{

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

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

    rep(i,1,n)

        dp[i]=i==1?0:dp[i]=INF;

    rep(i,1,n)

        for (int j=k;j>=1;j--)

            if (i+j<=n)

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

    //rep(i,1,n)cout<<dp[i]<<endl;

    cout<<dp[n]<<endl;

}


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

using namespace std;

int main()

{

    int n;

    string s;

    cin>>n;

    cin>>s;

    int a=s.length();

    if(a>n)

    {

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

        {

            cout<<s[i];

        }

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

    }

    else

        cout<<s<<endl;

}

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

#include <string.h>

int main(){

	int k;

	scanf("%d",&k);

	char s[100];

	scanf("%s",&s);

	int lol = strlen(s);

	s[k] = '\0';

	for(int i=0;s[i]!='\0';i++){

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

	}

	if(k<lol){

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

#include<cstdio>

#include<cstdlib>

#include<cctype>

#include<cmath>

#include<queue>

#include<cstring>

#include<map>

#include<stack>

#include<set>

#include<vector>

#include<algorithm>

#include<string>

typedef long long ll;

typedef unsigned long long LL;

using namespace std;

int n,h;

int main()

{

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

	int ans=0;

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

	{

		int x;

		scanf("%d",&x);

		if(x>=h)

			ans++;

	}

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

	//system("pause");

	return 0;

}


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



using namespace std;

int n,k;

int ar[100005];

int dp[100005];



int main() {

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

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

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    memset(dp,-1,sizeof dp);

    cin>>n>>k;

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

        cin>>ar[i];

    }

    dp[0]=0;

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

        int tmp=1e9;

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

            if(ind-ik<0){

                break;

            }

            else{

                tmp=min(tmp,dp[ind-ik]+abs(ar[ind-ik]-ar[ind]));

            }

        }

        dp[ind]=tmp;

    }

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

    return 0;

}

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



using namespace std;

#define ll long long



const int INF = 1e9 + 5;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int n,k;

    cin >> n>>k;

    int ar[n];

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

        cin >> ar[i];

    }

    vector<int> dp(n, INF);

    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(dp[j], dp[i] + abs(ar[i] - ar[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 int ll;

ll MOD = 1000000007;

ll INFL = 1ll << 60;

ll INF = 1 << 30;



int main() {

  ll n, k;

  cin >> n >> k;



  ll v[110000] = {};

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

    cin >> v[i];



  ll dp[110000] = {};

  for (int i = 1; i < n; i++) dp[i] = INFL;



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

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

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

    }

  }



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

}


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

using namespace std;



typedef long long int ll;

ll MOD = 1000000007;

ll INFL = 1ll << 60;

ll INF = 1 << 28;



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

int main() {

  // 入力

  int n, k;

  cin >> n >> k;

  vector<int> v(n);

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

    cin >> v[i];

  }



  vector<ll> dp(n);

  for (int i = 0; i < n; i++) dp[i] = INFL;

  dp[0] = 0;

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

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

      if (i - j < 0) break;

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

    }

  }



  cout << dp[n - 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):
#include <bits/stdc++.h>



#define ll int64

#define pb push_back

#define ff first

#define ss second

#define sz size()

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

#define allr(a) a.rbegin(), a.rend()

#define mod 1000000007

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

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

     

using namespace std;



const int maxn=110000;

int n,h[maxn],f[maxn],k;

int main(){

    IO;    

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

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

	memset(f,0x3f,sizeof(f)),f[1]=0;

	for(int i=2;i<=n;i++) for(int j=1;j<i&&j<=k;j++) f[i]=min(f[i],f[i-j]+abs(h[i]-h[i-j]));

	printf("%d",f[n]);

    return 0;

}

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

using namespace std;

#define ll long long

#define pb push_back

#define F first

#define S second

const int mod = 1e9+7, A = 1e5+5;

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

ll table1()

{

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

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

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

	return dp[n-1];

}

ll table2()

{

	dp[0] = 0;

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

	{

		dp[i] = 1e9;

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

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

	}

	return dp[n-1];

}

int main()

{

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

	cin >> n;

	cin >> k;

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

	//cout << table1();

	cout << table2();

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

#include<map>

#include<algorithm>

#include <iostream>

using namespace std;

typedef long long LL;

const  LL MAXN = 1e9+7;

int main(){

	int n;

	LL ans = 1;

	scanf("%d",&n);

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

		ans *= i;

		ans %= MAXN;

	}

	printf("%lld",ans);

	return 0;

}

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

#include <algorithm>

using namespace std;



int main()

{

	int n,i;

	long long sum=1;

	scanf("%d",&n);

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

	{

		sum*=i;

		sum%=(1000000000+7);

	}

	printf("%lld\n",sum);

	return 0;

}


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

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

using namespace std;

using ll = long long;

using P = pair<ll, ll>;

const string ln = "\n";

constexpr int INF = 1001001001;

constexpr int MOD = 1000000007;



int main() {

  cin.tie(nullptr);

  ios::sync_with_stdio(false);



  int n;

  cin >> n;



  ll ans = 1;



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

    ans *= i % MOD;

    ans %= MOD;

  }



  cout << ans << ln;



  return 0;

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

using namespace std;



int main()

{

    int n, k;

    cin >> n >> k;

    int arr[n+k+1];

    int dp[n+k+1];

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

        dp[i] = 0;



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

    {

        cin >> arr[i];

    }

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

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

    {

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

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

        {

            int x = i-j;

            if(x>=k+1)

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

        }

    }

    cout << dp[k+n] << 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):
#include<bits/stdc++.h>

#define pb emplace_back

#define ll long long

#define mp make_pair

#define PI acos(-1)

using namespace std;

ll dp[200005],a[200005],n;

ll solve(ll idx,ll k)

{

    if(idx>=n-1)

        return 0;

    if(dp[idx]!=-1)

        return dp[idx];

    ll res=INT_MAX;

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

    {

        if(idx+i<n)

            res=min(res,abs(a[idx+i]-a[idx])+solve(idx+i,k));

    }

    return dp[idx]=res;

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    ll i,j,k,x,y,z,m,t,l,r;

    cin>>n>>k;

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

        cin>>a[i];

    memset(dp,-1,sizeof dp);

    cout<<solve(0,k)<<'\n';

}
















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

using namespace std;



vector<int> dp(100005);



int main(){

    

    int n,k; 

    cin>>n>>k;

    

    vector<int> v (n);

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

        cin>>v[i];

    }

    

    dp[0] = 0;

    

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

        int x = INT_MAX;

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

            

            if(j<=i){

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

            x = dp[i];

            }

        }

        

    }

    cout<<dp[n-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):
#include <cstdio>

#include <iostream>

#include <cmath>

#include <cstring>

#include <algorithm>

#include <queue>

using namespace std;

int a,b;

int s[2000001] ;

int main()

{

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

    	s[i]=-1000000+i;

	cin>>a>>b;

	for(int i=1000000+b-a+1;i<=1000000+b+a-1;i++)

		cout<<s[i]<<" ";

}

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

using namespace std;

int main(){

    int k , x;

    cin >> k >> x;

    x -= k;

    for (int i = 0 ; i < 2*k - 1 ;i++){

        x++;

        cout << x << " ";

    }

    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;



typedef long long ll;



const int N = 1 * 1e5 + 10;

const ll inf = 1e18;



ll res;

ll a[N];

ll dp[N];



ll go(ll idx, ll k, ll n){

	if(idx == n)	return 0;

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

	ll mn = inf;

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

		mn = min(abs(a[idx] - a[i]) + go(i, k, n), mn);

	}

	return dp[idx] = mn;

}



void solve() {

	ll n, k;

	cin >> n >> k;

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

		cin >> a[i];

	}

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

	cout << go(1, k, n);

}



int main(int argc, char const *argv[]){

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

  ll t = 1;

  //cin >> t;

  while(t--){

    solve();

  }

    return 0;

}

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

using namespace std;



typedef long long ll;



const int N = 1 * 1e5 + 10;

const ll inf = 1 * 1e9 + 10;



ll res;

ll a[N];



void solve() {

	ll  n, k;

	cin >> n >> k;

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

		cin >> a[i];

	}

	std::vector<ll> dp(n, inf);

	dp[0] = 0;

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

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

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

		}

	}

	cout << dp[n - 1];



}



int main(int argc, char const *argv[]){

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

  ll t = 1;

  //cin >> t;

  while(t--){

    solve();

  }

    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;



#define REP(i,a,b) for(ll i = a; i < b; i++)

#define DB( x ) cout << #x << " = " << x << endl;

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

#define endl '\n'



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> pii;

typedef pair<int, pii> tri;

typedef vector <vector <ll>> matrix;

typedef pair<string, string> pss;



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

const int MAX = (int)1e5 + 100;



const ll oo = 0x3f3f3f3f3f3f3f3f;



ll dp[MAX], a[MAX];



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);



    memset(dp, oo, sizeof(dp));



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



    dp[1] = 0;



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

        cin >> a[i];

    }



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

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

            if(i - j >= 1)

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

        }

    }



    cout << dp[n] << endl;

}


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





}