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



using namespace std;





int alpha[26];





int main()

{

    int n,m;

    cin>>m>>n;

    m-=1;

    for (int i=n-m;i<=n+m;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 <bits/stdc++.h>      

using namespace std;

const int N = 2e5 + 100;

#define pb push_back

#define mp make_pair

#define f first

#define s second

#define ull unsigned long long

#define ll long long

#define pii pair <int,int>

#define forn(l,i,n) for(int i = l;i <= n;++i)   

#define FOR(l,i,n) for(int i = l;i < n;++i)

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

#define pll pair <ll,ll>

const ll inf = 1e9 + 7;

const ll ll_inf = 1e17 + 100;   

const double pi = 3.14159265358979323;

inline void fr(){

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

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

}

void boost()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}

 

ll binpow (ll a, ll n)

{

    ll res = 1;

    while (n) {

        if (n & 1)

        {

            res *= a;

            res %= inf;

        }

        a *= a;

        n >>= 1;

        a %= inf;

    }

    return res;

}

        

int n;

int h[N];

int dp[N];

int k;

 

main()	

{                                                                        

	cin >> n >> k;

	dp[0] = inf;

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

	{

	  	cin >> h[i];

	}

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

	{

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

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

		{

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

		}

	}

	cout << dp[n];



}	 


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

#define ll long long

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

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

#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)

#define rrep(i,n) RREP(i,n-1,0)

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

const int inf = 1e9+7;

const ll longinf = 1LL<<60;

const ll mod = 1e9+7;



int main() {

  cin.tie(0);

  ios::sync_with_stdio(false);

  int N, Q;

  cin >> N;

  vector<ll> A(N);

  vector<ll> cnt(100005);

  rep(i, N) {

    cin >> A[i];

    cnt[A[i]]++;

  }

  ll sum = accumulate(all(A), 0LL);

  cin >> Q;

  vector<ll> B(Q), C(Q);

  rep(i, Q) cin >> B[i] >> C[i];

  rep(i, Q) {

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

    cnt[C[i]] += cnt[B[i]];

    cnt[B[i]] = 0;

    cout << sum << endl;

  }

  return 0;

}


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

using namespace std;

#define ll long long

#define ld long double

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

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

#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)

#define rrep(i,n) RREP(i,n-1,0)

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

const int inf = 1e9+7;

const ll longinf = 1LL<<60;

const ll mod = 1e9+7;

const ld eps = 1e-10;



int main() {

  cin.tie(0);

  ios::sync_with_stdio(false);

  int n; cin >> n;

  vector<ll> A(n);

  map<ll, int> mp;

  ll sum = 0;

  rep(i, n) {

    cin >> A[i];

    mp[A[i]]++;

    sum += A[i];

  }

  int q; cin >> q;

  while(q--) {

    ll b, c; cin >> b >> c;

    mp[c] += mp[b];

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

    mp[b] = 0;

    cout << sum << "\n";

  }

  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):
/*

Sharif Hasan - CSE, PUST

Apr 24, 2020 02: 14 PM

*/

#include<bits/stdc++.h>

#define br cout<<"\n"

#define what(x) cout<<"in here var= "<<x<<"\n";



/*STL definations*/

#define pb push_back



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

#define FROM(a,i,n) for(int i=a;i<n;i++)

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



using namespace std;



int cnt[100001]={-1};

/*Main function*/

int main()

{

    long long n;long long sm=0;

    cin>>n;

    long long arr[n],cnt[100001]={0};

    FOR(i,n){

        cin>>arr[i];

        ++cnt[arr[i]];

        sm+=arr[i];

    }

    long long k;

    cin>>k;

    //cout<<sm<<endl;

    FOR(i,k){

        long long p,q;

        cin>>p>>q;

        if(cnt[p]!=0){

            long long reduced=cnt[p]*p;

            long long add=cnt[p];

           // cout<<p<<" r= "<<reduced<<" "<<add;br;

            cnt[p]=0;

            cnt[q]+=add;

            long long nxt=add*q;

            sm-=reduced;

           // cout<<sm<<" sm \n";

            sm+=nxt;

        }

        cout<<sm;br;

        

    }

    

    return 0;

}


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

using namespace std;

int a[100005];

long long cnt[100005];

long long ans;

int main()

{

    int n;

	scanf("%d",&n);

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

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

    long long q;

    scanf("%lld",&q);

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

    {

    	int j,k;

    	cin>>j>>k;

    	ans+=(k-j)*cnt[j];

    	cnt[k]+=cnt[j];

    	cnt[j]=0;

		cout<<ans<<endl;

	}

	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>

#define ll long long

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

using namespace std;

const int N = 1e5+5, M=5e5+5, oo=0x3f3f3f3f, mod = 1e9+7;



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

int solve(ll ind){

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

    if(mem[ind] != -1) return mem[ind];

    ll ret = oo;

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

        if(ind+i < n)

            ret = min(ret, solve(ind+i)+abs(arr[ind]-arr[ind+i]));

        else

            break;

    }

    return mem[ind] = ret;



}

int main()

{

    _69e27

    memset(mem, -1, sizeof mem);

    cin >> n >> k;

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

        cin >> arr[i];

    cout << solve(0);



    return 0;

}




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

#define ll long long

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

using namespace std;

const int N = 1e5+5, M=1e5+5, oo=0x3f3f3f3f, mod = 1e9+7;



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

int main()

{

    _69e27

    memset(dp, oo, sizeof dp);

    dp[0] = 0;

    cin >> n >> k;

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

        cin >> arr[i];

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

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

            if(j < n)

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

        }

    }

    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;



