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



using namespace std;

long long  a[100005],dp[100003];

long long n,k;

long long mincost(int x)

{

	if(x==n)

	{

		dp[x]=0;

		return dp[x];

	}

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

	{

		if(dp[x+i]==-1)

			dp[x+i]=mincost(x+i);

	}

	long long int  mini=1000000000;

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

	{

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

	}

	dp[x]=mini;

	return dp[x];

}

int main()

{

	cin>>n>>k;

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

	{

		cin>>a[i];

	}

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

	{

		dp[i]=-1;

	}

	

	cout<<mincost(1)<<'\n';

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

	// {

	// 	cout<<dp[i]<<' ';

	// }

	// cout<<'\n';



}

// Optimized solution to the same problem (faster):
//  Mayank Agarwal

//  IIIT Hyderabad







#include<bits/stdc++.h>

using namespace std;

const long long int M = 1000000007; 

#define ll long long int

#define ld double

#define pb push_back

#define mp make_pair

ll power(ll x, ll y,  ll p) 

{ 

    ll res = 1;      

    x = x % p;   

    while (y > 0) 

    { 

        if (y & 1) 

            res = (res*x) % p; 

        y = y>>1;x = (x*x) % p; 

    } return res%p; 

} 

ll modinver(ll a,ll b)

{

    return (power(a,1,M)*power(b,M-2,M))%M;

}

void SieveOfEratosthenes(int n) 

{   

    bool prime[10005]; 

    memset(prime, true, sizeof(prime)); 

  

    for (int p=2; p*p<=n; p++) 

    { 

        if (prime[p] == true) 

        { 

            for (int i=p*p; i<=n; i += p) 

                prime[i] = false; 

        } 

    } 

} 

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);cout.tie(0);

    ll n,k;

    cin>>n>>k;

    ll a[100002];

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

    {

    	cin>>a[i];

    }

    ll dp[100002];

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

    {

        dp[i]=1e16;

    }

    dp[n]=0;

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

    {

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

        {

            // cout<<i<<" "<<i+j<<'\n';

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

        }

        // cout<<dp[i]<<'\n';

    }

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





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

#include <string>

using namespace std;



int main(){

	int n,m,p,i,j,k,ac=0,wa=0,a[100100],w[100100];

	string s;

	cin>>n>>m;

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

        a[i]=0;

        w[i]=0;

	}

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

		cin>>p>>s;

		if(a[p]==0){

            if(s=="AC"){

                ac++;

                a[p]=1;

            }

            else{

                w[p]++;

            }

		}

	}

	for(k=1;k<=n;k++){

		if(a[k]==1){

			wa=wa+w[k];

		}

	}

	cout<<ac<<" "<<wa;

	return 0;

}


// Optimized solution to the same problem (faster):




#include<stdio.h>

#include<map>

#include<string>

#include<bits/stdc++.h>

using namespace std;

map<int,int>q;

int main()

{

	int n,m,i,a,ans=0,wns=0;

	string s;

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

	{

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

		{            

			cin>>a;

			cin>>s;

			if(q[a]==-1)

				continue;

			if(s=="AC")

			{

				ans+=1;

				wns+=q[a];

				q[a]=-1;

				continue;

			}

			q[a]++;

//			if(s=="AC"&&!q[a])

//			{                 

//				q[a]=1;

//				ans++;

//			}else if(s=="WA"&&!q[a])

//				wns++;

		}

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

		ans=wns=0;

		q.clear();

	}

}





                    

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

#include <vector>

using namespace std;

int main ()

{

	int n, m, acc=0, pen=0; cin>>n>>m;

	bool test[n+1]={false};

	int arr[n+1]={0};

	int p[m];

	string s[m];

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

	{

		cin>>p[i];

		cin>>s[i];

		if (test [p[i]]==false && s[i]=="AC")

		{

			test[p[i]]=true;

			acc++;

			pen+=arr[p[i]];

		}

		if (test[p[i]]==false && s[i]=="WA")

		{

			arr[p[i]]++;

		}

	}

	cout<<acc<<endl<<pen<<endl;

	return 0;

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

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n,k;

	cin>>n>>k;

	bool ac[100010]={false};

	ll wa[100010]={};

	ll ans=0;

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

		ll x;string s;

		cin>>x>>s;

		x--;

		if(ac[x]){

			continue;

		}

		if(s=="AC"){

			ac[x]=true;

			ans++;

		}

		else{

			wa[x]++;

		}

	}

	ll cnt=0;

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

		if(ac[i]){

			cnt+=wa[i];

		}

	}

	cout << ans<<" "<<cnt;

	// your code goes here

	return 0;

}

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

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007

 	bool ac[100010]={};

	ll wa[100010]={};

