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



#define F first

#define S second

#define PB push_back



using namespace std;



typedef long long ll;



int n, k;

ll h[100005];

ll memo[100005];



ll dp(int i){

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

	if(i >= n)	return 10000000000;

	if(memo[i] != -1){

		return memo[i];

	}

	

	ll mmin = 1e10;



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

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

	}



	return memo[i] = mmin;



}



int main(){



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

	

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);



	cin >> n >> k;

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

		cin >> h[i];

	}



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



	return 0;

}

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



#define F first

#define S second

#define PB push_back



using namespace std;



typedef long long ll;



int main(){

	

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);



	int n, k;

	cin >> n >> k;



	int h[n];

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

		cin >> h[i];

	}



	int cost[n];

	cost[0] = 0;



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

		cost[i] = INT_MAX;

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

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

		}

	}



	cout << cost[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<iostream>

using namespace std;



int N;

int mod=1e9+7;

int main(){



    while(cin>>N){

            long long ans=1;

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

        ans*=i;

        if(ans>mod){

            ans%=mod;

        }

    }

    cout<<ans<<endl;

    }

    return 0;

}


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

int main ()

{

    int i,n;

    long long s;

    scanf("%d", &n);

    s = 1;

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

    {

        s *= i;

        s %= (int)1e9+7;

    }

    printf ("%d", s);

}


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

typedef long long ll;

using namespace std;

ll n;

ll k;

ll h[100005];

ll dp[100005];

ll solve(int idx){

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

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

	ll val = INT_MAX;

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

		if(idx + i<n){

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

		}

	}

	dp[idx] = val;

	return val;

}

int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

	for(int i = 0; i < 100005; i++) dp[i] = -1;

	cin >> n >> k;

	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>

typedef long long ll;

#define mod 1000000007

#define pb push_back

#define mp(a,b) make_pair(a,b)

#define pii pair<int,int>

#define pll pair<ll,ll>

#define vll vector <ll>

#define vii vector <int>

#define vpii vector <pii>

#define vpll vector <pll>

#define ff first

#define ss second

#define matrix vector <vll>

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

#define PQ priority_queue

using namespace std;

ll pow(ll a, ll b){

	ll res = 1;

	while(b > 0){

		if(b&1) res*=a;

		b/=2;

		a*=a;

	}

	return res;

}

int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

	ll n, k;

	cin >> n >> k;

	ll h[n], dp[n];

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

		cin >> h[i];

		dp[i] = INT_MAX;

	}

	dp[0] = 0;

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

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

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

		}

	}

	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>

#define f(i,s,v) for(int i=(int)s;i<(int)v;++i)

#define rf(i,s,v) for(int i=(int)s-1;i>(int)v;--i)

#define pb push_back

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

#define memset(c, value) memset(c, (int)value, sizeof(c))

using namespace std;

typedef long long ll;

const int oo = 1e9;

const ll MOD =1e9+7;

void fast(){

    ios_base :: sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

}

int n, k;

int h[100005];

bool valid (int r,int c){

    return (r < n && r >= 0 && c >= 0 && c < n);

}

int row[] = {0 , 1 , 0, -1};

int column[] = {1 , 0, -1, 0};

int vis[100002];

int dp(int i){

    if(i == n-1)

        return 0;

    int &ret = vis[i];

    if(ret != -1)

        return ret;



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

    f(j, 2, k){

        if( i+j < n)

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

        else

            break;

    }

    return ret = path1;

}



int main()

{

    fast();

    memset(vis, -1);

    cin>>n>>k;

    ++k;

    f(i,0,n)

        cin>>h[i];

    cout<<dp(0)<<'\n';

    return 0;

}


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

#define IO ios::sync_with_stdio(0), cin.tie(0)

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 5;

const int inf = ~0u >> 1;

int d[maxn], h[maxn];

int main() {

    IO;

    int n, k;

    cin >> n >> k;

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

        cin >> h[i];

    }

    d[1] = 0;

    d[2] = abs(h[2] - h[1]);

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

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

        //cout << "i = " << i << " j = " << 1 << " d[i] = "  << d[i] <<endl;

        for (int j = 2; j <= min(k, i - 1); ++j) {

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

            //cout << "i = " << i << " j = " << j << " d[i] = "  << d[i] <<endl;

        }

    }

    cout << d[n] << 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<cmath>

using namespace std;

int main()

{

    int n;

    scanf("%d",&n);

    char s[1000000]={};

    scanf("%s",s);

    int k=strlen(s);

    if(k<=n)

       cout<<s<<endl;

    else if(k>n){

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

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

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

    }



    return 0;

}

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

