stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int m1=110000;

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

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;

}

// Faster C++ solution for the same problem (measured speedup: 1.409456177218206x):
#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;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

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

#define     bug(a,b)        cout<<a<<"\t"<<b<<"\n";

#define     Case(a,b)       cout<<"Case "<<a<<": "<<b<<"\n";

#define     watch(a)        cout<<fixed<<setprecision(a)

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

#define     Endl            "\n";

#define     tab             "\t"

#define     reset(a,b)      memset(a,b,sizeof(a));

#define     sf(a)           scanf(" %d", &a);

#define     srt(a)          sort(a.begin(),a.end());

#define     pi              2*acos(0.0)

#define     inf             INT_MAX

const int sz=1e5+9;

int n,x,arr[sz],dp[sz],k;



int clac(int i){

    if(i==n) return 0;

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

    int ret1=inf;

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

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

    }

    return dp[i]=ret1;

}



void solve(){

    cin>>n>>k;

    reset(dp,-1);

    lp(i,1,n+1)cin>>arr[i];

    cout<<clac(1)<<"\n";

}



int main(){

    //FasterIO;

    #ifndef ONLINE_JUDGE

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

    #endif

    // int t;

    //cin>>t;

    //while(t--)

        solve();

}


// Faster C++ solution for the same problem (measured speedup: 1.3807016045927996x):
#include <bits/stdc++.h>

/**

*   author: j o s e m a g o

*   date:

**/

using namespace std;

#pragma GCC optimize ("Ofast")

#define f first

#define s second

#define mp make_pair

#define lb lower_bound

#define ub upper_bound

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

#define mset(a,v) memset(a,v,sizeof(a))

#define pb push_back

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

#define rsz resize

#define ln "\n"



typedef double db;

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

const ld PI = 2*acos(0.0);

const int MOD = 1000000007;

const int JJJ = 100009;









int main(){



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

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



    ios_base::sync_with_stdio(0); cin.tie(0);



    int N,k;

    cin >> N >> k;

    vector<int>h(N+k+1);

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

        cin >> h[i];

    }

    vector<int>dp(N+k+1,INT_MAX);

    dp[0] = 0;

    for(int i = 1;i < k;i++) dp[i] = abs(h[i]-h[0]);

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

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

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

        }

    }



//    for(int i = 0;i < N;i++) cerr<<dp[i]<<" ";

    cout<<dp[N-1];









    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

     

using namespace std;



int k,n,h[110000],f[110000];

int main(){

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

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

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

	}

	memset(f,63,sizeof(f));

	f[1]=0;

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

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

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

		}

	}

	printf("%d",f[n]);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4286798529790607x):
#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;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main()

{

	long long int i,j,k,n,m,x,y,z,a[100000],ans=0,b[100000]={0};

	cin>>n>>k;

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

	{

		cin>>a[i];

	}

	z=0;

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

	{

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

		{

			x=a[i]-a[j];

			if(x<0)

			x=x*(-1);

			x=x+b[j];

			if(j==(i-1))

			b[i]=x;

			else

			b[i]=min(b[i],x);

		}

	}

	cout<<b[n-1];

}

// Faster C++ solution for the same problem (measured speedup: 1.803855437234941x):
#include<bits/stdc++.h>

using namespace std;

#define max1 1000005

#define siz 100000

#define mod 998244353

#define inf 1e18+5

#define ll long long int

#define debug(x) cout<<#x<<" "<<x<<endl

#define jam(x) cout<<"Case #"<<x<<": "

typedef pair<ll,ll> pr;

int main()

{

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);

  cout.tie(NULL);

  /*#ifndef ONLINE_JUDGE

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

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

  #endif*/

  ll n,k;

  cin>>n>>k;

  vector<ll> a(n);

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

  {

    cin>>a[i];

  }

  vector<ll> dp(n,inf);

  ll k1=k;

  dp[0]=0;

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

  {

    for(ll j=i-1;(j>=0&&k1>0);k1--,j--)

    {

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

    }

    k1=k;

  }

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

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<iostream>

#include<cstring> 

#include<algorithm>

using namespace std;

typedef long long ll;

int tree[300000+10];

int a[100000+10];

int c[100000+10];

int n;

