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

using namespace std;

using ll=long long;

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 main(){

   int N;

   cin>>N;

   vector<int> A(1000000,0),count(1000001,0);

   A.at(0)=N;

   for(int i=1;i<A.size();i++){

       count.at(A.at(i-1))++;

       if(count.at(A.at(i-1))==2){

           cout<<i<<endl;

           return 0;

       }

       else if(A.at(i-1)%2==0){

           A.at(i)=A.at(i-1)/2;

       }

       else {

           A.at(i)=3*A.at(i-1)+1;

       }

   }

}    

// Optimized solution to the same problem (faster):
#pragma GCC optimize("O3")

#include<bits/stdc++.h> 

using namespace std;

using ll=long long;

typedef pair<ll,ll> P;

#define fi first

#define se second

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

const ll inf=(1e18);

const ll mod=1000000007;

ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}

ll lcm(ll c,ll d){return c/gcd(c,d)*d;}

struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;

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

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

map<ll,int> mp;

int main(){

ll n;

cin>>n;

mp[n]++;

ll cnt=1;

ll t;

while(1){

  if(n%2==0)t=n/2;

  else t=3*n+1;

  cnt++;

  if(mp[t]){

       cout<<cnt<<endl;

       return 0;

  }

  mp[t]++;

  n=t;

}

}

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



int main()

{

    int s;

    std::cin >> s;

    int list[1000000];

    int index = 0;

    int a_i;

    int a_pre = s;

    list[0] = s;

    index = index + 1;

    bool search_flag = false;

    while( true){

        if ( a_pre % 2 == 0){

            a_i = a_pre / 2;

        } else {

            a_i = 3 * a_pre + 1;

        }

        list[index] = a_i;

        index = index + 1;

        for ( int i = 0; i< index-1; ++ i ){

            if ( list[i] == a_i) {

                search_flag = true;

                break;

            }

        }

        if (search_flag==true){

            break;

        } 

        a_pre = a_i;



    }

    std::cout << index << std::endl;

    return 0;

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

using namespace std;

#define ll long long

#define MX 100005



const ll INF = 1e12;



int A[MX];

ll dp[MX];

int n, k;



ll recur(int pos){

    if (pos == 1) return dp[pos] = 0;

    if (pos == 2) return dp[pos] = abs(A[2] - A[1]);

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



    ll mn = INF;

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

        if (pos - i >= 1){

            mn = min(mn, recur(pos - i) + abs(A[pos] - A[pos - i]));

        }

        else break;

    }

    return dp[pos] = mn;

}

int main()

{

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

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

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

        dp[i] = -1;

    }



    ll ans = recur(n);

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

    return 0;

}




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

#include<cmath>

#include<algorithm>

using namespace std;

const int maxn=1e5 + 5;

int main()

{

	int dp[maxn];

	int h[maxn];

	int n,k;

	while(~scanf("%d %d",&n,&k)){

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

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

		dp[1] = 0;

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

	    	dp[i]=1e9;

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

				if(i==j) break;

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

				}

	}

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



using namespace std;

typedef long long int ll;

ll dp[100010];

ll arr[100010];



int main(){

    ll n,k;

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



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

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

    }



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

        dp[i] = 10000000000000;

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

            if(i-j >= 1){

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

            }

        }

    }





    printf("%lld\n",dp[n]);

}


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

using namespace std;

int main()

{

    long long int n, m = 1;

    cin >> n;

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

    {

        m = m * i % 1000000007;

    }

    cout << m;

}

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

#include<math.h>

int main(){

	int i,m;

	long long n=1;

	scanf("%d",&m);

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

		n=n*i;

		n=n%1000000007;

	}

		printf("%d",n);

}




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

#include <vector>

#include <algorithm>

#include <string>

#include <map>

#include <cmath>

#include <queue>

#include <numeric>

#include <climits>

#include <iterator>

#include <iomanip>

#include <stack>

#include <bitset>

#include <functional>

using namespace std;

const constexpr int INF = 1e9;

//typedef std::pair<std::string,double> P;





#define FOR(i, a, n) for (ll i = (ll)a; i<(ll)n; ++i)

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



typedef long long ll;

typedef vector<int> VI;

const constexpr ll MOD = 10000000000+7;

vector<pair<int, int> > vp;

 

struct Less {

    bool operator()(const pair<int, int>& x, const pair<int, int>& y) const {

        return x.first > y.first;

    }

};



ll GCD(ll a, ll b){

    if(b==0) return a;

    return GCD(b, a%b);

}





//グラフの隣接リスト

VI g[200010];

//頂点の入次数を管理

int h[100010];

ll N, K;

string s;

ll v[51];



int main(void) {

    cin >> N >> K;

    

    ll cnt=0;

    if(K==0) cout << N*N << endl;

    else {

        for(ll i=K+1; i<=N; ++i){

        cnt += N/i*max(ll(0), i-K) + max(ll(0), N%i-K+1);

        }

        cout << cnt << endl;

    }

    

	return 0;

}

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

#include <vector>

#include <algorithm>

#include <string>

using namespace std;

using ll = long long;

//西暦⇒時代の歴

ll a[100010];

int main()

{

    ll N, K;

    cin >> N >> K;



    if (K == 0)

    {

        cout << N * N << endl;

        return 0;

    }

    ll ans = 0;

    for (ll i = K + 1; i <= N; ++i)

    {

        // aは適当な数字に固定 b = i;

        ans += N / i * (i - K);

        ans += max(0LL, (N % i) - K + 1);

    }

    cout << ans << endl;

    return 0;

}

// Fastest known solution for this problem:


#include <iostream>

#include <cstdio>



using namespace std;



int main()