#define MAXC 1000000007



int n,k;

int h[100005];

int dp[100005];



int solve(int x){

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

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

    else {

        int aux = MAXC;

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

            if (x+i < n){

                aux = min(aux,abs(h[x]-h[x+i])+solve(x+i));

            }

        }

        return dp[x] = aux;

    }

}



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

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin >> n >> k;

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

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

        cin >> h[i];

    }

    cout << solve(0) << endl;

    return 0;

}

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



using namespace std;



#define endl '\n'



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);



  int n, k;

  cin >> n >> k;

  vector<int> stones(n);

  vector<int> dp(n, 1e9);

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

    cin >> stones[i];

  }

  dp[0] = 0;

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

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

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

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

using namespace std;



int main() {

    int k,x; cin>>k>>x;

    if (k==1){

        cout<<x<<endl;

        return 0;

    }

    int n = k - 1;

    int m = k - 1;

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

        cout<<x-n;

        cout<<" ";

        n--;

    }

    cout<<x<<" ";

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

        x++;

        cout<<x<<" ";

    }

}

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

#include <vector>

using namespace std;



int main( void )

{

	// prime numbers table

	vector <bool> isPrime( 60000 + 1, false );

	isPrime[2] = true;

	for ( int i = 3; i < 60000; i += 2 )

	{

		isPrime[i] = true;

	}



	for ( int i = 3; i < 60000; i += 2 )

	{

		for ( int j = i + i; j < 60000; j += i )

		{

			isPrime[j] = false;

		}

	}



	vector <int> primes;

	primes.push_back( 2 );

	for ( int i = 3; i < 60000; i += 2 )

	{

		if ( isPrime[i] )

			primes.push_back( i );

	}



	int n;

	while ( cin >> n && n != 0 )

	{

		int kotae = 0;

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

		{

			if ( primes[i] <= n / 2 )

			{

				if ( isPrime.at( n-primes[i] ) )

					kotae++;

			}

		}

		cout << kotae << endl;

	}



}

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

#include <vector>

using namespace std;



int main( void )

{

	vector <bool> isPrime( 50001 + 1 );

	isPrime[2] = true;

	for ( int i = 3; i < 50001; i += 2 )

	{

		isPrime[i] = true;

	}



	for ( int i = 3; i < 50001; i += 2 )

	{

		for ( int j = i + i; j < 50001; j += i )

		{

			isPrime[j] = false;

		}

	}



	int cnt = 1;

	vector <int> primes( 5500 );

	primes[0] = 2;

	for ( int i = 3; i < 50001; i += 2 )

	{

		if ( isPrime[i] )

		{

			primes[cnt] = i;

			cnt++;

		}

	}



	int n;

	while ( cin >> n && n != 0 )

	{

		int kotae = 0;

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

		{

			if ( primes[i] <= n / 2 )

			{

				if ( isPrime.at( n-primes[i] ) )

					kotae++;

			}

		}

		cout << kotae << endl;

	}



	return 0;

}

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

