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

using namespace std;

typedef long long int ll;

#define faster ios_base::sync_with_stdio(0);

#define faster_tie cin.tie(0);

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

#define NL cout<<'\n'



ll n,k,x;

vector<ll>height;

ll sol[110000];



ll solve(ll x)

{

    if(x==1)

    {

        return 0;

    }

    if(sol[x]!=-1)return sol[x];

    ll a=INT_MAX;



    for(ll i=x-1;i>x-1-k;i--)

    {

        if(i==0)break;

        a=min(a,solve(i)+abs(height[i]-height[x]));

    }

    sol[x]=a;

    return a;



}





int main()

{

  

  	memset(sol,-1,sizeof sol);

    cin>>n>>k;

    height.push_back(-1);

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

    {

        cin>>x;

        height.push_back(x);

    }



    ll ans=solve(n);

    cout<<ans;







    return 0;

}


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

using namespace std;



typedef long long ll;



const int MOD = 1e9 + 7;

const int INF = 0x3F3F3F3F;

const ll INFLL = 0x3F3F3F3F3F3F3F3FLL;



int n, k;

int h[112345];

int ans[112345];



int main(){

	

	ios_base :: sync_with_stdio(false);

	cin.tie(0);

	

	cin >> n >> k;

	

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

		cin >> h[i];

	

	memset(ans, INF, sizeof ans);

	ans[1] = 0;

	

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

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

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

	

	cout << ans[n] << "\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;



int main()

{

    int arr[100000], pen[100000], i;

    for(i =0; i< 100000; i++)

    {

        arr[i]=0;

        pen[i]=0;

    }

    int n, m, penal= 0, score =0, num;

    string s;

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

    getchar();

    for(i =0; i<m; i++)

    {

        scanf("%d", &num);

        getchar();

        cin >> s;

        getchar();

        if(arr[num]==0)

        {

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

                pen[num]++;

            else

            {

                arr[num]=1;

                penal+=pen[num];

                score++;

            }

        }

    }

    printf("%d %d", score, penal);



    return 0;

}


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

#include<iostream>

using namespace std;

int a[100010]={0};

int main()

{

	int n,m,i;

	cin>>n>>m;

	int x=0,y=0;

	for(i=0;i<m;i++)

	{

		int s;string ss;

		cin>>s>>ss;

		if(a[s]>=0)

		{

			if(ss=="WA")

			{

				a[s]++;

			}

			else

			{

				x+=a[s];

				a[s]=-1;

				y++;

				

			}

		}

	}

	printf("%d %d\n",y,x);

}

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

#include <vector>

using namespace std;

int main ()

{

	int n, m, acc=0, pen=0; cin>>n>>m;

	bool test[n+1]={false};

	int arr[n+1]={0};

	int p[m];

	string s[m];

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

	{

		cin>>p[i];

		cin>>s[i];

		if (test [p[i]]==false && s[i]=="AC")

		{

			test[p[i]]=true;

			acc++;

			pen+=arr[p[i]];

		}

		if (test[p[i]]==false && s[i]=="WA")

		{

			arr[p[i]]++;

		}

	}

	cout<<acc<<endl<<pen<<endl;

	return 0;

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

using namespace std;

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

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

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

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

#define INF 999999999

typedef long long int Int;

using ll = long long;

using VI = vector<int>;



int main(){

  Int s;cin >> s;

  Int a[1000000] = {0};

  Int x = s;

  Int i = 1;

  a[x-1] = i;

  Int ans=0;

  bool flag = false;

  while(1){

    if(flag) break;

    i++;

    if(x%2==0){

      x/=2;

      if(a[x-1]==0) a[x-1] = i;

      else{

        ans = i;

        flag = true;

      }

    }

    else if(x%2==1){

      x=3*x+1;

      if(a[x-1]==0) a[x-1] = i;

      else{

        ans = i;

        flag = true;

      }

    }

    if(flag) break;

  }

  cout << ans << endl;

}


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

using namespace std;

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

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

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

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

#define INF 999999999

typedef long long int Int;

using ll = long long;

using VI = vector<int>;



int main(){

  Int s;

  cin >> s;

  map<Int,Int> mp;

  Int i=1;

  while(!mp.count(s)){

    mp[s]=i++;

    if(s&1) s=s*3+1;

    else s/=2;

  }

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

#include<vector>

#include<map>

using namespace std;

typedef long long int ll;

vector<ll>dat;

vector<ll>res;

ll m(ll i,ll j){

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

    if(i>=dat.size()-1){

        return 0;

    }

    res[i]=1e9;

    for(ll k=1;k<=j&&i+k<dat.size();k++){

        res[i]=min(res[i],abs(dat[i]-dat[i+k])+m(i+k,j));

    }

    return res[i];

}

int main(){

    ll a,aux,k;

    cin>>a>>k;

    for(ll i=0;i<a;i++){

        cin>>aux;

        dat.push_back(aux);

    }

    res=vector<ll> (a,-1);

    cout<<m(0,k)<<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<cstdio>

#include<iostream>

#include<cstring>

#include<algorithm>

using namespace std;



const int MAXN = 1e5 + 100;

const int inf = 0x3f3f3f3f;

int n, k;

int a[MAXN], dp[MAXN];



int main(){

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

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

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

	}

	memset(dp, 0x3f, sizeof dp);

	dp[1] = 0;

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

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

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

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

		}

	}

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

}

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

using namespace std;

#define int long long int



int n; //number of stones

int k; //farthest the frog can jump

vector<int> h; //height of the stones

vector<int> dp; //memorize heck



int jump(int stone){

	if(dp[stone]==-1){

		if(stone == n-1){

			return 0;

		}

		else{

			dp[stone] = INT_MAX;

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

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

			}

			return dp[stone];

		}

	}

	else{

		return dp[stone];

	}

}