{

    long long n,k;

    long long i,j;

    long long ans;

    while (scanf("%lld%lld",&n,&k)!=EOF)

    {

        ans=0;

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

        {

            j=i+k;

            ans+=(n/j)*i;

            if(n%j-k>=0)

                ans+=n%j-k+1;

        }

        if (k==0)

            ans=n*n;

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

    }

    return 0;

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

#include<cstring>

using namespace std;

char s[105],ans[105];

int main()

{

	cin>>s;

	int len=strlen(s),tot=0;

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

	{

		if(s[i]=='0')	ans[++tot]='0';

		if(s[i]=='B'&&tot>0)	tot--;

		if(s[i]=='1')	ans[++tot]='1';

	}

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

	{

		cout<<ans[i];

	}

	return 0;

}

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

#include<string.h>



int main(){

	char str[15];

	scanf("%s",&str);

	char temp[15];

	int index=0,len=strlen(str);

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

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

			temp[index] = '0';

			//printf("index[%d] : %s\n",index,temp);

			index++;

		} 

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

			temp[index] = '1';

			//printf("index[%d] : %s\n",index,temp);

			index++;

		} 				//01B0 temp =     --0

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

			//printf("index[%d] : %s\n",index,temp);

			if(index != 0) index--;

		}

	}

	//printf("%d\n",index);

	temp[index]='\0';

	printf("%s\n",&temp);

	

	

	

	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>

#define ll long long

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

#define PI acos(-1)

#define endl "\n"

#define fx(x) fixed<<setprecision(x)

#define sz(s) s.size()

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

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

const double eps=1e-9;

const int mod=1e9+7;

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

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

ll gcd(ll x,ll y) {return(!y)?x:gcd(y,x%y);}

ll lcm(ll x,ll y) {return((x/gcd(x,y))*y);}

using namespace std;

void fast(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

}

int a[100102];

int dp[100102];

int n,k;

int solve(int idx){

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

    if(idx+k>=n)return abs(a[idx]-a[n-1]);

    int &ret=dp[idx];

    if(ret!=-1)return ret;

    int mn=INT_MAX;

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

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

    }

    ret=mn;

    return ret;

}

int main()

{

    cin>>n>>k;

    memset(dp,-1,sizeof dp);

    memset(a,1000000000,sizeof a);

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

        cin>>a[i];

    }

    cout<<solve(0);

    return 0;

}


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

#define f first

#define s second

#define pb push_back

#define ENDL '\n'

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

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

#define deb(a) cout << #a << ": " << a << ENDL

#define fore(i, a, b) for(int i(a), ThkMk(b); i < ThkMk; ++i)

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



using namespace std;



typedef long long lli;

typedef long double ld;

typedef pair<lli, lli> ii;

typedef vector<lli> vi;



int main()

{   _

    lli n, k; cin >> n >> k;

    vi v(n); fore(i, 0, n) cin >> v[i];

    vector<lli> dp(n, 1e18);

    dp[0] = 0;

    fore(i, 0, n) {

        fore(j, i + 1, min(n, i + k + 1)) {

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

        }

    }

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

    return 0;

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

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

using namespace std;

typedef long long int ll;



int N;

int K;

int h[100010];

ll dp[100010];



ll INF = 1LL << 60;



void chmin(ll &a, ll b){

  if (a > b){

    a = b;

  }

}



int main(){



  cin >> N >> K;

  

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

    cin >> h[i];

  }

  

  for (int i = 0; i < 100010; 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];        

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

using namespace std ;



const int nmax = 102000;

const int inf =999999999;



int h[nmax];

int dp[nmax];



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);



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

    vector<int>mov;

    for(int i=1;i<=k;++i)mov.push_back(i);





    for(int i=1;i<=n;++i){cin>>h[i];dp[i]=inf;}

    dp[1]=0;

    dp[0]=inf;



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

    {

        for(auto j:mov)

        {

            if(i-j<=0)break;

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

#include <algorithm>

#include <climits>

#define SIZE 100001

#define INF INT_MAX >> 1

 

using namespace std;

 

int dp[SIZE];

 

int main(void){

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

	int h[SIZE] ={0};

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

		cin >> h[i];

	}

 

	fill(dp, dp+n, INF);

	dp[0] = 0;

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

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

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

		}

	}

 

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

}

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

#define INF 100000000000000

using namespace std;



int main(){

    ios::sync_with_stdio(false);    cin.tie(NULL);  cout.tie(NULL);

    int n, k;  cin >> n >> k;   vector<long long int> vec(n), dp(n, INF);

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

        cin >> vec[i];

    }

    dp[0]=0;

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

        for(int j=1;min(i, k)>=j;j++){

            dp[i]=min(dp[i], dp[i-j]+abs(vec[i]-vec[i-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;





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

#include<cstdio>

#include<algorithm>

using namespace std;

#define maxn 100010

int n,k;

int a[maxn];

int main()

{

	cin>>n>>k;

	int cnt=0;

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

	{

		cin>>a[i];

		if(a[i]>=k)

		{

			cnt++;

		}

	}

	cout<<cnt<<endl;

	return 0;

 } 

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



int main()

{

    int n,k,ans=0;

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

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

    {

        int x;

        scanf("%d",&x);

        if(x>=k)ans++;

    }

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

    return 0;

}

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

using namespace std ;

 

int main()

{

  int n , k , a , answer ;

  cin >> n >> k ;

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

  {

    cin >> a ;

    if(a >= k)

      answer++ ;

  }

  cout << answer << endl ;

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



using namespace std;



int main()

{

	int x, y;



	cin >> x >> y;



	if (x + y < 10) {

		cout << x + y;

	}

	else {

		cout << "error";

	}

    return 0;

}

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

int main(){

	int a,b;

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

	if(a+b<10){printf("%d\n",a+b);}

	else {printf("error\n");}

	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;

}