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

#include<cstring>

#include<algorithm>

#include<cstdio>

using namespace std;

#define MAXN 100000 + 10

#define ll long long int

ll N[MAXN];

ll dp[MAXN];

#define INF 0x3f3f3f3f;

int main()

{

    ll n,k;

    cin>>n>>k;

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

        cin>>N[i],dp[i] = INF;

    dp[0] = 0;

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

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

            if(j >= 0)

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

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

    return 0;

}


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

#include <algorithm>

#include <cmath>

using namespace std;



int h[(int)1e5], dp[(int)1e5], K, N;





int main()

{

	K = N = 0;

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

	{

		dp[j] = 1e9;

	}

	scanf("%d %d", &N, &K);

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

	{

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

	}

	dp[1] = 0;

	dp[2] = dp[1] + abs(h[1] - h[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(h[i] - h[i + j]));

		}

	}

	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>

using namespace std;



int a[110000];

int m[110000];

int n,k;



int dp(){

	m[0] = 0;

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

		int temp = 2e9;

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

			if(i-j>=0){

				temp = min(temp,

					(m[i-j] + abs(a[i] - a[i-j]))

				);

			}else{

				break;

			}

		}

		m[i] = temp;

	}

//	for(int i = 0;i<n;i++) cout<<m[i]<<" ";cout<<"\n";

	return m[n-1];

}



int main(){

	cin>>n>>k;

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

		cin>>a[i];

	}

	cout<<dp()<<"\n";

	return 0;

}

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

using namespace std;

#define MAX(a,b) (a>b?a:b)

#define MOD(a) (a>0?a:-(a))

#define custoate(i)

