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

using namespace std;

typedef long long ll;

ll dp[100009];

int arr[100009];

ll sum = 0;

int k;

long long Frog1(int n){

 

    if(n==0) return 0;

 

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

    else {

            ll sum = INT_MAX;

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

 

                ll x;

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

                else x = Frog1(i);

 

                x = abs(arr[i] - arr[n]) + x;

                sum = min(sum,x);

                //cout << n  << " " << i  << " " << j << endl;

            }

            dp[n] = sum;

            return sum;

    }

}

 

 

int main()

{

    int n;

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

    cin >> n >> k;

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

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

}

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

using namespace std;

#define ll long long

#define ld long double

#define sf scanf

#define pf printf

#define pb push_back

#define llu unsigned long long

#define U unsigned int

#define SIZE 10000000

#define pie 3.14159265358979323

#define minuss 1e-6

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



#define BIG 100000000000000



ll  lcm(ll a,ll b)

{

    ll GCD=__gcd(a,b);

    ll lcm=(a*b)/GCD;

    return lcm;

}

ll dp[1000005];

ll weight[1000005];

ll total,val;

int main()

{

    cin>>total>>val;

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

        cin>>weight[i];



    weight[0]=SIZE;

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

    {

        dp[i]=BIG;

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

            if(i-j>=1)

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



    }

    cout<<dp[total]<<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 <string.h>

using namespace std;

vector <int> x ;

int n , dp[100003] , k ;

int SAAD ( int idx ) {

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

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

	int res = 1e9 ;

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

		if (idx+i<n) res = min(res,SAAD(idx+i)+abs(x[idx]-x[idx+i]));

	}

	return dp[idx] = res ;

}

int main() {

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

	int p ;

	cin >> n >> k ;

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

		cin >> p; 

		x.push_back(p);

	}

	cout << SAAD(0) ;

	return 0;

}

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

#include <algorithm>

#include <math.h>

using namespace std;

int dp[int(1e5+2)] , x[int(1e5+2)] ;

int main() {

	int n , k ;

	cin >> n >> k ;

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

		cin >> x[i] ;

		if ( i == 0 ) continue ;

		int mn = 1e9 ;

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

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

		}

		dp[i] = mn ;

	}

	cout << dp[n-1] ;

	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 N = 1e5 + 9, OO = 0x3f3f3f3f;



int n, k, h[N], dp[N];



int solve(int i){

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

    if(i >= n)   return 1e9;



    int &ret = dp[i];

    if(~ret)    return ret;



    ret = OO;

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

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



    return ret;

}



int main(){



    cin >> n >> k;

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

        cin >> h[i];



    memset(dp, -1, sizeof dp);



    cout << solve(0);

}


// Optimized solution to the same problem (faster):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<unordered_map>

using namespace std;

//<DEFINE>

void Fast() {

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

}

void File() {

#ifndef ONLINE_JUDGE

	freopen("Input.txt", "r", stdin);

	freopen("Output.txt", "w", stdout);

#endif

}

#define ll long long

#define ull unsigned long long

#define mod 1000000007

ll gcd(ll a, ll b) {

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

}

#define fix(n) cout << fixed << setprecision(n);

#define numOfOnes(n) __builtin_popcount(n)

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

#define skip continue

#define stop(n) return cout<<n,0

#define pi  acos(-1)

#define endl "\n"

#define ceil(x,y) (x+y-1)/y

//</DEFINE>



/*█▀▀▄░░░░░░░░░░░▄▀▀█

░█░░░▀▄░▄▄▄▄▄░▄▀░░░█

░░▀▄░░░▀░░░░░▀░░░▄▀

░░░░▌░▄▄░░░▄▄░▐▀▀

░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█

░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█

▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█

█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀

░▀▄░░▀░░▀▀▀░░▀░░░▄█▀

░░░█░░░░░░░░░░░▄▀▄░▀▄

░░░█░░░░░░░░░▄▀█░░█░░█

░░░█░░░░░░░░░░░█▄█░░▄▀

░░░█░░░░░░░░░░░████▀

░░░▀█▄▄▄▀▀▀▀▄▄▄█▀*/

int n,k;

vector<int>v;

ll res[100001];

//<FUNCTIONS>

ll solve(int idx = 0) {

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

	if (~(res[idx]))return res[idx];

	ll mn = INT_MAX;

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

		if (idx + i >= n)break;

		mn = min(mn, solve(idx + i) + abs(v[idx] - v[idx + i]));

	}

	return res[idx] = mn;

}

//</FUNCTIONS>		

int main() {

	Fast();

	cin >> n >> k;

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

		res[i] = -1;

	v.resize(n);

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

	cout << solve() << 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<cstdio>

#include<cstring>

#include<iostream> 

#include<cmath>

#include<algorithm>

#include<stdlib.h>

using namespace std;



int main()

{

	string ans;

	string ppp;

	int n;

	cin>>n;

	cin>>ppp;

	

	if(ppp.length()>n)

	{

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

		{

			ans=ans+ppp[i];

		}

		ans.append(3,'.');

	}

	else

	{

		ans.assign(ppp);

	}

	cout<<ans<<endl;

return 0;

 }

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

#include <string.h>

int main(){

	int k;

	scanf("%d",&k);

	char s[100];

	scanf("%s",&s);

	int lol = strlen(s);

	s[k] = '\0';

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

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

	}

	if(k<lol){

		printf("...");

	}

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;int ans=0;

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

		int a;cin>>a;if(a>=k)ans++;

	}

	cout<<ans<<endl;

}

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

