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

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <queue>

#include <stack>

#include <map>

#include <set>

#include <vector>

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

#define repd(i,x,n) for(int i=x;i<=n;i++)

#define pii pair<int,int>

#define pll pair<long long ,long long>

#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

#define MS0(X) memset((X), 0, sizeof((X)))

#define MSC0(X) memset((X), '\0', sizeof((X)))

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define gg(x) getInt(&x)

using namespace std;

typedef long long ll;

inline void getInt(int* p);

const int maxn=1000010;

const int inf=0x3f3f3f3f;

/*** TEMPLATE CODE * * STARTS HERE ***/

ll n;

ll dp[maxn];

ll a[maxn];

ll k;

int main()

{

    gbtb;

    cin>>n>>k;

    repd(i,1,n)

    {

        cin>>a[i];

    }

    dp[1]=0;

    dp[0]=0;

    repd(i,2,n)

    {

        dp[i]=inf;

    }

    repd(i,2,n)

    {

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

        {

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

        }

    }

    cout<<dp[n];

    return 0;

}



inline void getInt(int* p) {

    char ch;

    do {

        ch = getchar();

    } while (ch == ' ' || ch == '\n');

    if (ch == '-') {

        *p = -(getchar() - '0');

        while ((ch = getchar()) >= '0' && ch <= '9') {

            *p = *p * 10 - ch + '0';

        }

    }

    else {

        *p = ch - '0';

        while ((ch = getchar()) >= '0' && ch <= '9') {

            *p = *p * 10 + ch - '0';

        }

    }

}

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

#include<cmath>

#include<algorithm>



using namespace std;



int A[100001];

int B[100001];



int func(int b, int e, int v)

{

	int m = abs(v - A[b]) + B[b];

	for(int i=b+1;i<e;++i)

	{

		m = min(m, abs(v - A[i]) + B[i]);

	}

	return m;

}



int main()

{

	int n, k;

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

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

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

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

		B[i] = func(0, i, A[i]);

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

		B[i] = func(i - k, i, A[i]);

		printf("%d\n", B[n - 1]);

	fflush(stdout);

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

ll dp[100009];

ll n,x,result,ans;

vector<ll>v;

ll frog(ll pos,ll k)

{

   if (pos >= 0 && pos <n-1)

    {

        if (dp[pos]!= -1)

            return dp[pos];

      ll  int ret = INT_MAX;

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

        {

            if(pos+i==n)

                break;

            ans = frog(i + pos,k ) + abs(v[pos]-v[pos+i]);

           // cout<<ans<<endl;

                ret = min(ret,ans );

        }

        return dp[pos] = ret;

    }

    else if(pos==n-1)

        return 0;

    else

        return INT_MAX;

}



int main()

{

    ll k;

    cin>>n>>k;

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

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

    {

        cin>>x;

        v.push_back(x);

    }

    cout<<frog(0,k)<<endl;

}

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

using namespace std;

#define ll long long

#define ull unsigned long long

#define ui unsigned int

#define us unsigned short

#define endl "\n"

const long long mod=1e9+7;const double pi=acos(-1);const long long inf=1e18;

int main(){	ios_base::sync_with_stdio(0); cin.tie(); cout.tie();	ll n,k,h[100100],dp[100100];	cin>>n>>k;	for(int i=1;i<=n;i++){		cin>>h[i];	}	dp[0]=dp[1]=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(1ll,i-k);j--){			dp[i]=min(dp[i],dp[j]+abs(h[i]-h[j]));		}}cout<<dp[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<iostream>

#include<string>

using namespace std;

char a[1001];

int main()

{

    string s;

    int k=0;

    cin>>s;

    for(int i=0;i<=s.size()-1;++i)

    {

    	if(s[i]=='0'||s[i]=='1')

    	{

    		k++;

    		a[k]=s[i];

		}

		else

		{

			if(k>0)

			k--;

		}

	}

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

	cout<<a[i];

    return 0;

}

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

#include<cstring>

using namespace std;

char s[20], S[20];

int main()

{

	scanf("%s", s);

	int len = strlen(s);

	int cnt = 0;

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

	{

		if(s[i] == '1')

		{

			S[cnt] = '1';

			cnt++;

		}

		if(s[i] == '0')

		{

			S[cnt] = '0';

			cnt++;

		}

		if(s[i] == 'B')

		{

			if(cnt)cnt--;

			S[cnt] = '\0';

		}

	}

	printf("%s\n", S);

	return 0;

}


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

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

#define pa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};

#define pb push_back



using ll = long long;

using namespace std;

using pint = pair<int,int>;

using pll = pair<ll,ll>;

const int INFint = 1e9;

const ll INFll = 1e18;

ll MOD=1e9+7;



ll gcd(ll a, ll b) { return __gcd(a,b); } //最大公約数

ll lcm(ll a, ll b) { return a/gcd(a,b)*b; } //最大公倍数



// aのn乗をMODで割りながら計算する

ll modpow(ll a, ll n) {

  long long res = 1;

  while (n > 0) {

    if (n & 1) res = res * a % MOD;

    a = a * a % MOD;

    n >>= 1; // right shift

  }

  return res;

}



// MODを法としたaの逆元を計算する

ll modinv(ll a) {

  return modpow(a, MOD - 2);

}



int main(){

  string s;

  cin>>s;

  int N = int(s.size());

  string ans;

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

    //cout << ans << endl;

    if (s[i] == 'B'){

      if (int(ans.size()) != 0){

        ans = ans.substr(0,int(ans.size())-1);

      }

    }else{

      ans += s[i];

    }

  }

  cout << ans << endl;

  return 0;

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

using namespace std;



const int N = 100009;

int f[N], dp[N];





int main(){



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

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

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

        }



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



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

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

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

                        if(i + j > n)break;

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

                }

        }



        cout << dp[n] << endl;









        return 0;

}




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

