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



using namespace std;

typedef long long ll;

typedef double dd;

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

#define endl "\n"

#define clr(n, r) memset(n,r,sizeof(n));



void fast() {



    cin.tie(0);

    cin.sync_with_stdio(0);

}

const int MAX = 1e5 + 9;

ll arr[MAX], dp[MAX];

int k,n;

int main() {

    fast();

    cin >> n>>k;

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

        cin >> arr[i];

    }

    clr(dp,10000);

    dp[0]=0;

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

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

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

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

        }

     //   cout<<dp[j]<<" ";

    }

    cout<<dp[n-1];

}

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

using namespace std;



#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void __f(const char* name, Arg1&& arg1) {

  cerr << name << " : " << arg1 << "\n";

}

template <typename Arg1, typename... Args>

void __f(const char* na, Arg1&& arg1, Args&&... args) {

  const char* c = strchr(na + 1, ',');

  cerr.write(na, c - na) << " : " << arg1 << " , ";

  __f(c + 1, args...);

}

#define endl "\n"

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

#define UNIQUE(v) sort(all(v)),v.erase(unique(all(v)),v.end())

typedef long long LL;

typedef pair <int,int> pii;

const double pi = acos(-1.0);

const double eps = 1e-8;

const int mod = 1e9 + 7;

const int inf = 1e9;

const LL INF = 1e18;

const int N = 1e5 + 5;



int main() {

  ios_base::sync_with_stdio(false);

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

  #ifdef LOCAL

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

  #endif // LOCAL

  int n, k;

  cin >> n >> k;

  vector <LL> h (n + 1);

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

  vector <LL> dp (n + 1, INF);

  dp[1] = 0;

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

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

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

    }

  }

  cout << dp[n] << endl;

  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>



using namespace std;



int main(){

   int k,x;

   cin>>k>>x;

   for (int i=-1000000;i<1000001;i++){

    if(i==x){

        for (i=x-(k-1);i<x+k;i++){

            cout<< i<<" ";

        }

    }

   }

   return 0;

}






// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <cmath>

using namespace std;

int main(){

int k,x;

cin>>k>>x;

int z=(x-k)+1;

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

    cout<<z<<" ";

    z+=1;

}

cout<<x<<" ";

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

    z+=1;

    cout<<z<<" ";



}

return 0;

}


// Fastest known solution for this problem:
#include <bits/stdc++.h>

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

using namespace std;



int main(void) {

	int k, x;

	cin >> k >> x;

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

		if (i == x+k) cout << i << endl;

		else cout << i << " ";

	}

	return 0;

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

using namespace std;

using LL = long long;

LL N, M, S, A[50];

inline LL ceil(LL d, LL n) {return (d+n-1)/n;}

inline bool ok(LL x) {

  if(M < S-x) return false;

  return accumulate(A,A+N,0ll,[&](LL s, LL a){return s+ceil(a+x+1-N, N+1);}) <= x;

}

int main() {

  scanf("%lld", &N);

  M=N*(N-1);

  for(auto i=0; i<N; ++i) scanf("%lld", A+i), S+=A[i];

  LL l = -1, h = 1e18;

  while(l+1 < h) {

    LL m = (l+h)>>1;

    if(ok(m)) h = m;

    else      l = m;

  }

  for(auto x=max(0ll,h-M); x<=h; ++x) if(ok(x)) {

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

    return 0;

  }

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

using LL = long long;

LL N, M, S;

LL A[50];

inline LL ceil(LL d, LL n) {return (d+n-1)/n;}

inline bool ok(LL x) {

  if(M < S-x) return false;

  LL s = 0;

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

    s += max(0ll, ceil(A[i]+x+1-N, N+1));

    if(x < s)return false;

  }

  return true;

}

int main() {

  scanf("%lld", &N);

  M=N*(N-1);

  for(auto i=0; i<N; ++i) scanf("%lld", A+i), S+=A[i];

  LL l = -1, h = 1e18;

  while(l+1 < h) {

    LL m = (l+h)>>1;

    (ok(m) ? h : l) = m;

  }

  for(auto x=max(0ll,h-M); x<=h; ++x) if(ok(x)) {

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

    break;

  }

}

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

//#include <iomanip>

//#include <string>

#include <vector>

//#include <algorithm>

//#include <utility>

//#include <set>

//#include <map>

//#include <queue>

//#include <deque>

//#include <bitset>

//#include <math.h>

using namespace std ;

using ll = long long ;

//using ld = long double ;

using vll = vector<ll> ;

//using vvll = vector<vll> ;

//using vc = vector<char> ;

//using vvc = vector<vc> ;

//using vb = vector<bool> ;

//using vvb = vector<vb> ;

//using pll = pair<ll,ll> ;\



//ll mod = 1000000007 ;

//ll INF = 10000000000 ;

//long double pie = acos(-1) ;



//void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}

//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}

//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}

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

//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}

//void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}

//void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}





