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

#include <stdio.h>



#define f first

#define s second

#define pb push_back

#define ppb pop_back

#define mp make_pair



using namespace std;



typedef long long ll;

typedef long double ld;

typedef unsigned long long ull;



const int N = 1e5 + 123, inf = 1e9 + 123, mod = 1e9 + 7;

const ll INF = 1e18;



int dp[N], a[N];



int main() {

	cin.tie(0);

	cout.tie(0);

	ios_base::sync_with_stdio(0);

	int n, k;

	cin >> n >> k;

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

		cin >> a[i];

	}             

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

		dp[i] = inf;

	}

	dp[1] = 0;

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

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

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

		}

	}                                                

	cout << dp[n];

	return 0;       	

}                               

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

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

using namespace std;

#define ff first

#define ss second

#define ll long long int

#define pii pair<int,int>

#define pll pair<ll,ll>

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

#define mem1(x) memset(x, -1, sizeof x)

#define MX 100005

ll arr[MX], dp[MX],n;

//ll rec(ll i, ll score)

//{

//    if(i==n-1) return score;

//    else if(i>=n) return 1e9;

//    else if(dp[i]<score && dp[i]!=-1) return dp[i];

//    else if(!i || dp[i]==-1 || score<dp[i])

//    {

//        ll ret1=1e9, ret2=1e9;

//        if(i+1<=n-1) ret1= rec(i+1, score+llabs(arr[i+1]-arr[i]));

//        if(i+2<=n-1) ret2= rec(i+2, score+llabs(arr[i+2]-arr[i]));

//        return dp[i]=min(ret1,ret2);

//    }

//}

int main()

{

    ll i,j,k,l, m;

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

    for(i=0; i<n; i++) scanf("%lld",&arr[i]);

    for(i=1; i<n; i++) dp[i]=1e9;

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

    {

        ll len=i+k;

        for(j=i+1; j<n && j<=len; j++)dp[j]=min(dp[j], dp[i]+llabs(arr[i]-arr[j]));

    }

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

    return 0;

}


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

#include<iostream>

#include<cmath>

#include<stack>

#include<cstring>

#include<algorithm>

using namespace std;

int n,k;

int h[100005];

int dp[100005];



int main()

{

memset(dp, 0x3f ,sizeof dp);

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

for( int i = 1 ; i <= n ; i++) scanf("%d",&h[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(h[i] - h[i+j]));

}

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



	return 0;

}






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

#include<cmath>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<cstdlib>

using namespace std;

int N,K,h[100005],dp[100005];

int main(){

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

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

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

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

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

		dp[i] = 0x3f3f3f3f;

		for(int j = K;j >= 1;j--){

		if(i-j>0)

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

		}

	}

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

	return 0;

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

using namespace std;



int n,k,h,sum;



int main()

{

	cin>>n>>k;

	

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

		{

			cin>>h;

			if(h>=k)sum++;

		}

	

	cout<<sum<<endl;

	

	return 0;

}

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

int main()

{

    int del[100000];

    int i,k,n,sum=0;

    scanf("%d",&n);

    scanf("%d",&k);

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

    {

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

    }

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

    {

        if(del[i]>=k)

            sum++;

    }

    printf("%d",sum);

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



using namespace std;

#define ll long long int

#define X first

#define Y second

const int MAXN = 1e5 + 5;

ll n, dp[MAXN], arr[MAXN], k;



ll solve(int idx = 1)

{

	if (idx > n) return 0;



	ll &ret = dp[idx];

	if (~ret) return ret;

	ret = 0;



	if (idx < n) ret = solve(idx + 1) + abs(arr[idx] - arr[idx + 1]);

	for (int i = idx + 2; i <= min(idx + k, n); i++)

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



	return ret;

}



int main()

{

	cin >> n >> k;

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

		cin >> arr[i];

	memset(dp, -1, sizeof dp);

	cout << solve() << endl;

	return 0;

}

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



using namespace std;

#define ll long long int

#define X first

#define Y second

const int MAXN = 1e5 + 5;

int n, dp[MAXN], arr[MAXN];





int main()

{

	int k;

	cin >> n >> k;

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

		cin >> arr[i];



	dp[1] = 0;

	dp[2] = abs(arr[1] - arr[2]);

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

	{

		dp[i] = 1e9 + 100;

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

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

	}

	cout << dp[n] << endl;

	return 0;

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



}

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

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

}


// Faster C++ solution for the same problem (measured speedup: 1.081065870582426x):




#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();

	}

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

}

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

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

}

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

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

}

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

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

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

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

}

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

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

}


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



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;

}

// Faster C++ solution for the same problem (measured speedup: 1.7898150576973244x):
/**

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

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

}


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

    

}
// Slower C++ solution:
#define __USE_MINGW_ANSI_STDIO

#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#include <unordered_set>

#include <unordered_map>



using namespace std;



typedef long long ll;

typedef long long ull;

typedef pair<ll, ll> ii;



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

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

#define endl		"\n"

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

#define mk			make_pair	



void File()

{

#ifndef ONLINE_JUDGE

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

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

#endif

#ifdef ONLINE_JUDGE

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

	freopen("pyramid.in", "r", stdin);*/

#endif

}

void fast()

{

	ios::sync_with_stdio(NULL);

	cout.tie(NULL);

	cin.tie(NULL);

	//File();

}

const double pi = 2 * acos(0.0);

const ll oo = 0x3f3f3f3f;

const int MOD = 1e9 + 7;

const int nn = 1e5 + 15;

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

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



int n, k;

ll arr[nn];

ll dp[nn];

int main()

{

	fast();

	cin >> n >> k;

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

		cin >> arr[i];

	fill(dp, dp + nn, oo*oo);

	dp[0] = 0;

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

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

	{

		ll mn = oo*oo;

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

		{

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

		}

		dp[i] = mn;

	}

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2810235430962293x):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

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

#define per(i,a,n) for (int i=n-1;i>=a;i--)

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

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

#define clr(v, d) memset(v, d, sizeof(v))

#define fi first

#define se second

#define pp push_back

#define pb pop_back

#define pi 3.141592654

#define MOD 1000000007 

#define cons1 1e12

#define cons2 1e6

using namespace std;

typedef long long ll;

typedef vector<int> vi;

typedef pair<int, int> pa;

typedef unsigned long long int ull;

typedef double long dl;

const int N = 1e5+9,oo=1e9;

int n,k;

int v[N], ans[N];

int sol(int i) {

	if (ans[i] == -1) {

		if (i == n - 1)

			ans[i] = 0;

		else {

			int x =1+ min(k, n - i - 1);

			int mx = oo;

			while (--x)

				mx = min(mx, abs(v[i] - v[i + x])+sol(i + x));

			

			ans[i] = mx;

		}



	}

	return ans[i];



}

void solve() {

	clr(ans, -1);

	cin >> n >> k;

	rep(i, 0, n)

		cin >> v[i];

	cout << sol(0) << endl;

	

}

int main() {

	ios::sync_with_stdio(0);

	cin.tie(NULL);

	cout.tie(NULL);

	int t = 1;

	//cin >> t;

	while (t--)

		solve();



#ifndef ONLINE_JUDGE

	- system("pause");

#endif



}