using namespace std;



int n,h[100005],c[100005],k;



int main()

{

    cin>>n>>k;

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

        cin>>h[i];

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

        c[i] = abs(h[0] - h[i]);



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

    {

        long long m = 1e10;

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

        {

            if ((c[i-j] + abs(h[i] - h[i-j])) <= m)

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

            c[i] = m;

        }

    }

    cout<<c[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<cmath>

#include<string>

#include<cstring>

#include<algorithm>

#include<iomanip>

#include<cstdio>

#include<stack>

#include<queue>

using namespace std;

typedef long long ll;

int main(){

	string s,ans;

	int k;

	cin>>k;

	cin>>s;

	ans=s;

	int len=s.length() ;

	if(len>k) {

		ans=s.substr (0,k)+"...";

	}

	cout<<ans;

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

#include <string.h>

#include <stdio.h>

#include <math.h>

#include <list>

#include <set>

#include <queue>

#include <iterator>

#include <bitset>

#include <bits/stdc++.h>

#include <vector>

#include <cctype>

#include <algorithm>

#define ll             long long

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

#define loop           for(i=0;i<n;i++)

#define pb             push_back

#define tc()           int t;cin>>t;while(t--) 

#define pi             acos(-1)         

 

using namespace std;

 

ll i,j,temp;



 

int main()

{_

    

   int n,len =0;

   char str[101];

   cin>>n>>str;

   while(str[len]!='\0')

   	len++;



   if(n>=len){

   	cout<<str<<endl;

   }

   else{

   	str[n] = '\0';

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

   }

 

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;



template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }



using ll=long long;

const int Nmax=100010;

int N,K; 

ll dp[Nmax];

int h[Nmax];

const ll INF=1LL<<60;





int main(){

  cin >> N >> K;

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

    cin >> h[i];

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

    dp[i]=INF;

  dp[0]=0;

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

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

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

    }

  }

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

  

}

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

using namespace std;

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

using ll=long long;

const ll MOD=1e9+7;

const int INF=1<<30;



template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }



int dp[100010];



