stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

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

}
// Slower C++ solution:
#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];

}

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

}
// Slower C++ solution:
#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";

}

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

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

using namespace std;





int main()

{

    int n,k;

    cin>>n>>k;



    int ar[n+10],ans[n+10];



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

    {

        cin>>ar[i];

        ans[i]=INT_MAX;

    }



    ans[0]=0;





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

    {

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

        {

            int r1,r2;



            r1=abs(ar[j]-ar[i])+ans[i];

            ans[j]=min(r1,ans[j]);

        }



    }



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



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.228856760515016x):
//才不露白

//还是青蛙跳还是动态规划

//唯一的是你不知道青蛙它从哪跳到了现在的石头上 如果你知道 那就好办了

//比如从k上面跳过来

//dp[i] = dp[k] + abs(h[i] - h[k])

//关键是你k不知道啊

//有想法就好 不用先急着自我否认

//枚举

//dp[1] = 0

//dp[2] = abs(h[2] - h[1])

//写个函数来算看起来不错

//dp[3] = min(从1跳过来,从2跳过来)

//dp[4] = min(从1跳,从2跳，从3跳)用循环 最后求min

//差不多2层吧 先写了试试看

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <queue>

#include <stack>

#include <vector>

#include <algorithm>



using namespace std;



const int MAXN = 1e5;

int n,k;

int h[MAXN + 5],dp[MAXN + 5];//



int CHANGE(int i,int k)//

{

	if(i <= k + 1)//一跳就到

	{//从一开始选

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

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

		{

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

			{

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

			}

		}

		return min;

	}

	else

	{

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

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

		{

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

			{

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

			}

		}

		return min;

	}

}



int main()

{

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

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

	{

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

	}

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

	{

		if(i == 1)

		{

			dp[i] = 0;

		}

		else if(i == 2)

		{

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

		}

		else

		{

			dp[i] = CHANGE(i,k);

		}

	}

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

	return 0;

}
// Slower C++ solution:
/**

*Coder: Yeshayah

**/



#include <bits/stdc++.h>

using namespace std;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

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

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

    int k;

    string s;

    cin >> k >> s;

    if (s.size() <= k) cout << s << "\n";

    else cout << s.substr(0,k) << "...\n";

    return 0;

}

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

#include<string.h>



int main()

{

	int n;

	scanf("%d",&n);

	char str[102];

	scanf("%s",&str);

	int len=strlen(str);

	if(len>n)

	{

		str[n]='.';

		str[n+1]='.';

		str[n+2]='.';

		str[n+3]='\0';

	}

	printf("%s",str);

	return 0;

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

using namespace std;



int main(){

	int N;

	cin >> N;



	int temp, numbers[100001] = {0};

	long long sum = 0;

	while(N-- > 0){

        cin >> temp;

        numbers[temp]++;

        sum += temp;

	}



    int Q, B, C;

    cin >> Q;

    while(Q-- > 0){

        cin >> B >> C;

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

        numbers[C] += numbers[B];

        numbers[B] = 0;

        cout << sum << endl;

    }

	return 0;

}


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

#define rush() int T;scanf("%d",&T);int kase=1;while(T--)

#define pb(x) push_back(x)

#define pr pair<int,int>

#define mem(a) memset(a,0,sizeof(a))

#define  fi first

#define  se second

using namespace std;

typedef long long ll;

const ll maxn=2e6+5;

const ll mod=1e9+7;

ll quickpow(ll x,ll y,ll mod){ll ans=1;while(y){if(y&1)ans=ans*x%mod;x=x*x%mod;y>>=1;}return ans;}



int a[maxn]={0};



int main()

{

    int n;

    cin>>n;

    ll sum=0;

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

    {

        int x;

        cin>>x;

        sum+=x;

        a[x]++;

    }

    int q;

    cin>>q;

    while(q--)

    {

        int b,c;

        cin>>b>>c;

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

        a[c]+=a[b];

        a[b]=0;

        cout<<sum<<endl;

    }

    return 0;

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

#include<algorithm>

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

#define Z 20000

using namespace std;

int d[Z+1];

int main(){

	int n,m,k,r,l;

	while(true){

		cin>>n>>k>>m>>r;

		if(!(n+m+k+r))break;

		rep(i,0,Z)d[i]=0;

		k=1,l=n;

		rep(i,0,Z)d[i]+=k/l,k%=l,k*=10;

		if(m)rep(i,1,n){k=1,l=i*n;rep(j,0,Z)d[j]+=k/l,k%=l,k*=10;}

		for(int i=Z;i>0;i--)d[i-1]+=d[i]/10,d[i]%=10;

		cout<<d[0]<<".";

		rep(i,1,r+1)cout<<d[i];

		cout<<endl;

	}

	return 0;

}

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

#include<algorithm>

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

#define Z 15000

using namespace std;

int d[Z+1];

int main(){

	int n,m,k,r,l;

	while(true){

		cin>>n>>k>>m>>r;

		if(!(n+m+k+r))break;

		rep(i,0,Z)d[i]=0;

		k=1,l=n;

		rep(i,0,Z)d[i]+=k/l,k%=l,k*=10;

		if(m)rep(i,1,n){k=1,l=i*n;rep(j,0,Z)d[j]+=k/l,k%=l,k*=10;}

		for(int i=Z;i>0;i--)d[i-1]+=d[i]/10,d[i]%=10;

		cout<<d[0]<<".";

		rep(i,1,r+1)cout<<d[i];

		cout<<endl;

	}

	return 0;

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

using namespace std;

typedef long long ll;

const int maxn=1000010;

const int inf=0x3f3f3f3f;



ll n;

ll dp[maxn];

ll a[maxn];

ll k;

void dfs()

{

    

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

    dp[0]=0;

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

    {

        dp[i]=inf;

    }

    

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

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

        {

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

        }

    }

    cout<<dp[n-1];

    return ;

}



int main()

 { 

      cin>>n>>k;

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

      cin>>a[i];

      dfs();

 }

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

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

using namespace std;

int main() {

    int k,x,a,b;

    cin>>k>>x;

    a=x-k+1;

    b=x+k-1;

    for(int i=a;i<=b;i++) {

        cout<<i<<" ";

    }

return 0;

}


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

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

#include <algorithm>



using namespace std;



int main(){

    int N, K;

    cin >> N >> K;



    int p1, px, mins[N], alturas[N];



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

        if(i < N){

            cin >> px;

            alturas[i] = px;

        }

        else {

            cout << mins[N-1];

        }

        if(i == 0){

            p1 = px; 

        }

        else if(i == 1){

            mins[1] = abs(p1 - px);

        }

        else {

            int saltos = min(K, min(N, i));

            mins[i] = 1000000000; 

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

                if(j == i-1){

                    mins[i] = min(mins[i], abs(p1 - px));

                }

                else {

                    int ult_salto = abs(px - alturas[i-j-1]);

                    mins[i] = min(mins[i], ult_salto + mins[i-j-1]); 

                }

            }

        }

    }

    return 0;

}

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