#include<cmath>

using namespace std;

int main()

{

    int n;

    scanf("%d",&n);

    char s[100000]={};

    scanf("%s",s);

    int k=strlen(s);

    if(k<=n)

        cout<<s<<endl;

    else if(k>n){

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

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

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

    }}

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

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const double PI = acos(-1);



int main()

{

	int k;

	string s;

	cin >> k >> s;

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

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

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

using namespace std;

int main()

{

    int k,x;

    cin>>k>>x;

    int m = x-k+1;

    if(k==1)

    {

        cout<<x<<endl;

    }

    else

    {



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

        {

            cout<<m<<" ";

             m++;



        }



    }

    return 0;









}


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

long long a[55],n,i,x,ans;//

main(){

for(std::cin>>n;i<n;std::cin>>a[++i]);

for(;std::sort(a+1,a+n+1),a[n]>=n;a[n]%=n)for(ans+=x=a[n]/n,i=1;i<n;a[i++]+=x); 

std::cout<<ans<<'\n';

}

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

    数据量很小,直接模拟

    对最大的数-n,其他数+1,直到最大数都小于n

    问操作次数.

    数据量大时,二分也应该可以.

*/

#include<cstdio>

using namespace std;

typedef long long LL;

LL a[55];

int main(){

    LL n, ans = 0;

    scanf("%lld", &n);

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

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

    }

    while(true){

        LL ma = a[0], pos = 0;

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

            if(a[i]>ma){

                ma = a[i];

                pos = i;

            }

        }

        if(ma<n){

            break;

        }

        LL tmp = ma / n;

        a[pos] %= n;

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

            a[i] += tmp;

        }

        a[pos] -= tmp;

        ans += tmp;

    }

    printf("%lld", ans);

    return 0;

}

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

//#include <iomanip>

//#include <string>

#include <vector>

//#include <algorithm>

//#include <utility>

//#include <set>

//#include <map>

//#include <queue>

//#include <deque>

//#include <bitset>

//#include <math.h>

using namespace std ;

using ll = long long ;

//using ld = long double ;

using vll = vector<ll> ;

//using vvll = vector<vll> ;

//using vc = vector<char> ;

//using vvc = vector<vc> ;

//using vb = vector<bool> ;

//using vvb = vector<vb> ;

//using pll = pair<ll,ll> ;\



//ll mod = 1000000007 ;

//ll INF = 10000000000 ;

//long double pie = acos(-1) ;



//void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}

//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}

//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}

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

//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}

//void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}

//void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}





int main(){

	ll n ; cin >> n ;

	vll a(n) ;

	for(auto &i:a) cin >> i ;

	bool ok = true ;

	ll cnt = 0 ;

	while(ok){

		ok = false ;

		ll sum = 0 ;

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

			if(a.at(i)>=n){

				sum += a.at(i)/n ;

			}

		}

		ok = (sum!=0) ;

		if(ok){

			cnt += sum ;

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

				a.at(i) += (sum-a.at(i)/n)-a.at(i)/n*n ;

			}

		}

	}

	cout << cnt << endl ;

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

using namespace std;

#define endl '\n'

#define pb push_back

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

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

#define reps(i, m, n) for (int i = m; i <= n; ++i)

typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

const int INF = 0x3f3f3f3f;

const ll LINF = 0x3f3f3f3f3f3f3f3fLL;



int dp[100010];

int h[100010];



signed main()

{

  ios::sync_with_stdio(false);

  cin.tie(0);

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

  dp[1] = 0;

  int n, k;

  cin >> n >> k;

  reps(i, 1, n) cin >> h[i];

  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]));

    }

  }

  cout << dp[n] << endl;

  return 0;

}

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



using namespace std;



#ifndef DEBUG

#define dump(...)

#endif



template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }

template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }



constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);



    size_t N, K;

    cin >> N >> K;



    vector<int64_t> h(N);

    for (auto &hi: h) {

        cin >> hi;

    }



    vector<int64_t> dp(N, LINF);

    dp[0] = 0;

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

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

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

        }

    }



    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>

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



const int mod = 1000000007;

class mint {

  long long x;



 public:

  mint(long long x = 0) : x((x % mod + mod) % mod) {}

  mint operator-() const { return mint(-x); }

  mint& operator+=(const mint& a) {

    if ((x += a.x) >= mod) x -= mod;

    return *this;

  }

  mint& operator-=(const mint& a) {

    if ((x += mod - a.x) >= mod) x -= mod;

    return *this;

  }

