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

#include <cmath>



using namespace std;



int path(int pedra);



int n_pedras, pulo_maximo;



int altura_pedras[100001];



int custos[100001];



int main(){

    cin >> n_pedras >> pulo_maximo;



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

        cin >> altura_pedras[i];

    }



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

        custos[i] = -1;

    }



    int custo_minimo = path(1);



    cout << custo_minimo << endl;



    return 0;

}



int path(int pedra){

    if(pedra == n_pedras){

        return 0;

    }



    if(custos[pedra] == -1){

        int pulos[pulo_maximo + 1];



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

            if(pedra + i <= n_pedras){

                 pulos[i] = abs(altura_pedras[pedra] - altura_pedras[pedra + i]) + path(pedra + i);

            }

        }



        int custo_minimo = pulos[1];



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

            if(pedra + i <= n_pedras){

                if(pulos[i] < custo_minimo){

                    custo_minimo = pulos[i];

                }

            }

        }



        custos[pedra] = custo_minimo;

    }



    return custos[pedra];

}

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

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

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

int main()

{

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

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

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

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

	f[1]=0;

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

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

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

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

	return 0;

}

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

#include<cstring>

#include<vector>

#include<stack> 

#include<cstdio>

using namespace std;

string a;

int main()

{

	vector<char>q;

	while(cin>>a)

	{

		int len=a.size();

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

		{

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

				q.push_back('0');

			else if(a[i]=='1')

				q.push_back('1');

			else if(!q.empty())	

				q.pop_back();

		}

		int j=q.size();

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

		{

			cout<<q[i];

		}

		cout<<endl;

	}

	return 0;

}

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

#include<string.h>

#include<algorithm>

using namespace std;



int main(){

	int a,n=0;

	char s[15],s1[15];

	scanf("%s",s);

	for(int i=0;i<strlen(s);i++){

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

			s1[n++]='0';

		}

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

			s1[n++]='1';

		}

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

			if(n>0){

				s1[--n]='\0';

			}

		}

		

	}

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

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

		}

	

	printf("\n");

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

using namespace std;

int main()

{

    int x,y;

    cin>>x>>y;

    if(x+y<10) cout<<x+y<<endl;

    else cout <<"error"<<endl;

    return 0;

}


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



int main(){

	

	int a,b;

	

	scanf("%d %d",&a,&b);

	

	if(a+b>=10){

		printf("error\n");

	}

	

	else{

		printf("%d\n",a+b);

	}

	

	return 0;

}

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

using namespace std;

#define ll long long int

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

#define rrep(i,n) for( int i = n; i >= 0; i-- )

#define REP(i,s,t) for( int i = s; i <= t; i++ )

#define RREP(i,s,t) for( int i = s; i >= t; i-- )

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF 2000000000

#define mod 1000000007

#define INF2 1000000000000000000





int main(void)

{

    cin.tie(0);

    ios::sync_with_stdio(false);

    int A, B;

    cin >> A >> B;

    if (A + B >= 10) {

        cout << "error" << endl;

    } else {

        cout << A + B << endl;

    }



    return 0;

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

 

using namespace std;

 

#define sf(n) scanf("%d", &n)

#define pd(n) printf("%.12lf", n)

#define pf(n) printf("%d", n)

#define p() printf("\n")

#define ps() printf(" ")

#define ll long long

#define ull unsigned long long

#define MAX 1e9

const int mod = 1000 * 1000 * 1000 + 7;

#define ex 1e18

#define dbl 2e09

#define qu(q) queue<int> q

#define pqu(q) priority_queue<int> q

#define len(s) s.length()

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

#define pb(x) push_back(x)

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

#define rall(x) x.rbegin(), x.rend()

#define F first

#define S second

#define clr(x) x.clear()

#define LOG(x) std::cout << x << std::endl;

 

const int INF = 0x3f3f3f3f;

const ll LL_INF = 0x3f3f3f3f3f3f3f3f;

const double PI = 3.14159265359;

 

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

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



int a[100005];

int dp[100005];

int n,k;

int solve(int idx){

    if(idx==(n-1))

        return 0;

    if(idx>=n)

        return INF;

    int &ret = dp[idx];

    if(~ret)

        return ret;

    ret = INF;

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

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

    }

    return ret;

}







int main(){



    cin>>n>>k;

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

        cin>>a[i];

    }

    memset(dp,-1,sizeof dp);

    cout<<solve(0)<<endl;

    return 0;

}

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



using namespace std;



#define F first

#define S second



int N, K;

int H[100005];

int dp[100001];



int main() {

	cin >> N >> K; 

	H[0] = INT32_MAX; // to avoid RTE

	for (int i = 1; i <= N; i++) cin >> H[i];

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

	int ans = 0;

	

	dp[N] = 0; // the base case

	K = min(N - 1, K);

	

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

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

			if (i - j < 0) break;

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

		}

	}



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

#include <iostream>

#include <algorithm>



using namespace std;



const int INF = 1000000000;



int main()

