stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#define _USE_MATH_DEFINES

#include <algorithm>

#include <cstdio>

#include <functional>

#include <iostream>

#include <cfloat>

#include <climits>

#include <cstring>

#include <cmath>

#include <map>

#include <queue>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <time.h>

#include <vector>

using namespace std;



typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> i_i;

typedef pair<ll, int> ll_i;

typedef pair<double, int> d_i;

typedef pair<ll, ll> ll_ll;

typedef pair<double, double> d_d;

struct edge { int u, v; ll w; };



ll MOD = 1000000007;

ll _MOD = 1000000009;

double EPS = 1e-10;



int main() {

	for (;;) {

		int N, M; cin >> N >> M;

		if (N == 0) break;

		vector< vector<short> > a(M, vector<short>(1300));

		int r; cin >> r;

		while (r--) {

			int t, n, m, s; cin >> t >> n >> m >> s;

			m--;

			a[m][t] += (s ? 1 : -1);

		}

		vector< vector<short> > s(M, vector<short>(1300));

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

			for (int t = 1; t < 1300; t++)

				s[m][t] = s[m][t - 1] + a[m][t - 1];

		int q; cin >> q;

		while (q--) {

			int ts, te, m; cin >> ts >> te >> m;

			m--;

			int cnt = 0;

			for (int t = ts + 1; t <= te; t++)

				if (s[m][t]) cnt++;

			cout << cnt << endl;

		}

	}

}

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

#include <algorithm>

#include <cstdio>

#include <functional>

#include <iostream>

#include <cfloat>

#include <climits>

#include <cstring>

#include <cmath>

#include <map>

#include <queue>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <time.h>

#include <vector>

using namespace std;



typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> i_i;

typedef pair<ll, int> ll_i;

typedef pair<double, int> d_i;

typedef pair<ll, ll> ll_ll;

typedef pair<double, double> d_d;

struct edge { int u, v; ll w; };



ll MOD = 1000000007;

ll _MOD = 1000000009;

double EPS = 1e-10;



int main() {

	for (;;) {

		int N, M; cin >> N >> M;

		if (N == 0) break;

		int r; cin >> r;

		vector<int> t(r), n(r), m(r), s(r);

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

			cin >> t[i] >> n[i] >> m[i] >> s[i];

		int q; cin >> q;

		while (q--) {

			int ts, te, _m; cin >> ts >> te >> _m;

			int x = 0, start, end, ans = 0;

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

				if (m[i] != _m) continue;

				if (s[i]) {

					if (x == 0) start = t[i];

					x++;

				}

				else {

					x--;

					if (x == 0) {

						end = t[i];

						ans += max(0, min(te, end) - max(ts, start));

					}

				}

			}

			cout << ans << endl;

		}

	}

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



using namespace std;

int n,k;

int ar[100005];

int dp[100005];



int main() {

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

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

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    memset(dp,-1,sizeof dp);

    cin>>n>>k;

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

        cin>>ar[i];

    }

    dp[0]=0;

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

        int tmp=1e9;

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

            if(ind-ik<0){

                break;

            }

            else{

                tmp=min(tmp,dp[ind-ik]+abs(ar[ind-ik]-ar[ind]));

            }

        }

        dp[ind]=tmp;

    }

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

    return 0;

}

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



using namespace std;

#define ll long long



const int INF = 1e9 + 5;



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int n,k;

    cin >> n>>k;

    int ar[n];

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

        cin >> ar[i];

    }

    vector<int> dp(n, INF);

    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(dp[j], dp[i] + abs(ar[i] - ar[j]));

            }

        }

    }

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

    return 0;

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

using namespace std;



typedef long long int ll;

ll MOD = 1000000007;

ll INFL = 1ll << 60;

ll INF = 1 << 30;



int main() {

  ll n, k;

  cin >> n >> k;



  ll v[110000] = {};

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

    cin >> v[i];



  ll dp[110000] = {};

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



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

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

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

    }

  }



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

}


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

using namespace std;



typedef long long int ll;

ll MOD = 1000000007;

ll INFL = 1ll << 60;

ll INF = 1 << 28;



// ====================================================================

int main() {

  // 入力

  int n, k;

  cin >> n >> k;

  vector<int> v(n);

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

    cin >> v[i];

  }



  vector<ll> dp(n);

  for (int i = 0; i < n; i++) dp[i] = INFL;

  dp[0] = 0;

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

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

      if (i - j < 0) break;

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

    }

  }



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

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



#define ll int64

#define pb push_back

#define ff first

#define ss second

#define sz size()

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

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

#define mod 1000000007

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

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

     

using namespace std;



const int maxn=110000;

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