int main() {

	ll n,k;

	cin>>n>>k;



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

		ll x;string s;

		cin>>x>>s;x--;

		if(s=="AC"){

			ac[x]=true;

		}

		else{

			if(ac[x]!=true){

				wa[x]++;

			}

		}

	}

		ll cnt1=0;

		ll cnt2=0;

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

			if(ac[i]){

				cnt1++;

				cnt2+=wa[i];

			}

		}

		cout << cnt1<<" "<<cnt2;	

	// your code goes here

	return 0;

}

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

#include <vector>

using namespace std;

int main ()

{

	int n, m, acc=0, pen=0; cin>>n>>m;

	bool test[n+1]={false};

	int arr[n+1]={0};

	int p[m];

	string s[m];

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

	{

		cin>>p[i];

		cin>>s[i];

		if (test [p[i]]==false && s[i]=="AC")

		{

			test[p[i]]=true;

			acc++;

			pen+=arr[p[i]];

		}

		if (test[p[i]]==false && s[i]=="WA")

		{

			arr[p[i]]++;

		}

	}

	cout<<acc<<endl<<pen<<endl;

	return 0;

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

using namespace std;

typedef long long ll;

#define pb push_back

#define mod 1000000007

#define null NULL

#define test cout<<"#"

#define maX 1000000000

 

vector<ll> ans(100001, LONG_LONG_MAX);

void solve(vector<ll> &v, ll n, ll k)

{

	ans[0] = 0;

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

	{	

		ans[x] = 1000000000000;

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

		{

			if((x-y)>=0)

				ans[x] = min(ans[x], abs(v[x]-v[x-y]) + ans[x-y]);

		}

	}

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    ll n, k;

	cin>>n>>k;

    vector<ll> v(n);

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

		cin>>v[x];

	solve(v, n, k);

	cout<<ans[n-1];

}

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

using namespace std;

typedef long long ll;

#define mod 1000000007

#define pb push_back

#define test cout<<"#"



int main()

{

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	ll n, k;

	cin>>n>>k;

	vector<ll> v(n);

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

		cin>>v[x];

	ll ans[n];

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

		ans[x] = LONG_LONG_MAX;

	ans[0] = 0;

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

	{

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

		{

			if(x-y>=0)

				ans[x] = min(ans[x], ans[x-y]+abs(v[x]-v[x-y]));

		}

	}

	cout<<ans[n-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<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

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;

inline LL max(LL x, LL y) {

	return x > y ? x : y;

}

inline LL min(LL x, LL y) {

	return x < y ? x : y;

}

int main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	cin >> n >> k;

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

		cin >> a[i];

	dp[0]=dp[1] = 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(1LL, i - k); j--)

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

	cout << dp[n];

	return 0;

}

// Optimized solution to the same problem (faster):
//才不露白

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

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

//比如从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;

}

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

using namespace std;

int main(){

	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	string s, ss = ""; cin>>s;

	for(int i : s){

		if(i == '0')

			ss += i;

		else if(i == '1')

			ss += i;

		else if(i == 'B')

			if(!ss.empty())

				ss.pop_back();		

	}

	cout<<ss;

	

	return 0;

}

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

using namespace std;



signed main()

{

    int n, k;

    cin >> n >> k;



    vector<int> h(n);

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

        cin >> h[i];



    vector<int> dp(n);

    dp[n-1] = 0;

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

    {

        dp[i] = INT_MAX;

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

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

    }



    cout << dp[0] << endl;

}


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



typedef long long ll;



const int maxn = 1e5 + 5;

const ll INF = 1e18;

int N, h[maxn], K;

ll dp[maxn];



ll solve(int n) {

    if (n == 0) {

        return 0;

    }

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

        return dp[n];

    }

    dp[n] = INF;

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

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

    }

    return dp[n];

}



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);



    cin >> N >> K;

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

        cin >> h[i];

    }

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

    ll answer = solve(N - 1);

    cout << answer;



    return 0;

}