int lowbit(int x){

	return x&-x;

}

void update(int x){

	while(x<=200020){

		tree[x]++;

		x+=lowbit(x);

	}

}

ll query(int x){

	ll res=0;

	while(x>0){

		res+=tree[x];

		x-=lowbit(x);

	}

	return res;

}

bool check(int x){

	c[0]=0;

	for(int i=1;i<=n;i++) c[i]=c[i-1]+(a[i]>=x ? 1:-1);

	memset(tree,0,sizeof(tree));

	ll sum=0;

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

		sum+=query(c[i]+100010);

		update(c[i]+100010);

	}

	return sum>=1ll*n*(n+1)/4;

}

int main(){

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

		int l=0,r=-1000000000;

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

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

			r=max(a[i],r);

		}

//		printf("%d %d\n",l,r);

		while(l<=r){

			int mid=(l+r)>>1;

			if(check(mid)) l=mid+1; else r=mid-1;

		}

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

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 1.670795649868662x):
#include<bits/stdc++.h> //Ithea Myse Valgulious

namespace chtholly{

typedef long long ll;

#define re0 register int

#define rel register ll

#define rec register char

#define gc getchar

#define pc putchar

#define p32 pc(' ')

#define pl puts("")

/*By Citrus*/

inline int read(){

  int x=0,f=1;char c=gc();

  for (;!isdigit(c);c=gc()) f^=c=='-';

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return f?x:-x;

  }

template <typename mitsuha>

inline bool read(mitsuha &x){

  x=0;int f=1;char c=gc();

  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';

  if (!~c) return 0;

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return x=f?x:-x,1;

  }

template <typename mitsuha>

inline int write(mitsuha x){

  if (!x) return 0&pc(48);

  if (x<0) pc('-'),x=-x;

  int bit[20],i,p=0;

  for (;x;x/=10) bit[++p]=x%10;

  for (i=p;i;--i) pc(bit[i]+48);

  return 0;

  }

inline char fuhao(){

  char c=gc();

  for (;isspace(c);c=gc());

  return c;

  }

}using namespace chtholly;

using namespace std;

const int yuzu=1e5;

typedef int fuko[yuzu<<1|10];

struct _bit{

fuko c;

void init(){memset(c,0,sizeof c);}

void add(int x){for (;x<=yuzu<<1;x+=x&-x) c[x]++;}

int query(int x){

  int llx=0;

  for (;x;x&=x-1) llx+=c[x]; 

  return llx;

  }

}my_;

fuko b,a;

bool judge(int x,int n,int i=1){

my_.init();

for (;i<=n;++i) b[i]=b[i-1]+(a[i]>=x?1:-1);

ll llx=0;

for (i=0;i<=n;++i) llx+=my_.query(b[i]+yuzu),my_.add(b[i]+yuzu);

return llx>=1ll*n*(n+1)/4;

}

int main(){

int i,n=read(),l=0,r=0;

for (i=1;i<=n;++i) r=max(a[i]=read(),r);

for (;l<=r;){

  int mid=l+r>>1;

  judge(mid,n)?l=mid+1:r=mid-1;

  }

write(l-1);

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define ull unsigned long long

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define endl '\n'

#define main() int main()

#define ff1(i,n) for(int i=1;i<=n;++i)

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

#define pi acos(-1.0)

#define ms(a, x) memset(a, x, sizeof(a))

#define lowbit(x) ((x)&(-(x)))

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define fir first

#define sec second

#define debug(x) cout<<x<<'\n'

#define sa(a) scanf("%d", &a)

#define sab(a, b) scanf("%d%d", &a, &b)

#define pa(a) printf("%d",a)

#define pab(a,b) printf("%d%d",a,b)

#define pii pair<string,int>

using namespace std;



#define maxn 100010

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

int max(int x,int y){if(x>y) return x;return y;}

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

    while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; }

    return ans%mod;

}

main(){

	IO;

    int n,k;

	cin>>n>>k;

	vector<int> a(n+1);

	ff1(i,n) cin>>a[i];

	int dp[n+1];

	ms(dp,inf);

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

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

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

			if(i-j>=1){

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

			}

		}

	}

	cout<<dp[n]<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 5.338973041582588x):