typedef		long long       ll;

typedef		vector<ll>      vll;

#define     fi              first

#define     se              second

#define		pb              push_back

#define		nn              "\n"

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

#define		zz(v)           (ll)v.size()

#define 	S(a)            scanf("%lld",&a)

#define 	SS(a,b)         scanf("%lld %lld",&a,&b)

#define 	SSS(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)

#define		ss              ' '

#define     pii             pair<ll,ll>

const double eps = 1e-8;



ll ar[100010];

ll dp[100010];

ll n,k;



int main()

{

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

    cin>>n>>k;

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

        cin>>ar[i];

    dp[1]=0;

    dp[0]=1e6;

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

    {

        ll ans=INT_MAX;

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

        {

            if(i-j>=0)

            {

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

            }

            else

                break;

        }

        dp[i]=ans;

    }

    cout<<dp[n]<<ss;

}














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

using namespace std;

typedef		 long long int    ll;

typedef		vector<ll>      vll;

#define     fi              first

#define     print(v)        for(ll i:v) cout<<i<<ss

#define     se              second

#define		pb              push_back

#define		nn              "\n"

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

#define		zz(v)           (ll)v.size()

#define 	S(a)            scanf("%lld",&a)

#define 	SS(a,b)         scanf("%lld %lld",&a,&b)

#define 	SSS(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)

#define		ss              ' '

#define     pii             pair<ll,ll>

#define		gcd(a,b)        __gcd(a,b)

#define		lcm(a,b)        (a*b)/gcd(a,b)

const double eps = 1e-8;







int main()

{



    ll n,k;

    cin>>n>>k;

    ll ar[n+10];

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

    {

        cin>>ar[i];

    }



    ll dp[n+10];



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

    {

        dp[i]=1e18;

    }

    dp[1]=0;

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

    {

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

        {

            if(i+j>n)

                continue;



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

        }

   



    }



    



     cout<<dp[n]<<nn;









}