using namespace std;



int main()

{

  bool is_prime[100000];

  is_prime[0] = is_prime[1] = false;

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

    is_prime[i] = true;

  }



  for(int i=2; i*i<100000; i++){

    if(is_prime[i]){

      for(int j=i*i; j<100000; j+=i){

        is_prime[j] = false;

      }

    }

  }



  while(1){

    int n;

    cin >> n;



    if(n == 0)

      break;



    int cnt=0;



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

      if(is_prime[i] && is_prime[n-i])

        if(i+(n-i) == n) cnt++;

    }



    cout << cnt << endl;

  }

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

#include<cstring>

using namespace std;

int main() 

{

    int k;

    char S[110];

    cin>>k>>S;

    int l=strlen(S);

    if(l>k) 

    {

        S[k]='.';

        S[k+1]='.';

        S[k+2]='.';

        S[k+3]='\0';

    }

    cout<<S<<endl;

    return 0;

}

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



int main(){

	int k;

	char word[100];

	scanf("%d", &k);

	scanf("%s", &word);

	

	int len = 0;

	

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

		len++;

	}

	

	if(len <= k){

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

	}

	else{

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

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

		}

		printf("...\n");

	}

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

#define Mod 1000000007

using namespace std;

int main()

{

	long long a;

	long long sum, i;

	while (scanf("%lld", &a) != EOF)

	{

		sum = 1;

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

		{

			sum *= i;

			if (sum >= Mod)

			{

				sum %= Mod;

			}

		}

		cout << sum << endl;

	}

	return 0;

}

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

#include <cmath>

#define m 1000000007

int main()

{

	long long sum;

	int n;

	while(~scanf("%d",&n)&&n)

	{

		sum=1;

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

		{

			sum*=i;

			sum=sum%m;

		}

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

	}

}

// 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):
/*

** author:mahmoud magdy

*/

#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define endl "\n"



inline int D()

{

    int m ;

    cin>>m;

    return m ;

}

inline ll lD()

{

    ll m ;

    cin>>m;

    return m ;

}

inline double dD()

{

    double m;

    cin>>m;

    return m;

}

void fast()

{

    ios_base :: sync_with_stdio( 0 ) ;

    cin.tie( 0 ) ;

    cout.tie( 0 ) ;

}

int a[100005];

int dp[100005];

int n , k;

int solve(int i){

 if(i>=n)return 1e9;

 if(i==n-1)

    return 0;

 if(dp[i]!=-1)

      return dp[i];

 int mn=1e9;

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

 int ch1=abs(a[i]-a[i+j])+solve(i+j);

 mn=min(ch1,mn);

 }

 return dp[i]=mn;

}

int main()

{

    fast();

    n=D();k=D();

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

        a[i]=D();

    }

    memset(dp,-1,sizeof dp);

    cout<<solve(0)<<endl;

}


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

using namespace std;



int main() {

	int n, k; cin >> n >> k;

    vector<int> alturas(n);

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

    vector<int> dp(n,1000000009); dp[0] = 0;

    int values[k];

    for(int i = 1; i <=k; i++) values[i-1] = i;

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

        for(int x: values){

            if(i+x >= n) break;

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

        }

    }

    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 nax = 1e5+103;

int n, k;

int dp[nax];

int a[nax];

int f(int i){

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

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

	int ans = abs(a[i]-a[i+1])+f(i+1);

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

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

	}

	return dp[i]=ans;

}

int main(){

	cin>>n>>k;

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

	memset(dp, -1, sizeof dp);

	cout<<f(0)<<endl;

	return 0;

}

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

using namespace std;







int main() {

    ios_base::sync_with_stdio(false);



    long long int a,b,c,d,i,j,k,n,m;

    cin>>n>>k;

    long long int ar[n+1],dp[n+1];

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

        cin>>ar[i];

     dp[0]=0;

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

        dp[i]=INT_MAX;

    //dp[1]=abs(ar[1]-ar[0]);

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

    {

        //a=INT_MAX;

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

        {

           c=i+j;

           dp[c]=min(dp[c],(dp[i]+abs(ar[i]-ar[c])));

           //cout<<i<<" "<<c<<" "<<dp[0]<<" "<<dp[c]<<"\n";

        }





    }

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





}