// Optimized solution to the same problem (faster):
/**

 * code generated by JHelper

 * More info: https://github.com/AlexeyDmitriev/JHelper

 * @author rahdirigs

 */



#include <bits/stdc++.h>





template <typename T> void s_min(T& a, T b) {

	if (a > b) {

		a = b;

	}

}



using namespace std;



typedef long long ll;



const ll INF = 1e18;



class TaskB {

public:

	void solve(std::istream& in, std::ostream& out) {

		int n, k;

		in >> n >> k;

		vector<int> h(n);

		for (int i = 0; i < n; i++) in >> h[i];

		vector<ll> dp(n, 0);

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

			dp[i] = INF;

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

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

			}

		}

		out << dp[n - 1];

	}

};





int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

	TaskB solver;

	std::istream& in(std::cin);

	std::ostream& out(std::cout);

	solver.solve(in, out);

	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;

const double eps=1e-6,pi=acos(-1);

const int N=1e5+5,M=1e9+7,SEGM=4*N,OO=0x3f3f3f3f;

int n,m,a[N],b[N],c[N];

int mem[N];



int solve(int i=0){

    if(i>=n) return 1e9;

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

    int &ret=mem[i];

    if(~ret) return ret;

    ret=1e9;

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

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

    return ret;

}



int main(){

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

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

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

        scanf("%d",a+i);

    memset(mem,-1,sizeof mem);

    printf("%d",solve());

    return 0;

}


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

using namespace std;

using ll = long long;

using ii = pair<int, int>;

using iii = pair<ii, int>;

#define vt vector

#define pq priority_queue

#define pb push_back

#define pf push_front

#define mp make_pair

#define fi first

#define se second

#define __lcm(a, b) a * b / __gcd(a, b)

const int MOD = 1e9 + 7;

const int INF = 2e9;

const int N = 1e5 + 5;



int n, k;

int a[N], dp[N];



void getDP() {

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

        int &sol = dp[idx];

        if(idx == 1) sol = 0;

        else {

            sol = INF;

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

                if(idx >= i + 1) 

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

            }

        }

    }

}



void solve() {

    cin >> n >> k;

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

        cin >> a[i];

    }

    getDP();

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

}



int main() {

 

    ios_base::sync_with_stdio(0);

    cin.tie(0); cout.tie(0);

    

    solve();

    

}

// 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 x_ real()

#define y_ imag()

#define cross(a, b) (conj(a)*(b)).imag()

#define dot(a, b) (conj(a)*(b)).real()

#define PI acos(-1)

#define F first

#define S second

#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)

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

typedef long double ld;

typedef long long ll;

typedef unsigned long long ull;

typedef complex<ld> point;

typedef tuple<int, int, int> line;

typedef vector<point> polygon;

typedef pair<double, double> pd;

pair<int, int> dirs[] = { {1, 2}, {-1, 2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, -2}, {-1, -2} };

ll mod = 1e9 + 7;

ld eps = 1e-6;









int main() {

	fastIO;

	int n, k;

	cin >> n >> k;

	int dp[n+1], arr[n+1];

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

		cin >> arr[i];

	}

	dp[0] = 0;

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

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

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

			if (i - j < 0)

				continue;

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

		}

	}



	cout << dp[n-1];



}


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

#include<stdlib.h>

#include<math.h>





typedef long long ll;





ll *mem, *inp, dist, min_dist, N;

int k;



int main()

{

    ll i, j;

    scanf("%lld%d", &N, &k);



    mem =(ll*) malloc(N * sizeof(ll));

    inp =(ll*) malloc(N * sizeof(ll));



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

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

    }



    *(mem+N-2) = llabs(*(inp+N-2) - *(inp+N-1));



    for(i = 3; i<=k+1 && i<=N; ++i)

        *(mem+N-i) = llabs(*(inp+N-i) - *(inp+N-1));



    for(i = N-(k+2); i>=0; --i){



        min_dist = llabs(*(inp+i) - *(inp+i+1)) + *(mem+i+1);

        for(j = 2; j<=k && i+j<N; ++j){

            dist = llabs(*(inp+i) - *(inp+i+j)) + *(mem+i+j);

            if (dist < min_dist) min_dist = dist;

        }



        *(mem+i) = min_dist;

    }



    printf("%lld\n", *mem);



    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;





}