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

#include<cstring>

#include<cstdio>

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

	char str2[15] = {0};

	int x = 0, ctr = 0, j = 0;

	scanf("%s", str);

	

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

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

			str2[j++] = '0';

			ctr++;

		}

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

			str2[j++] = '1';

			ctr++;

		}

        else{

			if(ctr > 0){

			j--;

			str2[j] = '\0';

			ctr--;

		}

		}

	}

	printf("%s", str2);

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

#include<cstring>

#include<cmath>

#include<map>

#include<algorithm>

#include <iostream>

using namespace std;

typedef long long LL;

const  LL MAXN = 1e9+7;

int main(){

	int n;

	LL ans = 1;

	scanf("%d",&n);

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

		ans *= i;

		ans %= MAXN;

	}

	printf("%lld",ans);

	return 0;

}

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

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

{

	int n;

	scanf("%d",&n);

	long long i=1;

	long long cc=1;

	//int flag=0;

	while(n--)

	{i=i*(cc++);i=i%1000000007;}

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

 	//if(flag) printf("\nPrint the answer modulo 109+7.\n");

	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>

#include <algorithm>

#include <string>

#include <cstring>

#include <iomanip>



using namespace std;

typedef long long ll;

const int maxn=1e6+1111;



int arr[maxn];

int dp[maxn];

int main(){

    int n,k;

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

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

    dp[0]=dp[1]=0;

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

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

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

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

        }

    }

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

    return 0;

}

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

#include <algorithm>

#include <string>

#include <cstring>

#include <iomanip>



using namespace std;

typedef long long ll;

const int maxn=1e5+1111;



int arr[maxn];

int dp[maxn];

int main(){

    int n,k;

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

    dp[0]=dp[1]=0;

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

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

        if(i>1) dp[i]=dp[i-1]+abs(arr[i]-arr[i-1]);

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

            dp[i]=min(dp[i-j]+abs(arr[i]-arr[i-j]),dp[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 <bits/stdc++.h>

using namespace std;

typedef long long int ll;

#define INF 1LL << 60;



int main() {

  int n, k;

  cin >> n >> k;

  int h[100110];

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



  ll dp[100110];

  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++) {

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

    }

  }



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

}


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

using namespace std;



typedef long long int ll;

ll MOD = 1000000007;

ll INFL = 1ll << 60;

ll INF = 1 << 28;



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

int main() {

  // 入力

  int n, k;

  cin >> n >> k;

  vector<int> v(n);

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

    cin >> v[i];

  }



  vector<ll> dp(n);

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

  dp[0] = 0;

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

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

      if (i - j < 0) break;

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

    }

  }



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

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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



using namespace std;



#define endl '\n'

#define tab " "

#define lli long long int

#define pb push_back



void read(){

    string s;

    lli si;



    cin >> si >> s;



    if(s.size() <= si) cout << s;

    else{

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

            cout << s[i];

        }

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

    }



    return;

}



int main(){

    read();

    return 0;

}

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



int main(){

	int k, len = 0;

	char str[100];

	scanf("%d %s", &k, str);

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

		len++;

	}

	if(len <= k){

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

	}

	else{

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

			if(i < k + 3){

				str[i] = '.';

			}

			else{

				str[i] = '\0';

			}

		}

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

	}

	return 0;

}

// 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 n,k;

	cin>>n>>k;

	vector<int>arr(n+1);

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

		cin>>arr[i];

	}

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

	dp[1]=0;

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

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

			if(dp[i-j]!=INT_MAX)dp[i]=min(dp[i],dp[i-j]+abs(arr[i]-arr[i-j]));

		}

	}

	cout<<dp[n]<<endl;

	return 0;

}

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

#include<algorithm>



int main(){

  int s,k;

  long long st[100001],p[100001];

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

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

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

  }

  p[0]=0;

  p[1]=0;

  p[2]=(st[2]-st[1] < 0) ? st[1]-st[2] : st[2]-st[1];

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

    p[i] = 1000000001;

  }

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

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

      if(i-j>=1){

	long long tmp;

	tmp = (st[i]-st[i-j] < 0) ? st[i-j]-st[i] : st[i]-st[i-j];

	if(p[i] > p[i-j]+tmp){

	  p[i] = p[i-j]+tmp;

	}

	if(tmp == 0) break;

      }else{

	break;

      }

    }

  }

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

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

#include <cstring>  

#include <algorithm>  

#include <cmath>  

#include <string>  

#include <map>  

#include <stack>  

#include <vector>  

#include <set>  

#include <queue>  

#include <string>

#include <iostream>

#include <cstdio>

#include <string>

#include <cstring>

#include <cctype>

#include <map>

#include <cctype>

#include <vector>

#include <cstdlib>

using namespace std;

long long mod = (long long)(1e9 + 7);



int main()

{

	long long  n;

	scanf("%lld", &n);



		long long sum = 1;

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

		{

			sum *= i;

			sum = sum%mod;

		}

		cout << sum%mod << '\n';



	return 0;

	

}

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

#include <algorithm>

using namespace std;



int main()