main(){

	cin >> n >> k;

	

	dp.assign(n, -1);

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

		int x;

		cin >> x;

		h.push_back(x);

	}



	cout << jump(0) << "\n";

}

// 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):
																//  SMOKE SHISHA PLAY FIFA  // 

#include<bits/stdc++.h>

using namespace std;



typedef long long int ll;

typedef long double ld;



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

#define pll pair<ll,ll>

#define MP make_pair

#define ff first

#define ss second

#define PB push_back

#define mod 1000000007

#define lp(i,start,end) for(ll i=start;i<=end;i++) 

#define deb1(a) cout<<#a<<" = "<<(a)<<endl;

#define deb2(a,b) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<endl;

#define deb3(a,b,c) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<", "<<#c<<" = "<<(c)<<endl;





ll modu(ll a, ll b)

{

	ll ans =1;

	while(b>0)

	{

		if(b&1)

		ans = (ans*a)%mod;

		b/=2;

		a = (a*a)%mod;

	}

	return ans;

}



int main()

{

	fastio

	ll t=1;

	//cin >> t;

	while(t--)

	{

		ll n,k;

		cin>>n>>k;

		ll a[n+1];

		ll cost[n+1];

		lp(i,1,n){

			cin>>a[i];

			cost[i]=mod;

		}

		cost[1]=0;

		lp(i,2,n){

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

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

			}

		}

		cout<<cost[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):
#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;

}


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

using namespace std;

const int Maxn=1e5+10;

int N,K;

int H[Maxn],Dp[Maxn];

int main()

{

	cin>>N>>K;

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

	Dp[1]=0;

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

	{

		cin>>H[i]; 

		for(int j=i-1;j>=max(i-K,1);j--) Dp[i]=min(Dp[i],Dp[j]+abs(H[i]-H[j]));

	}

	cout<<Dp[N]<<endl;

	return 0;

}

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

using namespace std;

const int maxN = 1e5 + 10;

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

int main()

{

	cin >> n >> k;

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

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

	{

		int mn = 2e9;

		for(int j = 1; j <= k && i - j >= 0; j++) mn = min(mn, dp[i - j] + abs(h[i] - h[i - j]));

		dp[i] = mn;

	}

	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;

#define ll long long

#define ull unsigned long long

#define mst(a, b) memset((a), (b), sizeof(a))

#define mp(a, b) make_pair(a, b)

#define pi acos(-1)

#define pii pair<int, int>

#define pb push_back

const int INF = 0x3f3f3f3f;

const double eps = 1e-10;

const int maxn = 1e5 + 7;

const int maxm = 5e5 + 10;

ll a[maxn];

ll dp[maxn];

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int n, m;

    cin >> n >> m;

    mst(dp, INF);

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

        cin >> a[i];

    dp[0] = 0;

    dp[1] = 0;

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

    {

        for (int j = i - 1; j >= max(1, i - m); --j)

        {

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

        }

    }

    cout << dp[n] << endl;

    return 0;

}

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

# define ll long long

# define fi first

# define se second



using namespace std ;



ll n, x, k;

ll ans[200001], vc[200001];



int main() {

    cin >> n >> k;

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

        cin >> vc[i] ;

    }

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

        ans[i] = abs(vc[i] - vc[i-1]) + ans[i-1];

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

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

        }

    }

    cout << ans[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;



int main()

{

    int N;

    cin >> N;



    int A[N];

    int freq[100001];

    memset(freq, 0, sizeof freq);



    long long int sum=0;

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

    {

        cin >> A[i];

        freq[A[i]]++;

        sum+=A[i];

    }



    int Q;

    cin >> Q;



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

    {

        int B, C;

        cin >> B >> C;

        sum=sum+(C-B)*freq[B];

        //cout << freq[C] << " " << freq[B] << endl;

        freq[C]+=freq[B];

        freq[B]=0;

        cout << sum << endl;

    }



}


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

#include <cstdio>



using namespace std;

const int MAXN = 2e5 + 5;



int n, q, a[MAXN], cnt[MAXN];

long long sum;

int main() {

	scanf("%d", &n);

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

		scanf("%d", a + i), cnt[a[i]]++, sum += a[i];

	}

	scanf("%d", &q);

	for (int b, c, i = 1; i <= q; i++) {

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

		sum = sum + (long long)cnt[b] * (c - b);

		cnt[c] += cnt[b];

		cnt[b] = 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;

int arr[100010],n,k;

int dp[100010];

int f(int pos){

	if(pos>=n-1)return 0;

	int &ret=dp[pos];

	if(ret^-1)return ret;

	ret=1000000010;

	for(int i=1;i<=k && pos+i<n;i++)ret=min(ret,f(pos+i)+abs(arr[pos]-arr[pos+i]));

		return ret;

}



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

{

	memset(dp,-1,sizeof dp);

	cin>>n>>k;

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

		cout<<f(0)<<endl;

	return 0;

}

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

#define ll long long

using namespace std;

ll f(ll a){

if(a<0){

    a=0;

}

return a;

}

int main(){

    ll n,k;

    cin>>n>>k;

    ll a[n],dp[n];

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

        cin>>a[i];

        dp[i]=9999999999;

    }

    dp[0]=0;

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

            ll q=f(i-k);

    //cout<<q<<endl;

        for(ll j=q;j<i;j++)

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

        //cout<<dp[i]<<" ";

    }

    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;





}