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

using namespace std;

#define inf 0x3f3f3f3f

#define ll long long

const int N=200005;

const int mod=1e9+7;

const double eps=1e-8;

const double PI = acos(-1.0);

#define lowbit(x) (x&(-x))

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

ll qpow(ll a,ll b){ll res=1;while(b){if(b&1) res=res*a%mod;a=a*a%mod;b>>=1;}return res;}

ll inv(ll a,ll p){return qpow(a,p-2);}

int main()

{

    std::ios::sync_with_stdio(false);

    string s;

    cin>>s;

    char ans[15];

    int j=0,l=s.length();

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

    {

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

        {

            if(j>0)

                j--;

        }

        else

            ans[j++]=s[i];

    }

    ans[j++]='\0';

    cout<<ans<<endl;

    return 0;

}


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

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

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

    {

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

        {

            A--;

            if(A<0)

                A=0;

        }

        else

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

    }

    H[A]='\0';

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

    return 0;

}


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

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

#define pa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};

#define pb push_back



using ll = long long;

using namespace std;

using pint = pair<int,int>;

using pll = pair<ll,ll>;

const int INFint = 1e9;

const ll INFll = 1e18;

ll MOD=1e9+7;



ll gcd(ll a, ll b) { return __gcd(a,b); } //最大公約数

ll lcm(ll a, ll b) { return a/gcd(a,b)*b; } //最大公倍数



// aのn乗をMODで割りながら計算する

ll modpow(ll a, ll n) {

  long long res = 1;

  while (n > 0) {

    if (n & 1) res = res * a % MOD;

    a = a * a % MOD;

    n >>= 1; // right shift

  }

  return res;

}



// MODを法としたaの逆元を計算する

ll modinv(ll a) {

  return modpow(a, MOD - 2);

}



int main(){

  string s;

  cin>>s;

  int N = int(s.size());

  string ans;

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

    //cout << ans << endl;

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

      if (int(ans.size()) != 0){

        ans = ans.substr(0,int(ans.size())-1);

      }

    }else{

      ans += s[i];

    }

  }

  cout << ans << endl;

  return 0;

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



using namespace std;

int main(){

	long long n,k,a[100000],dp[100000];

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

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

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

	}

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

		dp[i]=abs(a[0]-a[i]);

	}

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

		dp[i]=1e9;

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

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

	}

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

}


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

#include <cmath>

#include <cstring>

#include <string>

#include <vector>

#include <set>

#include <queue>

#include <utility>

#include <algorithm>

#include <map>

#include <cassert>

using namespace std;



const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

int n, k, h[maxn], cost[maxn];



int solve2(int x) {

    // recursive method

    int res = cost[x];

    if (res < inf) return res;

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

        if (x - i >= 0) res = min(res, solve2(x - i) + abs(h[x] - h[x - i]));

    }

    cost[x] = res;

    return res;

}



int main() {

    cin >> n >> k;

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

    memset(cost, inf, sizeof(cost));

    cost[0] = 0;

    cout << solve2(n - 1) << endl;

    return 0;

}

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

#include <cstdio>

#include <cmath>

#include <cstring>

#include <string>

#include <vector>

#include <set>

#include <queue>

#include <utility>

#include <algorithm>

#include <map>

#include <cassert>

using namespace std;



const int maxn = 1e5 + 5;

int n, k, h[maxn], cost[maxn];



int main() {

    cin >> n >> k;

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

    for (int i = 1; i < n; i++) cost[i] = 1e9;

    cost[0] = 0;

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

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

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

        }

    }

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

    return 0;

}

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

#include <vector>

#include <algorithm>

#include <climits>

using namespace std ;



int main(){

    std::ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);

    string text , actions ;

    cin >> actions ;

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

        if(actions[i] == '1') text += '1' ;

        else if(actions[i] == '0') text += '0' ;

        else {

            if(text.size() > 0)

            text.pop_back() ;

        }

    }

    cout << text ;

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

#include<vector>

#include<string.h>

#include<cstdio>

using namespace std;

int main()

{

    char s[12],a;

    int i,j,len,num;

    vector<char>str;

    cin.get(s,12);

    len=strlen(s);

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

        if(str.empty()==false && s[i]=='B') str.pop_back();

        else if(s[i]=='1' || s[i]=='0') str.push_back(s[i]);

    }

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

        cout<<str[i];

    }

    cout<<endl;

}


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

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

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

    {

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

        {

            A--;

            if(A<0)

                A=0;

        }

        else

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

    }

    H[A]='\0';

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

    return 0;

}


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

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

#define pa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};

#define pb push_back



using ll = long long;

using namespace std;

using pint = pair<int,int>;

using pll = pair<ll,ll>;

const int INFint = 1e9;

const ll INFll = 1e18;

ll MOD=1e9+7;



ll gcd(ll a, ll b) { return __gcd(a,b); } //最大公約数

ll lcm(ll a, ll b) { return a/gcd(a,b)*b; } //最大公倍数



// aのn乗をMODで割りながら計算する

ll modpow(ll a, ll n) {

  long long res = 1;

  while (n > 0) {

    if (n & 1) res = res * a % MOD;

    a = a * a % MOD;

    n >>= 1; // right shift

  }

  return res;

}



// MODを法としたaの逆元を計算する

ll modinv(ll a) {

  return modpow(a, MOD - 2);

}



int main(){

  string s;

  cin>>s;

  int N = int(s.size());

  string ans;

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

    //cout << ans << endl;

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

      if (int(ans.size()) != 0){

        ans = ans.substr(0,int(ans.size())-1);

      }

    }else{

      ans += s[i];

    }

  }

  cout << ans << endl;

  return 0;

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

#include <algorithm>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

int dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1000000/*1e9*/;