int main(){

  int npedras, maxpulo;

  scanf("%d %d",&npedras,&maxpulo);

  int hpedra[npedras],custoate[npedras];

  for (size_t i = 0; i < npedras; i++)

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

  custoate[0] = 0;

  int tempcost = 2147483647,k,tempcost2;

  for (size_t i = 1; i < npedras; i++,tempcost = 2147483647) {

    // printf("i = %d, tempcost = %d\n", i,tempcost);

    for (k= (maxpulo>i ? i : maxpulo); k > 0; k--) {

      // printf("k = %d ",k);

      tempcost2 = custoate[i-k] + MOD(hpedra[i-k]-hpedra[i]);

      if (tempcost > tempcost2)

        tempcost = tempcost2;

    }

    // printf("custo[%d] = %d",i,tempcost);

    custoate[i] = tempcost;

    // printf("\n\n");

  }

  printf("%d\n", custoate[npedras-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):
#include <functional>

#include <algorithm>

#include <iostream>

#include <numeric>

#include <iomanip>

#include <utility>

#include <cstdlib>

#include <sstream>

#include <bitset>

#include <vector>

#include <cstdio>

#include <ctime>

#include <queue>

#include <deque>

#include <cmath>

#include <stack>

#include <list>

#include <map>

#include <set>



using namespace std;



typedef vector<int> vi;

typedef pair<int,int> pii;

typedef long long ll;



#define dump(x)  cerr << #x << " = " << (x) << endl

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

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

#define pb push_back

#define sz size()



const double EPS = 1e-10;

const int INF = 100000000;





int main(){

    int n,m;

    while(cin>>n>>m){

        if(n==0 && m==0)break;

        

        int data[600][600]={};

        rep(i,600){

            rep(j,600){

                if(i==j){

                    data[i][j]=0;

                }

                else{

                    data[i][j]=INF;

                }

            }

        }

        

        rep(i,m){

            int a,b;

            cin>>a>>b;

            a--;b--;

            data[a][b]=1;

            data[b][a]=1;

        }

        

        rep(k,n){

            rep(i,n){

                rep(j,n){

                    data[i][j]=min(data[i][j],data[i][k]+data[k][j]);

                }

            }

        }

        int c=0;

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

            if(data[0][i]<=2)

                c++;

        }

        cout<<c<<endl;

    }

    return 0;

    

}

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



using namespace std;



typedef vector<int> vi;

typedef pair<int,int> pii;

typedef long long ll;



#define dump(x)  cerr << #x << " = " << (x) << endl

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

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

#define pb push_back



int main(){

    int n,m;

    int cost[600][600];

    while(cin>>n>>m){

        if(n==0&&m==0)break;

        rep(i,600)rep(j,600)cost[i][j]=999999999;

        rep(i,m){

            int a,b;

            cin>>a>>b;

            a--,b--;

            cost[a][b] = cost[b][a] = 1;

        }

        rep(k,n){

            rep(i,n){

                rep(j,n){

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

                }

            }

        }

        int sum=0;

        rep(i,600){

            if(cost[0][i]<=2&&i!=0)sum++;

        }

        cout<<sum<<endl;

    }

}

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

using namespace std;



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



int n, m;

vector< vector< int > > adj;

vector< int > vis;



void dfs(int v, int d) {

	if (d == 3) return;

	vis[v] = 1;

	for (int u : adj[v]) dfs(u, d + 1);

}



int main() {

	while (cin >> n >> m, n) {	

		adj.assign(n, vector< int >());

		

		for_(i,0,m) {

			int a, b;

			cin >> a >> b;

			--a; --b;

			adj[a].push_back(b);

			adj[b].push_back(a);

		}

		

		vis.assign(n, 0);		

		dfs(0, 0);

		cout << accumulate(vis.begin(), vis.end(), int()) - 1 << endl;

	}

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

using namespace std;

const int m=110000;

int a,b,c[m],d[m];

int main()

{

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

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

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

    memset(d,0x3f,sizeof(d)),d[1]=0;

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

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

            d[i]=min(d[i],d[i-j]+abs(c[i]-c[i-j]));

    printf("%d",d[a]);

    return 0;

}

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

#include <fstream>

#define inf INFINITY

#define file freopen( "input.txt" , "r" , stdin) ; freopen( "output.txt" , "w" , stdout) ; 

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

#define _3102_ main()

#define endl '\n'

typedef long long ll ;

typedef unsigned long long ull ; 

typedef long double ld ; 

using namespace std ;

const int N = 1e5+100  ; 

const ll mod = 1e9 + 7  ;







int _3102_ {

    Gamed

    ll n , k , arr[N] , dp[N] ; 

    cin >> n >> k ; 

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

        cin >> arr[i] ; 

    }

    dp[0] = 0 ;  

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

        ll ans = inf , cnt = k; 

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

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

        }

        dp[i] = ans ; 

    }

    cout << dp[--n] << 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;

 inline int read(){

    int x=0,f=1; char ch=getchar();

    while(ch<'0' || ch>'9') {if(ch=='-') f=-1; ch=getchar();}

    while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}

    return x*f;

 }

 int N,Q,a[100010],b,c,cnt[100010];

 #define LL long long

 int main(){

 	N=read();

 	LL sum = 0;

 	for (int i=1; i<=N; i++) a[i]=read(),cnt[a[i]]++,sum+=a[i];

 	Q=read();

 	while (Q--){

 		b=read(),c=read();

 		sum -= 1LL*cnt[b]*b;

 		sum += 1LL*cnt[b]*c;

 		cnt[c] += cnt[b];

 		cnt[b] = 0;

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

 	}

 	return 0;

 }

 

 

 /*

4

1 2 3 4

3

1 2

3 4

2 4

 */

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

using namespace std;



int num[100050];



int main()

