stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
/*

 * VJUDGE B - Collatz Problem

 * author: roy4801

 * (C++)

 */

#include <bits/stdc++.h>



using namespace std;



#define PROB "B"

#define TESTC ""



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

typedef long long int LL;

typedef unsigned long long ULL;

typedef pair<int, int> P;

#define F first

#define S second

#define INF 0x3f3f3f3f

#define MP make_pair

#define MT make_tuple

#define PB push_back

#define N 1000000

bool arr[N+5];

int n, cnt; // s, times

int main()

{

	#ifdef DBG

	freopen("./testdata/" PROB TESTC ".in", "r", stdin);

	freopen("./testdata/" PROB ".out", "w", stdout);

	#endif

	while(cin >> n)

	{

		memset(arr, 0, sizeof(arr));

		cnt = 1;

		//

		while(true)

		{

			arr[n] = true;

			if(n & 1) // odd

			{

				n *= 3;

				n += 1;

			}

			else

			{

				n /= 2;

			}



			cnt++;

			if(arr[n])

				break;

		}



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

	}



	return 0;

}


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



using namespace std;



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



int main(int argc, char const *argv[]){



	map<int, int> m;

	int input, ans, sta;

	while(cin >> input){

		sta = 0;

		ans = 1;

		m[input] = 1;

		while(1){

			if (input % 2 == 0){

				ans++;

				input /= 2;

				m[input] += 1;

			}

			else{

				ans++;

				input = (3*input + 1);

				m[input] +=1;

			}

			for (map<int,int>::iterator it=m.begin(); it != m.end(); ++it){

				if (it->second == 2){

					cout << ans << endl;

					sta = 1;

					break;

				}

			}

			if(sta == 1)	break;

		}

	}



	return 0;

}

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



int main() {

    ios::sync_with_stdio(false);

    cin.tie(0); cout.tie(0);



    string s;

    cin >> s;

    stack<char> st;

    int n = s.size();

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

        if (s[i] == 'B' && !st.empty())

            st.pop();

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

            st.push(s[i]);



    s = "";

    while (!st.empty())

    {

        s += st.top();

        st.pop();

    }

    reverse(s.begin(), s.end());

    cout << s;

}

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

#include<string.h>

int main(){

	int pivot = 0;

	char str[15],monitor[15];

	scanf("%s" , &str);

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

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

			monitor[pivot] = '0';

			pivot++;

		}

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

			monitor[pivot] = '1';

			pivot++;

		}

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

			if(pivot != 0){

				pivot--;

			}

		}

	}

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

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

	}

	

	

	

	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>

#include <algorithm>

#include <string>

#include <vector>

#include <cstdio>

#include <cmath>

#include <cstring>

#include <cstdlib>

#define endl '\n'

#define ll long long

#define pb push_back

#define pi 3.14159265358979323846264338327950288419716939937510

#define RUN_FAST ios::sync_with_stdio(false);

#define sz 100

using namespace std;



int main()

{

    RUN_FAST;

    ll n, x, tmp, i, j, cnt;

    ll sum=0;

    vector <ll> v;

    cin >> n;

    tmp=n;

    while (tmp--) {

        cin >> x;

        sum+=x;

        v.push_back(x);

    }

    ll arr[100110]={0};

    sort(v.begin(), v.end());

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

        cnt=1;

        tmp=v[i];

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

            if (v[j]!=tmp) {

                break;

            }

            cnt++;

        }

        arr[tmp]=cnt;

        i=j-1;

    }

    ll q, a, b;

    cin >> q;

    while (q--) {

        cin >> a >> b;

        tmp=arr[a];

        sum-=tmp*a;

        sum+=tmp*b;

        arr[b]=arr[b]+tmp;

        arr[a]=0;

        cout << sum << endl;

    }

    v.clear();

    return 0;

}


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

#include <algorithm>

#include <string>

#include <vector>

#include <cstdio>

#include <cmath>

#include <cstring>

#include <cstdlib>

#define endl '\n'

#define ll long long

#define pb push_back

#define pi 3.14159265358979323846264338327950288419716939937510

#define RUN_FAST ios::sync_with_stdio(false);

#define sz 100

using namespace std;



ll arr[100110];



int main()