int main(){

  int N,K;

  cin >> N >> K;

  vector<int> h(N);

  rep(i,N){

    cin >> h[i];

  }

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

    dp[i]=INF;

  }

  dp[0]=0;

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

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

      chmin( dp[i+j],dp[i]+abs(h[i]-h[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 <iostream>

using namespace std;



int main()

{

    int n, minHeight, friends, counter=0;

    cin >> n >> minHeight;

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

    {

        cin >> friends;

        if(friends >= minHeight)

            ++counter;

    }

    cout << counter;

    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>

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

using namespace std;

#define X real()

#define Y imag()

#define cross(a, b) ((conj(a) * (b)).Y)

#define dot(a, b) ((conj(a) * (b)).X)

#define EPS 1e-7

long long mod = 1e9 +7;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

const double pi = acos(-1);

typedef complex <double> point;

typedef tuple<int, int, int> line;

typedef vector<point> polygon;

ll n , x , h[100005] , arr[100005];

ll solve(int ind)

{

    if(ind >= n)

        return 2e9;

    if(ind == n - 1)

        return 0;

    if(arr[ind] != -1)

        return arr[ind];

    ll ans = 2e9;

    for(int i = ind + 1; i < n && i <= ind + x ; i++)

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

    arr[ind] = ans;

    return ans;

}



int main()

{

    cin >> n >> x;

    memset(arr , -1 , sizeof arr);

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

        cin >> h[i];

    cout << solve(0);



    return 0;

}




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

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

using namespace std;

#define X real()

#define Y imag()

#define cross(a, b) ((conj(a) * (b)).Y)

#define dot(a, b) ((conj(a) * (b)).X)

#define EPS 1e-7

long long mod = 1e9 + 7;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

const double pi = acos(-1);

typedef complex <double> point;

typedef tuple<int, int, int> line;

typedef vector<point> polygon;

ll n , h[100005] , memo[100005] , k;



int main()

{

    em3

    cin >> n >> k;

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

        cin >> h[i];

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

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

    {

        memo[i] = 1e10;

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

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

        ///cout << memo[i] << endl;

    }

    cout << memo[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>

#include<algorithm>

using namespace std;

int main()

{

    int i,j,n,m;

    cin>>n>>m;

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

        cout<<i;

        if(i!=m+n-1)

            cout<<" ";

    }

}


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

#include<stdio.h>

int main(){



int k,x,i,p,q,r;

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

p=(x-(k-1));

q=(x+(k-2));

r=(x+(k-1));

if(k==1)

{

    printf("%d",x);

}

else{

    for(i=p;i<=q;i++)    

    {

        printf("%d ",i);

    }

    printf("%d",r);

}



return 0;

}


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

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

using namespace std;



int main(void) {

	int k, x;

	cin >> k >> x;

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

		if (i == x+k) cout << i << endl;

		else cout << i << " ";

	}

	return 0;

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

#include <cstring>

using namespace std ;



int main()

{

	int n ;

	cin >> n ;

	string str ;

	cin >> str ;

	int len = str.length() ;

	if(n < len)

	{

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

		cout << str[i] ;

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

	}

	else if(n >= len)

	{

		cout << str << endl ;

	}



	return 0 ;

}

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



int main(){

	int k;

	char word[100];

	scanf("%d", &k);

	scanf("%s", &word);

	

	int len = 0;

	

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

		len++;

	}

	

	if(len <= k){

		printf("%s\n", word);

	}

	else{

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

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

		}

		printf("...\n");

	}

	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 n , k ;

const int N = 1e5 +  10 ;

int h[N] ;

int mem[N] ;









int jump(int i){

    if(i == 0) return 0 ;



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







    mem[i] = 2e9 ;

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

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





    return mem[i] ;

}









int main(){

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





    memset(mem , -1 , sizeof mem) ;

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

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







    printf("%d\n" , jump(n - 1)) ;

}


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



using namespace std ;



int n , k ;

const int N = 1e5 + 10;

int a[N] , cost[N] ;









int main(){

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

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

        cost[i] = 2e9 ;







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



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



        int j = i - k  ;

        if(j < 0) j = 0 ;



        for(j ; j < i ; j++){



            if(cost[j] + abs(a[j] - a[i]) < cost[i])

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



        }





        //printf("%d " , cost[i]) ;

    }









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

int dp[100005],a[100005];

int main()

{

    int i,k,n,m,mx,x,l;

    cin>>n>>k;

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

    {

        cin>>a[i];

    }

    dp[0]=0;

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

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

    {

        mx=INT_MAX;

        l=1;

        while((i-l)>=0)

        {

            x=abs(a[i]-a[i-l])+dp[i-l];

            mx=min(mx,x);

            if(l==k)

            {

                break;

            }

            l++;

        }

        dp[i]=mx;

    }

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

    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;





}