int main(){



    memset(dp,inf,sizeof(dp));

    cin >>n>>k;



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

        cin >> pedras[i];

    }



    ///Comeca aki a bagaca

    dp[n-1]=0;



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



        ///int menor=inf;



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

            if(i+j<=n-1){

                int cost = abs(pedras[i]-pedras[i+j]);

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

                /*menor = min(menor,dp[i+j]+cost);*/

            }

        }



        ///dp[i]=menor;

    }



    cout<<dp[0];



}


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

#include <algorithm>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

int dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1e9;



int main(){



    ///memset(dp,inf,sizeof(dp));

    cin >>n>>k;



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

        cin >> pedras[i];

    }



    ///Comeca aki a bagaca

    dp[n-1]=0;



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



        int menor=inf;



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

            if(i+j<=n-1){

                int cost = abs(pedras[i]-pedras[i+j]);

                ///dp[i]=min(dp[i],dp[i+j]+cost);

                menor = min(menor,dp[i+j]+cost);

            }

        }



        dp[i]=menor;

    }



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

#include<cstring>

#include<iostream>

#include<algorithm>

#define ll long long

#define Di 3.1415926

#include<math.h>

#include<string>

#include<stack>

#define INF 0x3f3f3f3f

using namespace std;

ll  gcd(ll a,ll b)

{

    return a%b? gcd(b,a%b):b;

}

int main()

{   stack<char>w;

      string s;

      cin>>s;

      char a[20];

     int len =s.length();

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

     {

         if(s[i]=='0')

            w.push('0');

         else if(s[i]=='1')

            w.push('1');

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

            {

              if(!w.empty())

              {

                  w.pop();

              }

            }



     }

     int i=0;

     while(!w.empty() )

     {

         a[i]=w.top();

         w.pop();

         i++;



     }

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

        cout<<a[j];

     cout<<endl;



 return 0;

}


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

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

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

    {

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

        {

            A--;

            if(A<0)

                A=0;

        }

        else

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

    }

    H[A]='\0';

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

    return 0;

}


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

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

#define pa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};

#define pb push_back



using ll = long long;

using namespace std;

using pint = pair<int,int>;

using pll = pair<ll,ll>;

const int INFint = 1e9;

const ll INFll = 1e18;

ll MOD=1e9+7;



ll gcd(ll a, ll b) { return __gcd(a,b); } //最大公約数

ll lcm(ll a, ll b) { return a/gcd(a,b)*b; } //最大公倍数



// aのn乗をMODで割りながら計算する

ll modpow(ll a, ll n) {

  long long res = 1;

  while (n > 0) {

    if (n & 1) res = res * a % MOD;

    a = a * a % MOD;

    n >>= 1; // right shift

  }

  return res;

}



// MODを法としたaの逆元を計算する

ll modinv(ll a) {

  return modpow(a, MOD - 2);

}



int main(){

  string s;

  cin>>s;

  int N = int(s.size());

  string ans;

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

    //cout << ans << endl;

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

      if (int(ans.size()) != 0){

        ans = ans.substr(0,int(ans.size())-1);

      }

    }else{

      ans += s[i];

    }

  }

  cout << ans << endl;

  return 0;

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

using namespace std;

int main()

{

  int n,k;

  int h[100005] = {};

  int dp[100005] = {};

  int until;

  cin >> n >> k;



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



  dp[0] = 0;

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

  until = 1;

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

    until = 1;

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

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

      if(i - j < 0) break;

      if(abs(h[i] - h[i - j]) + dp[i - j] < abs(h[i] - h[i - until]) + dp[i - until] ){

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

	until = j;

      }

    }

  }

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

  return 0;

}


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

using namespace std;

int main()

{

  int dp[100005],h[100005];

  int n,k,sm;

  cin >> n >> k;

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

  

  dp[0] = 0;

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



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

    sm = 1;

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

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

      if(i - j < 0) break;

      if(abs(h[i] - h[i-j]) + dp[i-j] < abs(h[i] - h[i-sm]) + dp[i-sm]){

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

	sm = 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;

long long a[100001],n,k;

int main()

{

	cin>>n>>k;

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

	long long f[100001];

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

	f[1]=0;

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

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

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

	cout<<f[n];

	return 0;

}

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

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

int n,k,a[100005],f[100005];

int main()

{

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

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

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

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

	f[1]=0;

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

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

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

	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>

#include <cstring>

using namespace std;

int dp[100000];

int n,a[100000],k;



int ans(int i)

{

	if(i==n-1)

		return 0;



	if(dp[i]!=-1)

		return dp[i];

  

	int best=1e9;

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

	{

		if(i+j<n)

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

	}

	return dp[i]=best;

}



int main()

{

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

	cin>>n>>k;

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

		cin>>a[i];



	cout<<ans(0)<<endl;



	return 0;

}

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

#include <cstring>

using namespace std;

int dp[100000];

int n,a[100000],k;



int topdown()

{

	dp[n-1]=0;



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

	{

		dp[i]=1e9;

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

		{

          	if(i+j<n)

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

		}

	}

	return dp[0];

}



int main()

{

  	ios_base::sync_with_stdio(0);            //fast input and output

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>k;

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

		cin>>a[i];



	cout<<topdown();



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

{

	string s;

	int n;

	cin>>n;

	cin>>s;

	int i=0;

	for(i=0;s[i]!=0;i++);

	if(i<=n)cout<<s;

	else{

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

			cout<<s[t];

		cout<<"...";

	}

}

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

#include<string.h>

int main(){

	int k;

	char s[105];

	scanf("%d",&k);

	getchar();

	scanf("%s",&s);

	if(strlen(s)>k){

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

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

		}

		printf("...");

		printf("\n");

	}

	else{

		int l = strlen(s);

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

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

		printf("\n");

	}

}

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

}