int main(){

	ll n ; cin >> n ;

	vll a(n) ;

	for(auto &i:a) cin >> i ;

	bool ok = true ;

	ll cnt = 0 ;

	while(ok){

		ok = false ;

		ll sum = 0 ;

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

			if(a.at(i)>=n){

				sum += a.at(i)/n ;

			}

		}

		ok = (sum!=0) ;

		if(ok){

			cnt += sum ;

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

				a.at(i) += (sum-a.at(i)/n)-a.at(i)/n*n ;

			}

		}

	}

	cout << cnt << endl ;

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

using namespace std;

typedef long long ll;

typedef long long int lli;

typedef long double ld;

const int oo = 1e9;

const int mod = 1e9+9;

 

//Juan Pablo Betancourt Maldonado



const int nax = 1e5+103;

int n, k;

int dp[nax];

int a[nax];

int f(int i){

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

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

	int ans = abs(a[i]-a[i+1])+f(i+1);

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

		if(i+j<n) ans = min(ans, abs(a[i]-a[i+j])+f(i+j));

	}

	return dp[i]=ans;

}

int main(){

	cin>>n>>k;

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

	memset(dp, -1, sizeof dp);

	cout<<f(0)<<endl;

	return 0;

}

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

#define maxn (int)(2e5)

#define INF 1<<30

#define i64 long long

#define pii pair<i64,i64>

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

#define LB(x,v) lower_bound(all(x), v) - x.begin()

#define fread() freopen("in.txt", "r", stdin);

using namespace std;



int h[maxn+2], dp[maxn+2], n, k;



void init(int n){

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

}



int solve(int cur){

    if(cur >= n) return INF;

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



    if(dp[cur] != INF) return dp[cur];



    //int res = INF;

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

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

    }



    return dp[cur];

}

int main() {

    /*BEFORE SUBMIT, CHECK

    sample i/o format

    statements for debugging are removed/comment out

    global values / stl containers are initialized/cleared for each test (if needed)

    all array size are sufficient

    loop variable or output value doesn't cross min/max limit of input

    // */

    // #ifndef ONLINE_JUDGE

    //     fread();

    // #endif

    

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

        init(n);

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

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

        }

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

    }    

    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>



using namespace std;



int main()

{

    int n;

    cin >> n;

    

    int m;

    cin >> m;

    

    int l = 1;

	int r = n;

	

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

		int tl;

		cin >> tl;

		int tr;

		cin >> tr;

        

		if (l < tl) l = tl;

		if (tr < r) r = tr;

	}

	

	if (r < l) {

		cout << "0" << endl;

	} else {

		cout << r - l + 1;

	}



    return 0;

}

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



int main()

{

    int n;

    int m;

    

    scanf("%d", &n);

    scanf("%d", &m);

    

    int l = 1;

	int r = n;

	

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

		int tl;

		int tr;

		scanf("%d", &tl);

		scanf("%d", &tr);

        

		if (l < tl) l = tl;

		if (tr < r) r = tr;

	}

	

	if (r < l) {

		printf("0\n");

	} else {

		printf("%d", r - l + 1);

	}



    return 0;

}

// Fastest known solution for this problem:
#include <bits/stdc++.h>

using namespace std;



int main()