#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;

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

using namespace std;



int SW( int n, int k ){

    int h[n+1], custo[n+1];

    int menor = (1 << 30);

    

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

        cin >> h[i]; 

    }

    

    custo[1] = 0;

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



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

        menor = (1 << 30);

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

            if( i-j>0 ){

            custo[i]=custo[i-j] + abs(h[i]-h[i-j]);

            }

            if( custo[i]<menor ){

                menor = custo[i];

            }

        }

        custo[i] = menor;

    }



    return custo[n];

}





int main() {

int n,k;



cin >> n >> k;



cout << SW(n,k) << endl;



}

// Faster C++ solution for the same problem (measured speedup: 5.224725657774321x):
#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;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

using ll=long long;

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



int main(){

   int N;

   cin>>N;

   vector<int> A(1000000,0),count(1000001,0);

   A.at(0)=N;

   for(int i=1;i<A.size();i++){

       count.at(A.at(i-1))++;

       if(count.at(A.at(i-1))==2){

           cout<<i<<endl;

           return 0;

       }

       else if(A.at(i-1)%2==0){

           A.at(i)=A.at(i-1)/2;

       }

       else {

           A.at(i)=3*A.at(i-1)+1;

       }

   }

}    

// Faster C++ solution for the same problem (measured speedup: 9.168688574815418x):
#pragma GCC optimize("O3")

#include<bits/stdc++.h> 

using namespace std;

using ll=long long;

typedef pair<ll,ll> P;

#define fi first

#define se second

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

const ll inf=(1e18);

const ll mod=1000000007;

ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}

ll lcm(ll c,ll d){return c/gcd(c,d)*d;}

struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;

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

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

map<ll,int> mp;

int main(){

ll n;

cin>>n;

mp[n]++;

ll cnt=1;

ll t;

while(1){

  if(n%2==0)t=n/2;

  else t=3*n+1;

  cnt++;

  if(mp[t]){

       cout<<cnt<<endl;

       return 0;

  }

  mp[t]++;

  n=t;

}

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define ll long long

#define MX 100005



const ll INF = 1e12;



int A[MX];

ll dp[MX];

int n, k;



ll recur(int pos){

    if (pos == 1) return dp[pos] = 0;

    if (pos == 2) return dp[pos] = abs(A[2] - A[1]);

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



    ll mn = INF;

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

        if (pos - i >= 1){

            mn = min(mn, recur(pos - i) + abs(A[pos] - A[pos - i]));

        }

        else break;

    }

    return dp[pos] = mn;

}

int main()

{

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

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

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

        dp[i] = -1;

    }



    ll ans = recur(n);

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

    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 4.545232376073202x):
#include<cstdio>

#include<cmath>

#include<algorithm>

using namespace std;

const int maxn=1e5 + 5;

int main()

{

	int dp[maxn];

	int h[maxn];

	int n,k;

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

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

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

		dp[1] = 0;

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

	    	dp[i]=1e9;

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

				if(i==j) break;

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

				}

	}

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

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>



using namespace std;

typedef long long int ll;

ll dp[100010];

ll arr[100010];



int main(){

    ll n,k;

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



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

        scanf("%lld",arr+i);

    }



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

        dp[i] = 10000000000000;

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

            if(i-j >= 1){

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

            }

        }

    }





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

}


// Faster C++ solution for the same problem (measured speedup: 1.6607715819325666x):
#include <cstdio>

#include <cstring>



using namespace std;



const int maxn = 1e5+5;

int n,K;

int h[maxn],f[maxn];



int abs(int x,int y) {

	if (x - y < 0) return y - x;

	return x - y;

}



int min(int x,int y) {

	if (x < y) return x;

	return y;

}



int main() {

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

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

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

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

	}

	f[0] = f[1] = 0;

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

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

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

		}

	}

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

	return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

int main()

{

    long long int n, m = 1;

    cin >> n;

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

    {

        m = m * i % 1000000007;

    }

    cout << m;

}

// Faster C++ solution for the same problem (measured speedup: 3.5201807924513635x):
#include<stdio.h>

#include<math.h>

int main(){

	int i,m;

	long long n=1;

	scanf("%d",&m);

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

		n=n*i;

		n=n%1000000007;

	}

		printf("%d",n);

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