{

    RUN_FAST;

    ll n, x, tmp, i, j, cnt;

    ll sum=0;

    scanf("%lld", &n);

    tmp=n;

    while (tmp--) {

        scanf("%lld", &x);

        arr[x]++;

        sum+=x;

    }

    ll q, a, b;

    scanf("%lld", &q);

    while (q--) {

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

        tmp=arr[a];

        sum-=tmp*a;

        sum+=tmp*b;

        arr[b]=arr[b]+tmp;

        arr[a]=0;

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

    }

    return 0;

}


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

using namespace std;

#define ll long long

#include <string>

const int mxN=1e5;

const int maxN=5e3;

#define ld long double

#define pb push_back

#define mp make_pair

#define ins insert

#define vi vector<int>



int main()

{

    int n;

    cin >> n;

    int cnt[100000]={};

    ll s=0;

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

    {

        int x;

        cin >> x;

        cnt[x-1]++;

        s+=x;

    }

    int q;

    cin >> q;

    while(q--)

    {

        int b,c;

        cin >> b >> c;

        s+=cnt[b-1]*(c-b);

        cnt[c-1]+=cnt[b-1];

        cnt[b-1]=0;

        cout << s << "\n";

    }

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





using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;

const int mod = 1e9+9;







const int nax = 1e5+103;

int n, k;

int dp[nax];

int a[nax];

int f(int i){

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

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

	int ans = abs(a[i]-a[i+1])+f(i+1);

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

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

	}

	return dp[i]=ans;

}





int main(){

	cin>>n>>k;

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

	memset(dp, -1, sizeof dp);

	cout<<f(0)<<endl;

	return 0;

}

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

using namespace std;

 

int h[100000], mCost[100000];

int Nstones, k;



int main() {



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

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

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

	}

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

        	mCost[i] = 10000000000;

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

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

        	}

	}

	printf("%d", mCost[Nstones - 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):
#define _CRT_SECURE_NO_WARINGS

#include <iostream>

#include <iomanip>

#include <map>

#include <cmath>

#include <algorithm>

#include <string>

#include <vector>

#include <queue>

#include <set>

#include <deque>

#include <stack>

#include <bitset>

#include <fstream>

#include <map>

#include <unordered_map>



using namespace std;

typedef int me;

typedef long long ll;

typedef unsigned long long ull;



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

#define endl '\n'

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

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

#define PI 3.1415926535

#define sin(i) (long double)sin(i *PI / 180.0)

#define cos(i) (long double)cos(i * PI/ 180.0)



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

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



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

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

int id8[] = { 0, 1, 2, 3, 4, 5, 6, 7 };



int main()

{

    andrew;

    int n, k;

    cin >> n >> k;

    vector<ll>v(n + 1);

    v[0] = 0;

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

        cin >> v[i];



    vector<ll>mem(100005, 1e9);

    

    mem[0] = 0;

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

    {

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

        {

            if (j < n)

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



        }

    }



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



    









}


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

using namespace std;

#define endl '\n'

#define optimize() ios_base::sync_with_stdio(0);

int n,k;

int arr[100001];

int cost[100001];



int func(int index)

{

    if(index>=n-1)

        return 0;

    if(cost[index]!=-1)

        return cost[index];



    int step=INT_MAX;



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

    {

        if(index+jump<=n-1)

        {

            step=min(step,abs(arr[index]-arr[index+jump])+func(index+jump));

        }



    }



    return cost[index]=step;





}

int main()

{

    optimize();



    cin>>n>>k;

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

    {

        cin>>arr[i];

    }



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



    int res=func(0);

    cout<<res<<endl;

}




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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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



#define Solved int main()

#define pb push_back

#define mp make_pair

#define F first

#define S second

#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

#define forn(i, n) for(int i = 1; i <= n; i++)

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

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

#define pii pair<int, int>

#define ll long long

#define ull unsigned long long

#define ld long double

#define mii map<int, int>

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

using namespace std;



inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}



const int maxn = 1e9;

const ll inf = 1e18;

const int mod = 998244353;

//const int mod = 1e9 + 7;

const int N = 1e5 + 10;



int n, k;

ll a[N];

ll dp[N];



void solve() {

    cin >> n >> k;



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

        cin >> a[i];



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

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

        dp[i] = inf;

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

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

        }

    }

    cout << dp[n];

}



Solved {

    SpeedForce;

	int tt = 1;

    //cin >> tt;

	while (tt--) {

		solve();

		cout << '\n';

	}



}


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