int main()

{

	int a,n,k,h[100000],cnt=0;

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

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

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

		if(h[a]>=k){

			cnt++;

		}

	}

	printf("%d",cnt);

}

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

#define ll long long

#define ld long double





int main()

{

    ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

    ll cord,koita;

    cin >> koita >> cord; /// 3 7

    ll start = cord - koita + 1; /// 5

    for (int i=start; i<cord; i++){

        cout << i << " "; /// 5 6

    }

    cout << cord << " "; // 7

    ll en = cord + koita - 1; /// 9

    for (int i= cord + 1; i<=en; i++){

        cout << i << " ";

    }

    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>

using namespace std;

string a;

int b[15],q;

int main()

{

    cin>>a;

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

    {

        if(a[i]=='0'){q++;b[q]=0;} 

        if(a[i]=='1'){q++;b[q]=1;}

        if(a[i]=='B'&&q>=1)q--;

    }    

    for(int i=1;i<=q;i++)cout<<b[i];

    cout<<endl;

    return 0;

}

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

#include<cstring>

using namespace std;

char s[20], S[20];

int main()

{

	scanf("%s", s);

	int len = strlen(s);

	int cnt = 0;

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

	{

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

		{

			S[cnt] = '1';

			cnt++;

		}

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

		{

			S[cnt] = '0';

			cnt++;

		}

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

		{

			if(cnt)cnt--;

			S[cnt] = '\0';

		}

	}

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

	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;

#define MAX 100000

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

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



int main(){

    int s;

    cin>>s;

    int k[1000000]={};

    k[s]++;

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

        if(s%2==0){

            s/=2;

        }else{

            s=3*s+1;

        }

        k[s]++;

        if(k[s]==2){

            cout<<i<<endl;

            break;

        }

    }



    return 0;

}


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

using namespace std;

#define MAX 100000

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

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



int main(){

    int s;

    cin>>s;

    bool k[1000000]={};

    k[s]=1;

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

        if(s%2==0){

            s/=2;

        }else{

            s=3*s+1;

        }

        if(k[s]){

            cout<<i<<endl;

            break;

        }else{

            k[s]=1;

        }

    }



    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;



#define TC ll t; cin >> t; while(t--)

typedef long long ll;



int main()

{

    //freopen("input.txt", "r", stdin);

    //freopen("output.txt", "w", stdout);

    ll k,x;

    cin >> k >> x;

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

    {

        cout << i << " ";

    }

    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;

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

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;

char a[10000005];

int b[10000005],l;

int main()

{

	scanf("%s",a);

	int x=strlen(a);

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

	{

		if(a[i]=='0')b[++l]=0;

		else if(a[i]=='1')b[++l]=1;

		else if((a[i]=='B')&&(l))l--;

	}

	for(int i=1;i<=l;i++)printf("%d",b[i]);

	printf("\n");

	return 0;

}

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

#include <cstring>

using namespace std;

char s[10005],ans[10005];

int top=0;

int main(){

    scanf("%s",s);

    for(int i=0;i<strlen(s);i++)

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

            top++;

            ans[top]=s[i];

        } else if(top>0) top--;

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

        printf("%c",ans[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<cstdio>

#include<string>

using namespace std;



int main(){

	string s;

	int k;

	cin>>k>>s;

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

	else{

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

			cout<<s[i];

		}

		cout<<"...";

	}

	return 0;

} 

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

#include<string.h>



int main()

{

	int n;

	scanf("%d",&n);

	char str[102];

	scanf("%s",&str);

	int len=strlen(str);

	if(len>n)

	{

		str[n]='.';

		str[n+1]='.';

		str[n+2]='.';

		str[n+3]='\0';

	}

	printf("%s",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;

#define INF 1000000000

int dp(int i);

int n,k;

int h[100009],memo[100009];



int main(){

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

  cin>>n>>k;

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

  cout<<dp(0)<<endl;

  return(0);

}



int dp(int i){

  int rec=INF;

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

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

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

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

  }

  return memo[i]=rec;

}


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

using namespace std;

#define INF 1000000000

int dp[100009];



int main(){

  int n,k;

  int h[100009];

  cin>>n>>k;

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

    cin>>h[i];

    dp[i]=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]-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 <iostream>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

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



int n,k;

const int inf = 1e9;



int frog2(int i,int j){



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

        return dp[i];

    }



    int menor = inf;



    if(i>=j) return dp[i]=0;



    for(int x=1;x<=k;x++){ ///i=0; i=1;i=2;i=3;

        if(i+x <= j){      ///x=1, x=1;x=1;x=1;

            long long cost = (frog2(i+x,j)+ abs(pedras[i]-pedras[i+x]));



            if(cost < menor){

                menor = cost;

            }

        }

    }



    return dp[i] = menor;

}



int main(){



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



    cin >>n>>k;



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

        cin >> pedras[i];

    }



    cout<<frog2(0,n-1)<<endl;

}


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



}


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

using namespace std;

double arr[1000000];

int main(){

    int a,b,c,i,n;

    cin>>n>>a;

    cout<<a-n+1;

    for(i = 1;i < 2*n-1;i++){

        cout<<' '<<a-n+i+1;

    }

}


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

}