#include <map>

#include <cmath>

#include <queue>

#include <numeric>

#include <climits>

#include <iterator>

#include <iomanip>

#include <stack>

#include <bitset>

#include <functional>

using namespace std;

const constexpr int INF = 1e9;

//typedef std::pair<std::string,double> P;





#define FOR(i, a, n) for (ll i = (ll)a; i<(ll)n; ++i)

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



typedef long long ll;

typedef vector<int> VI;

const constexpr ll MOD = 10000000000+7;

vector<pair<int, int> > vp;

 

struct Less {

    bool operator()(const pair<int, int>& x, const pair<int, int>& y) const {

        return x.first > y.first;

    }

};



ll GCD(ll a, ll b){

    if(b==0) return a;

    return GCD(b, a%b);

}





//グラフの隣接リスト

VI g[200010];

//頂点の入次数を管理

int h[100010];

ll N, K;

string s;

ll v[51];



int main(void) {

    cin >> N >> K;

    

    ll cnt=0;

    if(K==0) cout << N*N << endl;

    else {

        for(ll i=K+1; i<=N; ++i){

        cnt += N/i*max(ll(0), i-K) + max(ll(0), N%i-K+1);

        }

        cout << cnt << endl;

    }

    

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 6.4416566305405345x):
#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

using namespace std;

using ll = long long;

//西暦⇒時代の歴

ll a[100010];

int main()

{

    ll N, K;

    cin >> N >> K;



    if (K == 0)

    {

        cout << N * N << endl;

        return 0;

    }

    ll ans = 0;

    for (ll i = K + 1; i <= N; ++i)

    {

        // aは適当な数字に固定 b = i;

        ans += N / i * (i - K);

        ans += max(0LL, (N % i) - K + 1);

    }

    cout << ans << endl;

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstring>

using namespace std;

char s[105],ans[105];

int main()

{

	cin>>s;

	int len=strlen(s),tot=0;

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

	{

		if(s[i]=='0')	ans[++tot]='0';

		if(s[i]=='B'&&tot>0)	tot--;

		if(s[i]=='1')	ans[++tot]='1';

	}

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

	{

		cout<<ans[i];

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.267574731116816x):
#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;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define Test int t;cin>>t;while(t--)

#define PI acos(-1)

#define endl "\n"

#define fx(x) fixed<<setprecision(x)

#define sz(s) s.size()

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

#define allr(v) (v).rbegin(),(v).rend()

const double eps=1e-9;

const int mod=1e9+7;

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

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

ll gcd(ll x,ll y) {return(!y)?x:gcd(y,x%y);}

ll lcm(ll x,ll y) {return((x/gcd(x,y))*y);}

using namespace std;

void fast(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

}

int a[100102];

int dp[100102];

int n,k;

int solve(int idx){

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

    if(idx+k>=n)return abs(a[idx]-a[n-1]);

    int &ret=dp[idx];

    if(ret!=-1)return ret;

    int mn=INT_MAX;

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

        if(idx+i<n)mn=min(mn,solve(idx+i)+abs(a[idx]-a[idx+i]));

    }

    ret=mn;

    return ret;

}

int main()

{

    cin>>n>>k;

    memset(dp,-1,sizeof dp);

    memset(a,1000000000,sizeof a);

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

        cin>>a[i];

    }

    cout<<solve(0);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.7633430797113423x):
#include <bits/stdc++.h>

#define f first

#define s second

#define pb push_back

#define ENDL '\n'

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

#define sz(a) (int)(a.size())

#define deb(a) cout << #a << ": " << a << ENDL

#define fore(i, a, b) for(int i(a), ThkMk(b); i < ThkMk; ++i)

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



using namespace std;



typedef long long lli;

typedef long double ld;

typedef pair<lli, lli> ii;

typedef vector<lli> vi;



int main()

{   _

    lli n, k; cin >> n >> k;

    vi v(n); fore(i, 0, n) cin >> v[i];

    vector<lli> dp(n, 1e18);

    dp[0] = 0;

    fore(i, 0, n) {

        fore(j, i + 1, min(n, i + k + 1)) {

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

        }

    }

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

    return 0;

}