{

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

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

  int dp[100200]; dp[0] = 0; for (int i = 1; i < 100200; ++i) dp[i] = INF;

  

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

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

  

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

  return 0;

}

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



using namespace std;



template<class T> inline void chmin(T &x, const T &y) {if (x > y) x = y;}



const int INF = 1e9+7;



int n, k;

int h[101000];



int dp[101000];



int main()

{

  cin >> n >> k;

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

  

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

  

  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;

  return 0;

}

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

using namespace std;

int a[100000];

int partitin(int n,int r)

{

	int x;

	int i;

	int t;

	x=a[r];

	i=n-1;

	for(int j=n;j<r;j++)

	{

		if(a[j]<=x)

	{	i++;

		t=a[i];

		a[i]=a[j];

		a[j]=t;

		}

	}

	t=a[i+1];a[i+1]=a[r];a[r]=t;

	return i+1;

}

int main()

{

	int n,m;

	cin>>n;

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

	{

		cin>>a[i];

	}

	m=partitin(0,n-1);

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

	{

	    if(i) cout<<" ";

		if(i==m) cout<<"[";

		cout<<a[i];

		if(i==m) cout<<"]";

    }

    cout<<endl;

    return 0;

}

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

#define MAX 100000

int A[MAX],n;

int partition(int p,int r)

{

	int x,i,j,t;

	x=A[r];

	i=p-1;

	for(j=p;j<r;j++)

	{

		if(A[j]<=x)

		{

			i++;

			t=A[j];A[j]=A[i];A[i]=t;

		}

	}

	t=A[i+1];A[i+1]=A[r];A[r]=t;

	return i+1;

}

int main()

{

	int i,q;

	scanf("%d",&n);

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

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

	q=partition(0,n-1);

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

	{

		if(i)printf(" ");

		if(i==q)printf("[");

		printf("%d",A[i]);

		if(i==q)printf("]");

	}

	printf("\n");

	return 0;

}

// Fastest known solution for this problem:
#include<stdio.h>

#include<iostream>



#define MAX 100000



int partition(int* A, int p, int r)

{

    int i, j, x;

    x = A[r];

    i = p - 1;

    for(j = p; j < r; j++){

		if(A[j] <= x){ i++; std::swap(A[i], A[j]); }

    }

	std::swap(A[i + 1], A[r]);



    return i + 1;

}



int main()

{

    int i, n, A[MAX], q;

    scanf("%d", &n);

    for(i = 0; i < n; i++) scanf("%d", &A[i]);



    q = partition(A, 0, n - 1);



    for(i = 0; i < q; i++){ printf("%d ", A[i]); }



    printf("[%d]", A[q]);



    for(i = q + 1; i < n; i++){ printf(" %d", A[i]); }



    printf("\n");



    return 0;

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

using namespace std;

int n, k;

int h[1000005], memo[1000005];

int f(int n) {

    if(n == 0) {

        return 1e9;

    }

    if(n == 1) {                        // bisa juga base case nya n = 0, f(0) harus tinggi, misal 1e9;

        return 0;

    }

    if(n == 2) {

        return abs(h[1] - h[2]);

    } 

    else if(memo[n] != -1) {

        return memo[n];

    }

    int minim = INT_MAX;

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

        if(n - i > 0) {

            minim = min(f(n - i) + abs(h[n] - h[n - i]), minim);

        }

//        printf("#%d %d\n", f(n - i), abs(h[n] - h[n - i]));

    }

    memo[n] = minim;

    return memo[n];

}



int main() {

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

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

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

    }

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

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

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

//        printf("%d %d\n", i, f(i));

//    }

}

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

#define int long long

#define double long double

#define endl "\n"

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

#define pb push_back

#define mp make_pair

#define ms(v,x) memset(v,x,sizeof(v))

#define pii pair<int,int>

#define ff first

#define ss second

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

#define rep(i,j,k) for(int i=j;i<k;i++)

#define td(v) v.begin(),v.end()

#define inf 1000000000 // 1e9

#define M   1000000007 // 1e9 + 7

using namespace std;

inline int mod(int n, int m){ int ret = n%m; if(ret < 0) ret += m; return ret; }

int gcd(int a, int b){return (b == 0 ? a : gcd(b, a%b));}

int lcm(int a,int b){return (a*b)/gcd(a,b);}

int exp(int a,int b,int m){

    if(b==0) return 1LL;

    if(b==1) return mod(a,m);

    int k = mod(exp(a,b/2,m),m);

    if(b&1){

        return mod(a*mod(k*k,m),m);

    }

    else return mod(k*k,m);

}