  mint& operator*=(const mint& a) {

    (x *= a.x) %= mod;

    return *this;

  }

  mint operator+(const mint& a) const {

    mint res(*this);

    return res += a;

  }

  mint operator-(const mint& a) const {

    mint res(*this);

    return res -= a;

  }

  mint operator*(const mint& a) const {

    mint res(*this);

    return res *= a;

  }

  mint pow(ll t) const {

    if (!t) return 1;

    mint a = pow(t >> 1);

    a *= a;

    if (t & 1) a *= *this;

    return a;

  }

  // for prime mod

  mint inv() const { return pow(mod - 2); }

  mint& operator/=(const mint& a) { return (*this) *= a.inv(); }

  mint operator/(const mint& a) const {

    mint res(*this);

    return res /= a;

  }



  friend ostream& operator<<(ostream& os, const mint& m) {

    os << m.x;

    return os;

  }

};



struct combination {

  vector<mint> fact, ifact;

  combination(int n) : fact(n + 1), ifact(n + 1) {

    assert(n < mod);

    fact[0] = 1;

    for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;

    ifact[n] = fact[n].inv();

    for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i;

  }

  mint operator()(int n, int k) {

    if (k < 0 || k > n) return 0;

    return fact[n] * ifact[k] * ifact[n - k];

  }

} comb(100005);



int main() {

  cin.tie(nullptr);

  ios::sync_with_stdio(false);



  int n;

  cin >> n;



  mint ans(1);



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

    ans *= mint(i);

  }



  cout << ans << ln;



  return 0;

}

// Optimized solution to the same problem (faster):
#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;

}

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



using namespace std;



int main()

{

    int n=0;

    int h=0;

    cin>>n>>h;

    int arr[n];

    int x=0;

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

    {

        cin>>arr[i];

    }

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

    {

        if(arr[i2]>=h)

        {

            x++;

        }

    }

    cout<<x;

    return 0;

}


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



int main ()

{

	long long int N,K,c=0,i,h[100000]; 

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

	

	

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

	{

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

		if(h[i]>=K)

		{

			c++;

		}

	}

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

	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;

const int SIZE = 100005;

int h[SIZE];

int mem[SIZE];

int K;



int dp(int i) {



    if (i == N - 1) return 0;



    if (i == N - 1) return 0;



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



    int ret = INT_MAX;



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

      if (i + j < N)

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

    }

    return  mem[i] = ret;

}



int main() {



    cin >> N;

    cin >> K;



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

        cin >> h[i];

    }



    memset(mem, -1, sizeof mem);



    cout << dp(0);

}

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

using namespace std;

 

#define MOD (1000000007)

#define f first

#define s second

#define pb(x) push_back(x)

#define mp(x,y) make_pair(x,y)

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

#define print(vec,l,r) for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl;

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

#define forr(i,a,b) for(int i = (a); i > (b); i--)

#define input(vec,N) for(int i = 0; i < (N); i++) cin >> vec[i];

typedef long long int ll;

#define ld long double

const int N = 2e5+30;

//https://codeforces.com/problemset/page/2?tags=1700-1700

 

 

void solve(){

   int n,k;

   cin>>n>>k;

   int a[n+3];

   int dp[n+3];

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

     dp[i] = 1e9;

   }

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

     cin>>a[i];

   }

   dp[1]  = 0;

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

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

      if(i-j>=1){

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

      }

     }

   }

   cout<<dp[n]<<'\n';

}

 

 

int main(){

 	ios_base::sync_with_stdio(false);

  cin.tie(NULL);

	int T;

  //cin >> T;

  //cout<<prs.size()<<'\n';

	 T = 1;

  //visited[1]=1;

	while(T--){

		solve();

	}

}

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

#include <bits/stdc++.h>



using namespace std;

int dp[100000],n,arr[100000],k;

int bt(int i)

{

    if(dp[i]!=-1)

        return dp[i];

    if(i>=n)

        return 99999999;

    if(i==n-1)

        return 0;

    int &ret=dp[i];

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

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

    {

        ret=min(bt(i+s)+abs(arr[i]-arr[i+s]),ret);

    }

    return ret;

}

int main()

{

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

    cin>>n>>k;

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

    {

        cin>>arr[i];

    }

    cout<<bt(0);

}


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

using namespace std;

int n,k,dp[100009],a[100009],mx=1e9+8;

int main()

{

    cin>>n>>k;

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

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

    dp[0]=0;

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

    {

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

        {

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

        }

    }

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





}