int main(){

    IO;    

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

	for(int i=1;i<=n;i++) scanf("%d",&h[i]);

	memset(f,0x3f,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.432545900044641x):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define pb push_back

#define F first

#define S second

const int mod = 1e9+7, A = 1e5+5;

ll n, k, h[A], dp[A];

ll table1()

{

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

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

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

	return dp[n-1];

}

ll table2()

{

	dp[0] = 0;

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

	{

		dp[i] = 1e9;

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

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

	}

	return dp[n-1];

}

int main()

{

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

	cin >> n;

	cin >> k;

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

	//cout << table1();

	cout << table2();

	return 0;

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

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

#define SORT(v, n) sort(v, v+n);

#define VSORT(v) sort(v.begin(), v.end())

#define VRSORT(v) sort(v.rbegin(), v.rend())

#define ll int64_t

#define pb(a) push_back(a)

#define INF 1000000000

#define MOD 1000000007

using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;



typedef vector<unsigned int>vec;

typedef vector<vec> mat;



template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }





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

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



ll dp[101010];

ll h[101010];



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int n,K;

    cin>>n>>K;

    REP(i,n) cin>>h[i];

    REP(i,101010) dp[i]=1LL<<60;

    dp[0]=0;

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

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

            chmin(dp[i+k],dp[i]+abs(h[i+k]-h[i]));

        }

    }

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

}

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

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

#define VSORT(v) sort(v.begin(), v.end())

#define VRSORT(v) sort(v.rbegin(), v.rend())

#define ll long long

using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;

//typedef vector<unsigned int>vec;

typedef vector<ll>vec;

typedef vector<vec> mat;

typedef vector<vector<int>> Graph;



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

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

const int INF = 1000000000;

const ll LINF = 1000000000000000000;//1e18

const ll  MOD = 1000000007;

const double PI = acos(-1.0);

const double EPS = 1e-10;



template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};



int h[202020];

ll dp[202020];

int N, K;



ll rec(int i){

    if(dp[i] < LINF) return dp[i];

    if(i <= 0) return 0;



    ll res = INF;

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

        if(i - k >= 0) chmin(res, rec(i-k) + abs(h[i] - h[i-k]));

    }



    return dp[i] = res;

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    cin >> N >> K;

    REP(i,N) cin >> h[i];

    REP(i,N) dp[i] = LINF;



    cout << rec(N-1) << endl;

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

#include<cstring>

#include<cmath>

#include<map>

#include<algorithm>

#include <iostream>

using namespace std;

typedef long long LL;

const  LL MAXN = 1e9+7;

int main(){

	int n;

	LL ans = 1;

	scanf("%d",&n);

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

		ans *= i;

		ans %= MAXN;

	}

	printf("%lld",ans);

	return 0;

}

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

#include <algorithm>

using namespace std;



int main()

{

	int n,i;

	long long sum=1;

	scanf("%d",&n);

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

	{

		sum*=i;

		sum%=(1000000000+7);

	}

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

	return 0;

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

using namespace std;



int main()

{

    int n, k;

    cin >> n >> k;

    int arr[n+k+1];

    int dp[n+k+1];

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

        dp[i] = 0;



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

    {

        cin >> arr[i];

    }

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

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

    {

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

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

        {

            int x = i-j;

            if(x>=k+1)

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

        }

    }

    cout << dp[k+n] << endl;

    return 0;

}






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

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

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

//比如从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<bits/stdc++.h>

#define pb emplace_back

#define ll long long

#define mp make_pair

#define PI acos(-1)

using namespace std;

ll dp[200005],a[200005],n;

ll solve(ll idx,ll k)

{

    if(idx>=n-1)

        return 0;

    if(dp[idx]!=-1)

        return dp[idx];

    ll res=INT_MAX;

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

    {

        if(idx+i<n)

            res=min(res,abs(a[idx+i]-a[idx])+solve(idx+i,k));

    }

    return dp[idx]=res;

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    ll i,j,k,x,y,z,m,t,l,r;

    cin>>n>>k;

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

        cin>>a[i];

    memset(dp,-1,sizeof dp);

    cout<<solve(0,k)<<'\n';

}
















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

using namespace std;



vector<int> dp(100005);



int main(){

    

    int n,k; 

    cin>>n>>k;

    

    vector<int> v (n);

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

        cin>>v[i];

    }

    

    dp[0] = 0;

    

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

        int x = INT_MAX;

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

            

            if(j<=i){

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

            x = dp[i];

            }

        }

        

    }

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

    

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

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<bitset>

using namespace std;

//#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define ARDEB(i,X) cout<< #X <<"["<<i<<"]: "<<X[i]<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define ARDEB(i,X) {}

#define END {}

#endif

typedef long long int ll;

#define int long long

typedef pair<int,int> P;

struct edge{int to,cost;};

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

const int INF=100000000000000000;



int N,A[100110],K;

int dp[100110];



int DP(int a){

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

	vector<int> v;

	if(a==N-1){

		return 0;

	}

	DEB(a)END

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

		if(a<N-i){

			v.push_back(DP(a+i)+abs(A[a]-A[a+i]));

		}

	}

	return dp[a]=(*min_element(v.begin(),v.end()));

}



signed main(){

	cin>>N>>K;

	REP(i,N){

		cin>>A[i];

	}

	REP(i,100010){

		dp[i]=-1;

	}

	cout<<DP(0)<<endl;

	return 0;

}


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

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<unordered_map>