main(){

    fastio;

    int n,k;

    cin >> n >> k;

    vector<int> dp(n,(1LL<<62));

    vector<int> h(n);

    for(int &x : h) cin >> x;

    dp[0] = 0;

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

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

            if(i+j >= n) break;

            dp[i+j] = min(dp[i] + abs(h[i]-h[i+j]), dp[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):
/* In the name of Allah */



#include <bits/stdc++.h>

using namespace std;



// const int INF = 20000000;



#define ll long long

#define _SetBit(A, k) (A[(k / 32)] |= (1 << (k % 32)))

#define _ClearBit(A, k) (A[(k / 32)] &= ~(1 << (k % 32)))

#define _CheckBit(A, k) (A[(k / 32)] & (1 << (k % 32)))

#define SetBit(x, k) (x |= (1 << k))

#define ClearBit(x, k) (x &= ~(1 << k))

#define CheckBit(x, k) (x & (1 << k))



int vis[26];



int main()

{

  int n;

  cin >> n;

  char x[n + 1];

  ll r = 0, g = 0, b = 0;

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

  {

    cin >> x[i];

    if (x[i] == 'R')

      r++;

    else if (x[i] == 'G')

      g++;

    else

      b++;

  }



  ll cnt = 0;

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

  {

    for (int j = 0; j < (n - i + 1) / 2; j++)

    {

      if (x[i] != x[i + j] && x[i + j] != x[i + 2 * j] && x[i] != x[i + 2 * j])

        cnt++;

    }

  }



  ll res = r * g * b;

  res -= cnt;

  // cout << "**" << r << " " << g << " " << b << " " << cnt << ":\n";

  cout << res << "\n";



  return 0;

}

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

#include<algorithm>



using namespace std;



typedef long long LL;



const int N = 4010;



int r[N], g[N], b[N];

char s[N];



int main()

{

    int n;

    scanf("%d", &n);

    scanf("%s", s + 1);

    

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

    {

        r[i] = r[i - 1], g[i] = g[i - 1], b[i] = b[i - 1];

        if(s[i] == 'R')  r[i] ++;

        else if(s[i] == 'G')  g[i] ++;

        else  b[i] ++;

    }

    

    LL res = 0;

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

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

            if(s[i] != s[j])

            {

                char c = (int)'R' + 'G' + 'B' - s[i] - s[j];

                if(c == 'R')  res += r[i - 1];

                else if(c == 'G')  res += g[i - 1];

                else  res += b[i - 1];

                if(i - (j - i) >= 1 and s[i - (j - i)] == c)  res --;

            }

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

    return 0;

}

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

using namespace std;

typedef long long ll;

const int mod = 1000000007;



int main()

{

	int n;

	string s;

	cin >> n >> s;



	ll r = 0, g = 0, b = 0;

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

		if (s[i] == 'R') r++;

		else if (s[i] == 'G') g++;

		else b++;

	}



	ll all = r * g * b;

	ll sub = 0;

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

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

			int k = 2 * j - i;

			if (k < n && s[i] != s[j] && s[j] != s[k] && s[k] != s[i]) sub++;

		}

	}

	cout << all - sub << endl;

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

#include<algorithm>

#include<cmath>

#include<cstring>

using namespace std;

int n,k;

int h[100000];

int dp[100001];

int rec(int i){

  int res;

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

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

  else if(i==n-2) res=rec(i+1)+abs(h[i]-h[i+1]);

  else{

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

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

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

    }

  }

  return dp[i]=res;

}

int main(){

  cin >> n >> k;

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

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

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

  return 0;

}

  


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

#include<cstring>

#include<algorithm>

#include<cmath>

#define INF 2000000000

using namespace std;

int n,k;

int h[100000];

int dp[100001];

int main(){

  cin >> n >> k;

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

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

  dp[0]=0;

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

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

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

    }

  }

  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;

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

#define ld long double

#define ll long long

#define pb push_back

#define pi acos(-1)

#define eps 1e-7

#define F first

#define S second

#define X real()

#define Y imag()

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

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

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

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

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

typedef complex <ld> point ;

typedef vector <point> polygon ;

typedef tuple <int , int , int> line ;

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

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

ll solve(ll ind)

{

	if (ind >= n)

		return 1e10 ;

	if (ind == n-1)

		return 0 ;

	if (ans[ind] != -1)

		return ans[ind] ;

	ll val = 1e10 ;

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

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

	ans[ind] = val ;

	return val ;

}

int main()

{

	_K

	cin >> n >> k ;

	memset(ans , -1 , sizeof ans) ;

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

	cout << solve(0) ;

	return 0 ;

}

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

using namespace std;

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

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

#define BiEdge(v,a,b) (v)[(a)].pb((b)),(v)[(b)].pb((a))

#define ll long long

#define ld long double

#define pb push_back

#define Pb pop_back

#define F first

#define S second

#define X real()

#define Y imag()

#define pi acos(-1)

#define eps 1e-7

#define bpc __builtin_popcount

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

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

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

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

typedef complex <ld> point ;

typedef vector <point> polygon ;

typedef tuple <ll,ll,ll> line ;

const int mod = 1e9+7 , A = 1e6+1 ;



int main()

{

	ios

	ll n , k ;

	cin >> n >> k ;

	ll dp[n] , a[n] ;

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

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

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

	{

		ll x ;

		dp[i] = 1e18 ;

		i - k < 0 ? x = 0 : x = i - k ;

		for (int j = i - 1 ; j >= x ; j--)

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

	}

	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;





}