stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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;

}

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

#include <algorithm>

#include <vector>

using namespace std;

 

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

int main(){

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

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

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

	}

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

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

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

			dp[i]=min(dp[i],dp[j]+abs(a[i]-a[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>

#include <algorithm>

#include <bits/stdc++.h>

using namespace std;



int main() {

  int s; cin >> s;

  vector<int> flag(1000000, 0);

  flag[s] = 1;

  bool f = false;

  int cnt = 1;

  while(!f) {

    cnt++;

    if (s%2 == 0) {

      if (flag[s/2] == 1) {

        f = true;

      } else {

        flag[s/2] = 1;

        s = s/2;

      }

    } else {

      if (flag[s*3+1] == 1) {

        f = true;

      } else {

        flag[s*3+1] = 1;

        s = s*3+1;

      }

    }

  }

  cout << cnt << endl;

  return 0;

}

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

#include <algorithm>

#include <bits/stdc++.h>

using namespace std;



int main() {

  int s; cin >> s;

  set<int> st;

  int x = s;

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

    st.insert(x);

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

    else x = x*3+1;

  }

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

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

#include<string>

using namespace std;

int main() {

	int g = 0;

	string s, r;

		cin >> s;

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

			if (s[i] == '0' || s[i] == '1') {

				r += (s[i]);

				g++;

			}

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

				if (!r.empty()) {

					r.erase(g - 1, 1);

					g--;

				}

			}



				

		}

		if (!r.empty())

		cout << r << endl;



	return 0;

}

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

#include<string.h>



int main(){

	char str[15];

	scanf("%s",&str);

	char temp[15];

	int index=0,len=strlen(str);

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

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

			temp[index] = '0';

			//printf("index[%d] : %s\n",index,temp);

			index++;

		} 

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

			temp[index] = '1';

			//printf("index[%d] : %s\n",index,temp);

			index++;

		} 				//01B0 temp =     --0

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

			//printf("index[%d] : %s\n",index,temp);

			if(index != 0) index--;

		}

	}

	//printf("%d\n",index);

	temp[index]='\0';

	printf("%s\n",&temp);

	

	

	

	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,m;

   cin>>n>>m;

   const int N = n*2 -1;

   int arr[N];

   arr[0]=m;

   int j=1;



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

       arr[j++]=i;

   }



    for(int i=m-1;i>m-n;i--){

        arr[j++]=i;

    }





    sort(arr,arr+j);



    for(int i=0;i<j;i++)cout<<arr[i]<<" ";



    cout<<endl;

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

#include <cmath>

#include <cstring>

#include <queue>

#include <iostream>

#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 1009;

const double PI = acos(-1.0);



LL mod = 1e9+7;

int main()

{

    int a;

    cin >> a;

    LL sum = 1;

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

        sum = (sum*i)%mod;

    cout << sum << endl;

}


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

#include <algorithm>

using namespace std;



int main()

{

	int n,i;

	long long sum=1;

	scanf("%d",&n);

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

	{

		sum*=i;

		sum%=(1000000000+7);

	}

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

	return 0;

}


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

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

using namespace std;

using ll = long long;

using P = pair<ll, ll>;

const string ln = "\n";

constexpr int INF = 1001001001;

constexpr int MOD = 1000000007;



int main() {

  cin.tie(nullptr);

  ios::sync_with_stdio(false);



  int n;

  cin >> n;



  ll ans = 1;



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

    ans *= i % MOD;

    ans %= MOD;

  }



  cout << ans << ln;



  return 0;

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

using namespace std;

const int nax = 1e5+103;

int number, k;

int dyn[nax];

int a[nax];

int op1(int i){

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

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

	int res = abs(a[i]-a[i+1])+op1(i+1);

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

		if(i+j<number) res = min(res, abs(a[i]-a[i+j])+op1(i+j));

	}

	return dyn[i]=res;

}