#include<bitset>

#include<iomanip>

#include<list>

#include<deque>

using namespace std;

//#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define END {}

#endif

typedef long long ll;

#define int ll

#define uset unordered_set

#define umap unordered_map

typedef std::pair<int,int> P;

struct edge{int to,cost;};

const int INF=100000000000000000;

const int INF2=9223372036854775807;

const int MOD=1000000007;

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

#define min(X,Y) (((int)(X)<(int)(Y))?(X):(Y))

#define max(X,Y) (((int)(X)>(int)(Y))?(X):(Y))

#define MAX(X,Y) (*max_element(X,Y))

#define MIN(X,Y) (*min_element(X,Y))

#define NP(X,Y) next_permutation(X,Y)

#define setp(X,Y) cout<<fixed<<setprecision(Y)<<X;

int ceil2(int a,int b){if(a%b==0){return a/b;}else{return a/b+1;}}

int pow2(int a,int b){int r=1;for(int i=1;i<=b;i++){r*=a;}return r;}

int Log2(int a){int t=0;while(1){if(a==0||a==1){break;}a/=2;t++;}return t;}



int N,K;

int H[102010];

int dp[102000];



signed main(){

	cin>>N>>K;

	REP(i,N){

		cin>>H[i];

		dp[i]=INF;

	}

	dp[0]=0;

	for(int i=0;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]));

		}

	}

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

	return 0;

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

#include <iostream>

#include <cmath>

#include <cstring>

#include <algorithm>

#include <queue>

using namespace std;

int a,b;

int s[2000001] ;

int main()

{

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

    	s[i]=-1000000+i;

	cin>>a>>b;

	for(int i=1000000+b-a+1;i<=1000000+b+a-1;i++)

		cout<<s[i]<<" ";

}

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

using namespace std;

int main(){

    int k , x;

    cin >> k >> x;

    x -= k;

    for (int i = 0 ; i < 2*k - 1 ;i++){

        x++;

        cout << x << " ";

    }

    return 0;

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

using namespace std;



typedef long long ll;



const int N = 1 * 1e5 + 10;

const ll inf = 1e18;



ll res;

ll a[N];

ll dp[N];



ll go(ll idx, ll k, ll n){

	if(idx == n)	return 0;

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

	ll mn = inf;

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

		mn = min(abs(a[idx] - a[i]) + go(i, k, n), mn);

	}

	return dp[idx] = mn;

}



void solve() {

	ll n, k;

	cin >> n >> k;

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

		cin >> a[i];

	}

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

	cout << go(1, k, n);

}



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

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

  ll t = 1;

  //cin >> t;

  while(t--){

    solve();

  }

    return 0;

}

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

using namespace std;



typedef long long ll;



const int N = 1 * 1e5 + 10;

const ll inf = 1 * 1e9 + 10;



ll res;

ll a[N];



void solve() {

	ll  n, k;

	cin >> n >> k;

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

		cin >> a[i];

	}

	std::vector<ll> dp(n, inf);

	dp[0] = 0;

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

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

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

		}

	}

	cout << dp[n - 1];



}



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

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

  ll t = 1;

  //cin >> t;

  while(t--){

    solve();

  }

    return 0;

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



using namespace std;



#define REP(i,a,b) for(ll i = a; i < b; i++)

#define DB( x ) cout << #x << " = " << x << endl;

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

#define endl '\n'



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> pii;

typedef pair<int, pii> tri;

typedef vector <vector <ll>> matrix;

typedef pair<string, string> pss;



const ll mod = (ll)1e9 + 7;

const int MAX = (int)1e5 + 100;



const ll oo = 0x3f3f3f3f3f3f3f3f;



ll dp[MAX], a[MAX];



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);



    memset(dp, oo, sizeof(dp));



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



    dp[1] = 0;



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

        cin >> a[i];

    }



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

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

            if(i - j >= 1)

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

        }

    }



    cout << dp[n] << endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.7994942781346022x):
#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 <bits/stdc++.h>

using namespace std;

#define Me3za() ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);srand(time(NULL))

#define endl "\n"

int n,k;

int l;

int arr[100005];

long long mem[100005];

long long solve(int i,long long cur){

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

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

mem[i]=LLONG_MAX;

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

   long long sol=solve(i+j,arr[i+j])+abs(cur-arr[i+j]);

   mem[i]=min(sol,mem[i]);

}

return mem[i];

}

int main() {

Me3za();

cin>>n>>k;

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

    cin>>arr[i];

}

memset(mem,-1,sizeof mem);

cout<<solve(0,arr[0]);

}




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

using namespace std;



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



int n,k;

int arr[N];



int mem[N];

int solve(int i)

{

    if(i==n-1)

        return 0;

    if(mem[i]!=-1)

        return mem[i];

    int ret=OO;

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

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

    return mem[i]=ret;

}





int main()

{

//    freopen("input.txt","rt",stdin);

//    freopen("output.txt","wt",stdout);

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

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

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

    memset(mem, -1, sizeof mem);

    printf("%d\n",solve(0));



    return 0;

}