/* Final check before submit :



1. array size or integer overflow,like uninitialised 0 index.



2. Think twice,code once.check all possible counter test case.



3. Be careful about corner case! n=1?k=1? something about 0?



4. avoid stupid mistake!complexity(t/m)?precision error?submit same code twice?



5. if got WA than remember that you are missing something common.

   *** Be confident!!! who knows? may be your one step back to AC ***

4. minus mod ans=(ans-k+mod)%mod;



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

using namespace std;



long dp[100001], a[100001], n, k;



long magic(int idx){

	if(idx == n)

		return 0;

	if(idx > n)

		return 1e9 + 7;

	long &ret = dp[idx];

	if(~ret)

		return ret;

	ret = INT_MAX;

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

		ret = min(ret, abs(a[idx] - a[idx + i]) + magic(idx + i));

	}

	return ret;

}



int main(){

 

	ios::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);



	memset(dp, -1, sizeof dp);

	cin >> n >> k;

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

		cin >> a[i];

	cout << magic(1);

	return 0;

}

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

using namespace std;



int main(){ 

    

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    

    int n, k, a[100001];

    cin >> n >> k;

    vector<int> dp(n, INT_MAX);

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

        cin >> a[i];

    dp[0] = 0;

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

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

            if(i + j < n)

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

            else

                break;

        }

    }

    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 left=abs(arr[i]-arr[i+1])+clac(i+1);

    int right=inf;

    if(i+2<=n){

        right=abs(arr[i]-arr[i+2])+clac(i+2);

    }

    */

    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.3696624852390105x):
#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;

}
// Slower C++ solution:


#include <bits/stdc++.h>



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

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

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

#define M_PI 3.14159265358979323846

#define mod 1000000007

#define endl "\n"

using namespace std;

using ll = long long;

using lli = long long int;

using ld = long double;





const int N = 5e5 + 6, inf = 2e9;

int n,k;

int arr[N], dp[N];

int main()

{

	MEGA_MIND



		cin >> n >>k;

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

	{

		dp[i] = inf;

		cin >> arr[i];

	}



	dp[0] = 0;

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

	{

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

		{

			if(j<n)

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

		}

	}

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

	return 0;

}

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

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

#include <math.h>

#include <string.h>

#include <vector>

#include <algorithm>

#include <stack>



using namespace std;

typedef long long ll;







int main(){

    ll n,k,h[1000000];cin>>n>>k;

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

    ll dp[n];

    dp[0]=0;

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

        dp[i]=10000000000;

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

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

        }

    }

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

    

    return 0;

}




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

using namespace std;

typedef long long ll;

//#include <boost/multiprecision/cpp_ll.hpp>

//typedef boost::multiprecision::cpp_ll ll;

typedef long double dd;

#define i_7 (ll)(1E9+7)

//#define i_7 998244353

#define i_5 i_7-2

ll mod(ll a){

    ll c=a%i_7;

    if(c>=0)return c;

    return c+i_7;

}

typedef pair<ll,ll> l_l;

ll inf=(ll)1E16;

#define rep(i,l,r) for(ll i=l;i<=r;i++)

#define pb push_back

ll max(ll a,ll b){if(a<b)return b;else return a;}

ll min(ll a,ll b){if(a>b)return b;else return a;}

void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);

void Min(ll &pos,ll val){pos=min(pos,val);}

void Add(ll &pos,ll val){pos=mod(pos+val);}

dd EPS=1E-9;

string alp="abcdefghijklmnopqrstuvwxyz";

#define endl "\n"

#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);







int main(){fastio

    ll n,k;cin>>n>>k;

    ll h[n];rep(i,0,n-1)cin>>h[i];

    ll dp[n];

    dp[0]=0;

    rep(i,1,n-1){

        dp[i]=inf;

        rep(j,max(i-k,0),i-1){

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

        }

    }

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

    

    

    return 0;

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

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

long long dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1e9;



int frog2(int i,int j){



    if(dp[i] != -1){

        return dp[i];

    }



    int menor = inf;



    if(i>=j) return dp[i]=0;



    for(int x=1;x<=k;x++){ ///i=0; i=1;i=2;i=3;

        if(i+x <= j){      ///x=1, x=1;x=1;x=1;

            long long cost = (frog2(i+x,j)+ abs(pedras[i]-pedras[i+x]));



            if(cost < menor){

                menor = cost;

            }

        }

    }



    return dp[i] = menor;

}



int main(){



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



    cin >>n>>k;



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

        cin >> pedras[i];

    }



    cout<<frog2(0,n-1)<<endl;

}


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

#include <algorithm>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];

int dp[100100]; ///Guardo os valores da funcao frog;



int n,k;

const int inf =1000000/*1e9*/;



int main(){



    memset(dp,inf,sizeof(dp));

    cin >>n>>k;



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

        cin >> pedras[i];

    }



    ///Comeca aki a bagaca

    dp[n-1]=0;



    for(int i=n-2;i>=0;i--){



        ///int menor=inf;



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

            if(i+j<=n-1){

                int cost = abs(pedras[i]-pedras[i+j]);

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

                /*menor = min(menor,dp[i+j]+cost);*/

            }

        }



        ///dp[i]=menor;

    }



    cout<<dp[0];



}