int main(){

	cin>>number>>k;

	for(int i=0; i<number; i++) cin>>a[i];

	memset(dyn, -1, sizeof dyn);

	cout<<op1(0)<<endl;

	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>

#define MAX 100005



using namespace std;



int h[MAX];

int minimum[MAX];



int n, k;



int main(){

    cin>>n>>k;

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

        cin>>h[i];

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

    minimum[0]=0;

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

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

                //cout<<"v";

                if(minimum[i+j]<0) minimum[i+j] = minimum[i]+abs(h[i]-h[i+j]);

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

        }

    }

    cout<<minimum[n-1]<<"\n";

    return 0;

}

/*

4

10 30 40 20

0  20 30 30



4

10 50 10 10

*/




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

using namespace std;

int h[100001];

int dp[100001];

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

    int n, K;

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

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

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

    dp[0] = 0;

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

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

        for(int k=2;k<=K;++k)

            if(i >= k)

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

    }

    printf("%d\n", 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 <iostream>

#include <algorithm>



using namespace std;



int N, A, B, used[100], rings[100], G[100][199];



int rec(int pos)

{

	int ret = used[pos] = 1;



	for(int i = 0; i < rings[pos]; i++)

	{

		if(!used[G[pos][i]])

		{

			ret = max(ret, rec(G[pos][i]) + 1);

		}

	}



	used[pos] = 0;



	return ret;

}



int main()

{

	int N, A, B;



	while(true)

	{

		scanf("%d", &N);

 

		if(N == 0) { break; }



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

		{

			used[i] = rings[i] = 0;

		}



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

		{

			scanf("%d", &A); A--;

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



			G[A][rings[A]] = B; rings[A]++;

			G[B][rings[B]] = A; rings[B]++;

		}



		int nodes = 0;



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

		{

			if(rings[i])

			{

				nodes++;

			}

		}



		int ret = 0;



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

		{

			ret = max(ret, rec(i));



 			if(ret == nodes) break;

		}

 

		printf("%d\n", ret);

	}



	return 0;

}

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

#include <algorithm>

#pragma warning(disable : 4996)

using namespace std;

int N, A, B, v[100], r[100], G[100][199];

int rec(int pos) {

	int ret = 0; v[pos] = 1;

	for (int i = 0; i < r[pos]; i++) {

		if (!v[G[pos][i]]) ret = max(ret, rec(G[pos][i]));

	}

	v[pos] = 0;

	return ret + 1;

}

int main() {

	while (scanf("%d", &N), N) {

		for (int i = 0; i < 100; i++) r[i] = 0;

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

			scanf("%d%d", &A, &B), A--, B--;

			G[A][r[A]++] = B, G[B][r[B]++] = A;

		}

		int ret = 0;

		for (int i = 0; i < 100; i++) ret = max(ret, rec(i));

		printf("%d\n", ret);

	}

	return 0;

}

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

#include <set>

#include <cstring>

#include <vector>

using namespace std;



//int node[101][101];

int visit[101];

int ans,last;

int die[101];

int flag;



void dfs(int now, int cnt, int *st, vector<vector<int> >& vv){

	if( cnt>ans ){

		ans=cnt;

		last = now;

		flag = 1;

	}

	for(int next=0; next<vv[now].size(); next++)if( !visit[vv[now][next]] ){

		int k = vv[now][next];

		visit[k] = die[k] = 1;

		st[cnt] = k;

		dfs(k,cnt+1,st,vv);

		visit[k] = 0;

	}

	return ;

}