{

	long long sum=0;

	int N,Q,B,C,a;

	scanf("%d",&N);

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

		num[i]=0;

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

	{

		scanf("%d",&a);

		sum+=(long long)a;

		++num[a];

	}

	scanf("%d",&Q);

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

	{

		scanf("%d%d",&B,&C);

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

		num[C]+=num[B];

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

#include<algorithm>

#include<cmath>

#include<cstring>

#include<cstdlib>

#include<ctime>

#include<cstdio>

#include<string>

//#include<string>

//#include<sstream>

using namespace std;

int main()

{

	int i,k=0;

	char a[1000],b[1000];

	scanf("%s",&a);

	int n=strlen(a);

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

	{

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

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

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

		{

			if(k==0) continue;

			else k--;

		}

	}

	for(i=0;i<k;i++) printf("%c",b[i]);

	printf("\n");

}

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

#include <vector>

#include <string>

#include <cmath>

#include <set>

#include <stdio.h>

#include <algorithm>

#include <map>

#include <queue>

#include <unordered_map>

#define ll long long

#define pii pair <int, int>

#define pll pair <long long, long long>

#define plxy pair <long long, pll>

#define INT_MAX 1e18

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

using namespace std;

using namespace std;

const int mod=1e9+7;

const int N=1e5+5;

ll n,k,arr[N];

ll dp[N];

ll solve(ll idx)

{

    if(idx==n-1)

        return 0;

    if(dp[idx]!=-1)

        return dp[idx];

    ll choice=INT_MAX;

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

    {

        if(idx+i<n)

            choice=min(choice,solve(idx+i)+abs(arr[idx+i]-arr[idx]));

    }

    dp[idx]=choice;

    return dp[idx];

}

int main()

{

     Keep_calm_You_are_still_learning

     cin>>n>>k;

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

     {

         cin>>arr[i];

     }

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

     cout<<solve(0);

    return 0;

}


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

#define ull unsigned long long

#define ll long long

#define pb push_back

#define endl '\n'

#define line cout<<"----------------------"<<endl

#define mp make_pair

#define mod 1000000007

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

#define filein freopen("input.txt","r",stdin)

#define fileout freopen("output.txt","w",stdout)

using namespace std;

const ll  mx=300009;

ll  ara[mx];

ll  cost[mx];

int main()

{

    ll n,k;

    cin>>n>>k;

    for(ll  i=1;i<=n;i++) cin>>ara[i];

    cost[1]=0;

    cost[2]=abs(ara[2]-ara[1]);

    for(ll  i=3;i<=n;i++)

    {

        cost[i]=LLONG_MAX;

        for(ll  j=1;j<=min(i-1,k);j++)

        {

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

        }

    }

    cout<<cost[n]<<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 n,k;

int h[100100],f[100100];

int smallest ;

int main(){

	cin>>n>>k;

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

		cin>>h[i];

	}

	f[1]=0;

	f[2]=abs(h[2]-h[1]);

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

		smallest=2147483647;

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

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

		}

		f[i]=smallest;

	}

	cout<<f[n];

}


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

#include <algorithm>

#include <cmath>

using namespace std;



int h[(int)1e5], dp[(int)1e5], K, N;





int main()

{

	K = N = 0;

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

	{

		dp[j] = 1e9;

	}

	scanf("%d %d", &N, &K);

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

	{

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

	}

	dp[1] = 0;

	dp[2] = dp[1] + abs(h[1] - h[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(h[i] - h[i + j]));

		}

	}

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

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

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



int main() {

    int s;

    cin >> s;

    int a[2000000] = {};

    bool flag[10000] = {};

    a[0] = s;

    flag[a[0]] = true;

    int i = 1;

    while(1) {

        if (a[i - 1] % 2 == 0) a[i] = a[i - 1] / 2;

        else a[i] = a[i - 1] * 3 + 1;

        if (flag[a[i]]) {

            cout << i + 1 << endl;

            return 0;

        }

        flag[a[i]] = true;

        i++;

    }

}


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

using namespace std;

typedef long long ll;

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

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



int main() {

    int s;

    cin >> s;

    set<int> used;

    int a = s;

    while(used.count(a) == 0) {

        used.insert(a);

        if (a % 2 == 0) a /= 2;

        else a = a * 3 + 1;

    }

    cout << used.size() + 1 << endl;

    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 arr [100009];

int dp [100009];

int n,k;

int frog (int f_index)

{

if (f_index + 1 == n)return 0;

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

int s = INT_MAX;

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

{

if (f_index + i >= n)break;

s = min (s , frog (f_index + i) + abs (arr [f_index + i] - arr [f_index]));

}

return dp [f_index] = s ;

}

int main ()

{

cin >>n>>k;

for (int p = 0;p < 100009;p++)

{

dp [p] = -1;

}

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

{

cin >> arr [i];

}

cout <<frog (0)<<'\n';



}

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



#define  ll         long long

#define  llu        unsigned long long

#define  mod        1000000007

#define  pi         ((2.0)*acos(0.0))

#define  pb         push_back

#define  pf         push_front

#define  eb         emplace_back

#define  mp         make_pair

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

#define  X          first

#define  Y          second



/*loop*/

#define  fr(i,m,n)   for(ll i=m;i<n;++i)



/*output shortcuts*/

#define  nl         "\n"

#define  Yes        cout<<"Yes\n"

#define  yes        cout<<"yes\n"

#define  YES        cout<<"YES\n"

#define  No         cout<<"No\n"

#define  no         cout<<"no\n"

#define  NO         cout<<"NO\n"



ll a[10000000];





using namespace std;

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    ll n,k;

    cin>>n>>k;



    ll a[n];

    fr(i,0,n)

    cin>>a[i];



    ll dp[n];

    dp[n-1]=0;



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

    {

        ll p=LONG_MAX;



        fr(j,1,k+1)

        {

            if(i+j >= n)

                break;

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

            dp[i]=p;

        }

    }



    cout<<dp[0]<<nl;



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;





}