stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする

#include <math.h> //数学関数と数学定数を利用する

using namespace std;

typedef long long int ll;



void chmin(ll &a, ll b){

  if (a > b) a = b;

}

ll INF = 1LL << 60;

ll dp[100010]; //DPテーブル

int h[100010];

int K;



ll rec(int N){

  

  ll res = INF;

  //ベースケース

  if (N == 0){

    return 0;

  }



  //計算済みであればその値を返す

  if (dp[N] < INF){

    return dp[N];

  }



  //各ジャンプ毎にmin値を計算する（再帰）

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

    if(N-i >= 0) chmin(res, rec(N-i) + abs(h[N] - h[N-i]));

  }



  return dp[N] = res; //DPテーブルに記録しながらリターン

}





int main(){

  int N; cin >> N >> K;

  

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

    cin >> h[i];

  }

  

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

    dp[i] = INF;

  }  

  

  cout << rec(N-1);

  return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする

#include <math.h> //数学関数と数学定数を利用する

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

#define PI 3.141592653589793

using namespace std;

typedef long long ll;

template<class T> void chmax(T &a,T b) { if (a<b) a=b;}

template<class T> void chmin(T &a,T b) { if (a>b) a=b;}

int gcd(int a, int b){//ユークリッドの互除法

  if (a < b) gcd(b,a); //aの方がbよりでかいのが前提

  if (b == 0) return a; //aをbで割り切れたらreturn

  else gcd(b, a % b);

}



const int MOD = 1000000007;

const int INF = 1 << 30;

int dp[100005],h[100005];



int main(){

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

  rep(i,n){

    cin >> h[i];

  }

  

  rep(i,100005) dp[i] = INF;

  

  dp[0] = 0;

  

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

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

      if(i+j < n){

        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>

#include <cstring>

#include <set>

#include <stack>

#include <list>

#include <vector>

#include <map>

#include <math.h>

#include <algorithm>



typedef long long ll;

using namespace std;



int main(){

    ll N, K;

    cin >> N >> K;

    ll a[N+1];

    ll dp[N+1];



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

        cin >> a[i];

    }



    dp[0]=0;

    dp[1]=0;

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

        dp[i] = 0x3f3f3f3f;

    }



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

        }

    }



    cout << dp[N];

    return 0;

}

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

#include <cstring>



using namespace std;



const int maxn = 1e5+5;

int n,K;

int h[maxn],f[maxn];



int abs(int x,int y) {

	if (x - y < 0) return y - x;

	return x - y;

}



int min(int x,int y) {

	if (x < y) return x;

	return y;

}



int main() {

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

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

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

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

	}

	f[0] = f[1] = 0;

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

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

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

		}

	}

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

using namespace std;

typedef long long int ll;

const int INF = 1000000000;

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

#define rep(i,n) REP(i, 0, n)

int dp[1001][1001];

int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    string a, b;

    cin >> a >> b;

    int n = a.size(), m = b.size();

    memset(dp, 0, sizeof(dp));

    rep(i, m + 1) dp[0][i] = i;

    rep(i, n + 1) dp[i][0] = i;

    REP(i, 1, n + 1) REP(j, 1, m + 1){

        dp[i][j] = dp[i - 1][j - 1];

        if(a[i - 1] != b[j- 1]) dp[i][j] += 1;

        int temp = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);

        dp[i][j] = min(temp, dp[i][j]);

    }

    cout << dp[n][m] << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
class _in{struct my_iterator{int it;const bool rev;explicit constexpr my_iterator(int it_, bool rev=false):it(it_),rev(rev){}constexpr int operator*(){return it;}constexpr bool operator!=(my_iterator& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const my_iterator i,n;public:explicit constexpr _in(int n):i(0),n(n){}explicit constexpr _in(int i,int n):i(i,n<i),n(n){}constexpr const my_iterator& begin(){return i;}constexpr const my_iterator& end(){return n;}};



#include <bits/stdc++.h>

using namespace std;



int main() {

    string s1, s2;

    cin >> s1 >> s2;

    int N = s1.size(), M = s2.size();

    vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));

    for(int i : _in(N + 1)) dp[i][0] = i;

    for(int j : _in(M + 1)) dp[0][j] = j;

    for(int i : _in(N)) for(int j : _in(M)) {

        dp[i + 1][j + 1] = dp[i][j];

        if(s1[i] != s2[j]) ++dp[i + 1][j + 1];

        int temp = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1);

        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], temp);

    }

    cout << dp[N][M] << endl;

}

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

using namespace std;

#define int long long

int editDistance(string x,string y){

  int n=x.length(),m=y.length();

  int INF=1LL<<55LL;

  vector<vector<int> > dp(n+1,vector<int>(m+1,INF));

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

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

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

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

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

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

      dp[i][j]=min(dp[i][j],dp[i-1][j-1]+(x[i-1]!=y[j-1]));

    }

  }

  return dp[n][m];

}