{

    int N, M;

    cin >> N >> M;

    int LMax = 1;

    int RMin = N;

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

    {

        int L;

        int R;

        cin >> L >> R;

        LMax = max(LMax, L);

        RMin = min(RMin, R);

    }

    if(RMin + 1 >= LMax)

        cout << RMin - LMax + 1 << endl;

    else

        cout << "0" << endl;

    return 0;

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

using namespace std;



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

#define PER(i, a, b) for(int i = (a); i >= (b); i--)

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

#define debug(x) cerr << #x << " = " << x << endl;

#define SET(c, v) memset(c, v, sizeof c)

#define pi 3.14159265358979323846264338

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

#define INF 0x3f3f3f3f

#define mod 1000000007

#define pf push_front

#define pb push_back

#define mk make_pair

#define MAX 100000

#define S second

#define F first

typedef long long ll;

typedef long double lf;

typedef pair<int, int> ii;

typedef pair<int, ii> pii;

typedef unsigned long long int ull;

int v[MAX], pd[MAX], N, K;



int solve(int i) {

	int &ans = pd[i];

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

	if(i >= N) return 1 << 30;

	if(ans != -1) return ans;

	ans = 1 << 30;

	REP(j, i+1, i+K) ans = min(ans, solve(j) + abs(v[i] - v[j]));

	return ans;

}



int main(int argc, char** argv) {

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

	rep(i, 0, N) scanf("%d", &v[i]);

	memset(pd, -1, sizeof pd);

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

	return 0;

}


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

using namespace std;

 

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

#define PER(i, a, b) for(int i = (a); i >= (b); i--)

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

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

#define pf push_front

#define pb push_back

#define mk make_pair

#define S second

#define F first

typedef long long ll;

typedef long double lf;

typedef pair<int, int> ii;

const int MAX = 1e5+5;

int N, K, a[MAX];

ll pd[MAX];



ll solve() {

	REP(i, 2, N) {

		pd[i] = (1LL << 60);

		rep(j, max(1, i-K), i)

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

	}

	return pd[N];

}



int main(int argc, char ** argv) {

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

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

	printf("%I64d\n", solve());

	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;

//long long a[200005];

int main()

{

    int N;

    cin >> N;

    string S;

    cin >> S;

    if (N >= S.length())

    {

        cout << S << endl;

    }

    else

    {

        cout << S.substr(0, N) << "..." << endl;

    }

}


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

#include<string.h>

int main(){

	int k;

	char s[105];

	scanf("%d",&k);

	getchar();

	scanf("%s",&s);

	if(strlen(s)>k){

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

			printf("%c",s[i]);

		}

		printf("...");

		printf("\n");

	}

	else{

		int l = strlen(s);

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

			printf("%c",s[i]);

		printf("\n");

	}

}

// Fastest known solution for this problem:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const double PI = acos(-1);



int main()

{

	int k;

	string s;

	cin >> k >> s;

	if (s.size() <= k) cout << s << endl;

	else cout << s.substr(0, k) + "..." << endl;

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

using namespace std;

typedef long long ll;

ll sum=0;

//vector<ll> sl;

ll dp[100000];

ll func(ll a[],ll n,ll size,ll k)

{

	ll s;

	if(dp[n]>=0)

	return dp[n];

	else

	{

     ll mini=INT_MAX;

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

     {

     	if(n>=i)

     	{

		 

     s=dp[n-i]+abs(a[n]-a[n-i]);

     //cout<<"\nghj\n"<<s<<endl;

     if(s<mini)

     mini=s;

     }

     else 

     break;

	 }



	dp[n]=mini;

	if(n+1==size)

	return dp[n];

	else

	return func(a,n+1,size,k);

    }

}

main()

{

	ll n,a[100000],b[100000],k;

	cin>>n>>k;

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

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

	cin>>a[i];

	dp[0]=0;

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

	

	if(n>2)

	cout<<func(a,2,n,k)<<endl/*5 3

10 30 40 50 20*/;

	else

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

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

	cout<<dp[i]<<"  ";*/

}

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

using namespace std;

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

typedef long long ll;

#define lb lower_bound

#define ub upper_bound

#define pb push_back

#define mp make_pair

const ll inf=1e9+2;

int32_t main()

{

	IOS

	ll n,i,j,k;

	cin>>n>>k;

	vector<ll>v(n),dp(n,inf);

	for(auto &x:v)

		cin>>x;

	dp[0]=0;

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

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

	{

		for(j=i-k>0?i-k:0;j<i;j++)

		{

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

		}

    

	}

	cout<<dp[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<bits/stdc++.h>

using namespace std;

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

int main(){

	cin>>n>>k;

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

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

	f[1]=0;

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

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

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

	}

	cout<<f[n];

	return 0;

}

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



#define Solved int main()

#define pb push_back

#define mp make_pair

#define F first

#define S second

#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

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

#define rep(i, x, y) for(int i = x; i <= y; i++)

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

#define pii pair<int, int>

#define ll long long

#define ull unsigned long long

#define ld long double

#define mii map<int, int>

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

using namespace std;



inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}



const int maxn = 1e9;

const ll inf = 1e18;

const int mod = 998244353;

//const int mod = 1e9 + 7;

const int N = 1e5 + 10;



int n, k;

ll a[N];

ll dp[N];



void solve() {

    cin >> n >> k;



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

        cin >> a[i];



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

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

        dp[i] = inf;

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

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

        }

    }

    cout << dp[n];

}



Solved {

    SpeedForce;

	int tt = 1;

    //cin >> tt;

	while (tt--) {

		solve();

		cout << '\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 <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <queue>

#include <stack>

#include <map>

#include <set>

#include <vector>

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

#define repd(i,x,n) for(int i=x;i<=n;i++)

#define pii pair<int,int>

#define pll pair<long long ,long long>

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

#define MS0(X) memset((X), 0, sizeof((X)))

#define MSC0(X) memset((X), '\0', sizeof((X)))

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define gg(x) getInt(&x)

using namespace std;

typedef long long ll;

inline void getInt(int* p);

const int maxn=1000010;

const int inf=0x3f3f3f3f;

/*** TEMPLATE CODE * * STARTS HERE ***/

ll n;

ll dp[maxn];

ll a[maxn];

ll k;

int main()

{

    gbtb;

    cin>>n>>k;

    repd(i,1,n)

    {

        cin>>a[i];

    }

    dp[1]=0;

    dp[0]=0;

    repd(i,2,n)

    {

        dp[i]=inf;

    }

    repd(i,2,n)

    {

        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;

}



inline void getInt(int* p) {

    char ch;

    do {

        ch = getchar();

    } while (ch == ' ' || ch == '\n');

    if (ch == '-') {

        *p = -(getchar() - '0');

        while ((ch = getchar()) >= '0' && ch <= '9') {

            *p = *p * 10 - ch + '0';

        }

    }

    else {

        *p = ch - '0';

        while ((ch = getchar()) >= '0' && ch <= '9') {

            *p = *p * 10 + ch - '0';

        }

    }

}

// Optimized solution to the same problem (faster):
#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;

}


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





}