using namespace std;

typedef long long ll;



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

int memo[100010];



int main(void)

{

    int s, i = 1;

    REP(ii, 100000) memo[ii] = 0;

    cin >> s;

    while(1) {

        if(memo[s] == 1) {



            cout << i << endl;

            return 0;

        }

        memo[s] ++;

        if(s%2==0) {

            s=s/2;

        } else {

            s = 3 * s + 1;

        }

        i++;

    }

}


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



using namespace std;

typedef long long ll;



#define INF (long long)(1e9+7)

#define REP(i,n) for(ll i = 0; i < n; i++)

#define REP1(i,n) for(ll i = 1; i <= n; i++)



ll gcd(ll a, ll b)

{

  if(b == 0) return a;

  return gcd(b, a % b);

}







int main(void)

{

  ll s;

  cin >> s;

  set<ll> st;



  ll cnt = 1;

  ll a = s;

  do {

    st.insert(a);

    if(a % 2 == 0) {

      a = a / 2;

    } else {

      a = 3 * a + 1;

    }

    cnt++;

    //    cout << a << " " << endl;

  } while(st.find(a) == st.end());

  //  cout << endl;

  cout << cnt << endl;

}


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

#include <algorithm>

#include <cstdint>

#include <cinttypes>

#include <cstring>



using i32 = std::int_fast32_t;

using i64 = std::int_fast64_t;



inline i32 diff(i32 x, i32 y) {

	return x > y ? x - y : y - x;

}



const i32 MaxN = 100001;



i32 dp[MaxN], A[MaxN];



signed main(signed argc, char **argv) {

	i32 n, k;

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



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

		scanf("%" SCNdFAST32, A + i);

	}

	

	memset(dp, 0x3f, sizeof dp);

	

	dp[0] = 0;



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

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

			if (i - j < 0) {

				break;

			}

			dp[i] = std::min(dp[i], dp[i - j] + diff(A[i], A[i - j]));

		}

	}



	printf("%" SCNdFAST32, dp[n - 1]);

	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>

#include<algorithm>

#include<cmath>

#include<string>

#include<vector>

#include<array>

#include<cstdlib>

#include<ctime>

#include<climits>

using namespace std;



int f(int n){

	if(n % 2 == 0){

		return n / 2;

	}

	else{

		return 3 * n + 1;

	}

}



int main(){

	int s;

	cin >> s;



	//int a[1000000];

	//vector<int> a;

	vector<int> a(1000000);

	a[0] = s;

	int ans = 0;

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

		a[i] = f(a[i - 1]);



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

			if(a[i] == a[j]){

				ans = i + 1;

				goto Exit;//

			}

		}

	}



Exit://

	cout << ans << endl;

}

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

#include<algorithm>

#include<cmath>

#include<string>

#include<vector>

#include<array>

#include<cstdlib>

#include<ctime>

#include<climits>

using namespace std;



int f(int n){

	if(n % 2 == 0){

		return n / 2;

	}

	else{

		return 3 * n + 1;

	}

}



int main(){

	int s;

	cin >> s;



	//int a[1000000];

	vector<int> a;

	//vector<int> a(1000000);

	a.push_back(s);

	int ans = 0;

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

		a.push_back(f(a[i - 1]));



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

			if(a[i] == a[j]){

				ans = i + 1;

				goto Exit;

			}

		}

	}



Exit:

	cout << ans << endl;

}

// 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):
#define M 2000000000

#include <stdio.h>



int min(int,int);

int abs(int);



int main(){

    int n,k;

    int h[100000];

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

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

    int dp[100000]={};

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



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

        if(i==0){

            dp[i]=0;

        }

        else {

            int j;

            if(i-k<0) j=0;

            else j=i-k;

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

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

            }

        }

        // for(int k=0;k<n;k++){

            // printf("dp[%d]=%d\n",k,dp[k]);

        // }

        // printf("\n");

    }

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



}



int min(int a,int b){

    if(a<=b) return a;

    else return b; 

}



int abs(int n){

    if(n>=0) return n;

    else return -n;

}

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

#include <limits.h>



int n,k;

int h[100000];

int dp[100000];



int abs(int x){return x>0?x:-x;}

int min(int a,int b){return a>b?b:a;}



int main()

{

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

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



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

    dp[0]=0;



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

    {

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

        {

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

        }

    }

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





}