{

	int n,i;

	long long sum=1;

	scanf("%d",&n);

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

	{

		sum*=i;

		sum%=(1000000000+7);

	}

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

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



typedef   long long  int ll;

typedef  long double ld;

typedef std::vector<ll> vi;

typedef std::vector<std::vector<ll> > vv;



#define MOD 1000000007

#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);

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

#define mp make_pair

#define pb(x) push_back(x)

#define fi first

#define se second

#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;

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

#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];

#define ms(a,val) memset(a,val,sizeof(a))

using namespace std;



const int N = 1e5 + 5;





void solve(){

            ll i,j,n,m,k;

            

            

            cin>>n>>k;

            ll a[n];

            ll mincost[N];

            ms(mincost,0);

            input(a,0,n);

            

            fo(i,1,n)

            {

				ll minn = INT_MAX;

				for(j = i-1;j>=0&&j>=i-k;j--)

				{

					//if(i==n-1)

					//cout<<abs(a[i]-a[j])<<endl;

					minn = min(minn,mincost[j]+abs(a[j]-a[i]));//,mincost[i-2]+abs(a[i-2]-a[i]));

					

				}

				mincost[i] = minn;

			}

			//print(mincost,0,n);

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

            

    

            

						            



















}



int main()

{

    IO;

      ll t=1;

      //cin>>t;





      while(t--)

      {



          solve();

      }

    return 0;

}


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

#define ll long long

using namespace std;



int dp[100005], a[100005];

int n,k;



int solve(int n, int k)

{

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

        }

    }

    return dp[n-1];



}

int main()

{

    cin>>n>>k;

    memset(dp, 1000000, sizeof(dp));

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

        cin>>a[i];

    cout<< solve(n,k) <<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>

using namespace std;

const int N = 1e5 + 5;

int n,k;

int a[N];

long long dp[N];



long long f(int idx){

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

  long long &ret = dp[idx];

  if (ret != -1) return ret;

  ret = 1e18;

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

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

  }

  return ret;

}



int main(){

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

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

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

   }

   memset(dp,-1,sizeof dp);

   cout << f(0) << endl;

}

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

using namespace std;

const int N = 1e5 + 5;

int n,k;

int a[N];

int dp[N];



int f(int idx){

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

  int &ret = dp[idx];

  if (ret != -1) return ret;

  ret = 2e9;

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

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

  }

  return ret;

}



int main(){

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

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

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

   }

   memset(dp,-1,sizeof dp);

   cout << f(0) << 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 rep(i,n) for (int i = 0; i < (n); i++)

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

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

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

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

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

using namespace std;

using uint = unsigned int;

using ll = long long;

using ull = unsigned long long;

using vi = vector<int>;

using vvi = vector<vi>;

using vd = vector<double>;

using pii = pair<int, int>;

using vc = vector<char>;

using vvc = vector<vc>;

using vs = vector<string>;

using tiii = tuple<int, int, int>;



const int N = 1000000;



int main() {

  int s;

  cin >> s;

  

  vi a(N+1,0);

  a.at(1) = s;

  repi(i,2,N) {

    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;

    }

    

    repi(j,1,i-1) {

      if (a.at(j)==a.at(i)) {

        cout << i << endl;

        return 0;

      }

    }

  }

}

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

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

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

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

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

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

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

using namespace std;

using uint = unsigned int;

using ll = long long;

using ull = unsigned long long;

using vi = vector<int>;

using vvi = vector<vi>;

using vd = vector<double>;

using pii = pair<int, int>;

using vc = vector<char>;

using vvc = vector<vc>;

using vs = vector<string>;

using tiii = tuple<int, int, int>;



const int N = 1000000;



int main() {

  int s;

  cin >> s;

  

  set<int> st;

  int x = s;

  while (st.count(x)==0) {

    st.insert(x);

    if(x%2 == 0) {

      x /= 2;

    }

    else {

      x = x*3+1;

    }

  }

  

  cout << st.size()+1 << 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<bits/stdc++.h>

using namespace std;

int min(int a,int b){

    if(a<b) return a;

    else return b;

}





int main(){

    int he[100005],dp[100005];

    int n,k;

    cin >> n >> k;

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

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

        cin >> he[i];

    }

    

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

         int result;

    if(i==n) result=0;

    

    else{

        int x=-1;

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

            int a;

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

            

            if(x==-1) x=a;

            x=min(x,a);

            if(j-i==k) break;

        }

        result=x;

    }

    dp[i]=result;

    }

    cout << dp[1] << endl;

    

}

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

#define MAXN 1e5



using namespace std;





int dp (){

}



long long n,m,a,b,c,k;

string s1,s2;



int main(){

    cin>>n>>k;

    int l[n],l2[n];

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

        cin>>l[i];

        l2[i]=0;

    }

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

        l2[i]=l2[i-1]+abs(l[i]-l[i-1]);

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

            l2[i]=min(l2[i], abs( l[i] - l[i-j]) + l2[i-j]);

        }

    }

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

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

#include <bits/stdc++.h>

#include<algorithm>

using namespace std;

int main(){

	int k,x;

	cin>>k>>x;//k连续  第  x  块儿

	//向后 

	int num1=x-k;

	int num2=x+k;

	int t=2*k-1;

	int f=0;

	for(int i=num1+1;i<num2;i++){

		if(i>=-1000000||i<=1000000){

			cout<<i;

			if(f==t){

			}else{

				cout<<" ";

			}

		}

	}

	return 0;

}

// Optimized solution to the same problem (faster):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

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



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

        printf("%d ",i);

    }

    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;

}