int main(){

	int n;

	while(cin>>n,n){

		set<int> s;

		vector<vector<int> > vv(101);

		ans = 0;

		//memset(node,0,sizeof(node));

		memset(visit,0,sizeof(visit));

		memset(die,0,sizeof(die));

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

			int a,b; cin>>a>>b;

			//node[a][b] = node[b][a] = 1;

			vv[a].push_back(b);

			vv[b].push_back(a);

			s.insert(a);

			s.insert(b);

		}

		int tmp[101];

		/*

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

			printf("vv[%d].size = %d\n",i,vv[i].size());

			for(int j=0; j<vv[i].size(); j++)

				printf("%d ",vv[i][j]);

			puts("");

		}

		*/

		for(set<int>::iterator it=s.begin(); it!=s.end(); it++)if( !die[*it] ){

			flag = 0;

			

			visit[*it]=1;

			tmp[0] = *it;

			dfs(*it,1,tmp,vv);

			visit[*it]=0;

			

			die[last] = 0;

			//printf("*it:%d\n",*it);



		}

		printf("%d\n",ans);

	}

	return 0;

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

using namespace std;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }



const long long int INF = 1LL << 60;



int main(){

    long long int N, K; cin >> N >> K;

    long long int h[110000]; for (int i=0; i<N; i++) cin >> h[i];

    long long int dp[110000]; for (int i=0; i<110000; i++) dp[i] = INF;



    dp[0] = 0;



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

        for (int j=0; j<=K; j++) {

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

        }

    }



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

}

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

#include <vector>



const int INF = 2100000000;



int main() {

  auto min = [](int a, int b) { return (a < b ? a : b); };

  int N, K;

  std::cin >> N >> K;

  std::vector<int> h(N + 1);

  for (int i = 1; i <= N; i++) std::cin >> h[i];



  // dp[i] := 足場 i に移動するのに要するコスト

  std::vector<int> dp(N + K + 1, INF);

  dp[0] = 0;

  dp[1] = 0;



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

    }

  }

  std::cout << dp[N] << std::endl;

}

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

using namespace std;

#define f first

#define s second

#define ll long long

#define ld long double

#define pii pair <int,int>

#define pll pair <ll,ll>

#define pil pair <int,ll>

#define pli pair <ll,int>

#define ull unsigned long long

#define pdd pair <double,double>

#define pb push_back

#define mp make_pair

#define puu pair<ull,ull>

#define pvv pair<vector<int>, vector<int> >

#define _bp __builtin_popcount

#define ptt pair <Tree*,Tree*>

#define all(v) v.begin(),v.end();

const long double pi = acos(-1);

void boos()

{

     ios_base :: sync_with_stdio(false);

     cin.tie(0);

     cout.tie(0);

}

void file()

{

   freopen(".in","r",stdin);

   freopen(".out","w",stdout);

}

const ll INF = 1e9 + 7;

const ll INF1 = 998244353;

const ll LLINF = (ll)1e18;

const int  INTmx = (int)1e9;

const int N = 2e5 + 100;

ll dp[N];

ll a[N];

int main()

{

    //file();

    boos();

    int n,k;

    cin >> n >> k;

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

    {

        cin >> a[i];

        dp[i] = LLINF;

    }

    dp[1] = 0;

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

    {

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

        {

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

        }

    }

    cout << dp[n];

}

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

#define inf 2000000000

int n,k;

int h[100002]={100000},f[100002]={};

int min(int a,int b)

{

	return a<b? a:b;

}

int max(int a,int b)

{

	return a>b? a:b;

}

int abs(int x)

{

	return x<0? -x:x;

}

int main()

{

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

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

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

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

	{

		f[i]=inf;

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

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

	}

	printf("%d",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 <bits/stdc++.h>

using namespace std;



const int N = 1e6+5 , inf = 1e9+5;



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



main(){

  cin >> n >> k;

  for(int i = 0 ; i < N ; i++) dp[i] = inf;

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

  dp[1] = 0;

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

  cout << dp[n] << "\n";

}

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

using namespace std;



const int N = 1e5+5 , inf = 0x3f3f3f3f;



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



int main(){

  cin >> n >> k;

  for(int i = 2 ; i < N ; i++) dp[i] = inf;

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

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

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

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

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





}