signed main(){

  string s1,s2;

  cin>>s1>>s2;

  cout<<editDistance(s1,s2)<<endl;

  return 0;

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

#include <cmath>



using namespace std;

int k,j,i,n,h[100005];

long long dp[100005];

int main()

{

    cin>>n>>k;

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

        cin>>h[i];

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

        dp[i]=1e17;

    dp[1]=0;

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

    {

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

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

    }

    cout<<dp[n];

    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;

int h[100001];

int dp[100001];

int n,k;

int solve(int stone)

{

    if(stone==n) return 0;

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

    int cost=1<<30;

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

    cost=min( cost,abs(h[stone]-h[stone+i])+solve(stone+i) );



    return dp[stone]=cost;

}



int main()

{

    int i;

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



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

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



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

    printf("%d\n",solve(1));





    return 0;

}




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

     **author : _KMS_

     **created : 28-03-2020

**/

#include<bits/stdc++.h>

using namespace std;

const long long M =2e5+10,MOD=998244353;

typedef long long ll;

int arr[M];

int dp[M];

int main() {

   

  ios::sync_with_stdio(false);

  cin.tie(0);

  int n,k;

  cin>>n>>k;

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

  cin>>arr[i];

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

for(int i=2;i<=n;++i)dp[i]=INT_MAX;

 arr[0]=arr[1];

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

  {  int temp=k;

     int in=i-1;

     while(temp>0&&in>0){

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

    temp--;

    in--;

  }

  }

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

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

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

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

#define ceil(n, m) ((n / m) + ( n % m ? 1 : 0))

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

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

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

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



int main(){

    //fast_IO;



    string s;

    cin >> s;

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

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

            s[i]=' ';

            bool f = 1;

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

                    if(s[j]!=' ')

                        s[j] = ' ',f=0;

            }

        }

    }

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

        if(s[i]!=' ') cout<<s[i];

    return 0;

}


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

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

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

    {

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

        {

            A--;

            if(A<0)

                A=0;

        }

        else

            H[A++]=G[i];

    }

    H[A]='\0';

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

    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;



typedef long long ll;



#define INF 1e18

#define MAX_N 100005



ll N, K, h[MAX_N], memo[MAX_N];



ll dp(int p) {

  if (p < 1) {

  	return INF;

  }

  if (p == 1) {

  	return 0;

  }

  if (p <= K + 1) {

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

  }

  ll opt = INF;

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

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

  }

  return memo[p] = opt;

}



int main() {

  scanf("%lli %lli", &N, &K);

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

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

  }

  // printf("%lli\n", dp(N));

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

  	memo[i] = abs(h[i] - h[1]);

  }

  for (int i = 2 + K; i <= N; i++) {

  	ll opt = INF;

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

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

	}

	memo[i] = opt;

  }

  printf("%lli\n", memo[N]);

}

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

using namespace std;



typedef signed long long ll;

const int MX = 1e5 + 5;

const int INF = 1e9 + 5;

int n, k, A[MX], dp[MX];



int solve(int pos) {

    if(pos >= n) return INF;

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

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

    int ans = INF;

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

        if(pos + i < n) {

            ans = min(ans, abs(A[pos] - A[pos + i]) + solve(pos + i));

        }

    }



    return dp[pos] = ans;

}



int main() {

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



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

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

    }

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

    printf("%d\n", solve(0));

    return 0;

}

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





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

using namespace std;



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

#define endl         "\n"

#define int          long long

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

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

#define pii          pair<int,int>

#define fi           first

#define se           second

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

#define deb(x)       cout << #x << ' ' << x << endl

#define clock        cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;



void INPUT() {

	#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

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

    #endif

}



const int N = 1e5 + 5;



int a[N], n, k;

int cache[N];

 

int dp(int idx) {

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

	int ans = (idx == n - 1 ? 0 : LLONG_MAX);

	forn(i, 1, k + 1) {

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

	}

	cache[idx] = ans;

	return cache[idx];

}



int32_t main(){

    IOS //INPUT();

    cin >> n >> k;

    forn(i, 0, n) cin >> a[i];

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

    int ans = dp(0);

    cout << ans << endl;

    return 0;

}

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



#define int         long long

#define pb          push_back

#define pii         pair<int,int>

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

#define x           first

#define y           second

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

#define endl        '\n'

#define hell        1000000007

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

using namespace std;

int dp[100005];

void solve(){

	int n,k;

	cin>>n>>k;

	int a[n];

	rep(i,0,n) cin>>a[i];

	rep(i,0,n) dp[i]=hell;

	dp[0]=0;

	rep(i,1,n) {

		rep(j,1,k+1) {

			dp[i]=min(dp[i],(i-j>=0)?dp[i-j]+abs(a[i-j]-a[i]):hell);

		}

	}

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

}



signed main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

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





}