stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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]);

}




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

}


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



using namespace std;

#define ll long long int

#define Max 110000



int main() {

	int n,k;

	cin>>n>>k;

	int arr[n];

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

	{

		cin>>arr[i];

	}

	ll dp[Max+1];

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

	{

		dp[i]=INT_MAX;

	}

	dp[0]=0;

	//cout<<dp[1];

	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(arr[i+j]-arr[i]));

		}

	}

	cout<<dp[n-1];

	return 0;

}

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

#define ll long long int

using namespace std;

ll solve(int cost[],int n,int k)

{

	ll dp[n];

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

		dp[i]=INT_MAX;

	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(cost[i+j]-cost[i]));

	}

	return dp[n-1];

}

int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	int n,k;

	cin>>n>>k;

	int cost[n];

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

		cin>>cost[i];

	long long int ans=solve(cost,n,k);

	cout<<ans;

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

using namespace std;

char c[232],top; 

int main() {

	char ch;

	while(cin>>ch) {

		if(ch=='B'&&top)top--;

		if(ch!='B')c[top++]=ch;

	}

	for(int i=0;i<top;++i) cout<<c[i]; cout<<endl;

	return 0;

}

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

#include<cstring>

using namespace std;

char s[20], S[20];

int main()

{

	scanf("%s", s);

	int len = strlen(s);

	int cnt = 0;

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

	{

		if(s[i] == '1')

		{

			S[cnt] = '1';

			cnt++;

		}

		if(s[i] == '0')

		{

			S[cnt] = '0';

			cnt++;

		}

		if(s[i] == 'B')

		{

			if(cnt)cnt--;

			S[cnt] = '\0';

		}

	}

	printf("%s\n", S);

	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):
// # Problem A - Frog 2 - Algoritmos e Grafos - MATA52

#include<bits/stdc++.h>

#include<cstdlib>

using namespace std;



int frog2(int stones[], int n, int h[], int k_max){

    stones[0] = 0;

    stones[1] = abs(h[1] - h[0]);

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

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

            int w1 = stones[i];

            int w2 = abs(h[i - j] - h[i]);

            stones[i] = min(w1, stones[i - j] + w2);

        }

    }

    return stones[n - 1];

}



int main() {

    int n, k_max;

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

    int stones[n], h[n];

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

    printf("%d", frog2(stones, n, h, k_max));

    

    return 0;

}

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

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

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

int main()

{

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

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

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

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

	f[1]=0;

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

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

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

	printf("%d\n",f[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>

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

#define r(i, j) for (int i = 1; i <= j; i++)

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

using ll = long long;

using namespace std;

template <typename T>

using vec = std::vector<T>;







int main() {

    int N, M, Q;

    cin >> N >> M >> Q;

    vec<int> A(Q),B(Q),C(Q),D(Q);

    rep(i, Q) {

        cin >> A[i] >> B[i] >> C[i] >> D[i];

        --A[i];--B[i];

    }



    ll ans = 0;

    for (int a = 1; a <= M; ++a) {

        r(b, a) r(c, b) r(d, c) r(e, d) r(f, e) r(g, f) r(h, g) r(i, h)  r(j, i){

            // vec<int> Z ={a,b,c,d,e,f,g,h,i};

            vec<int> Z ={j,i,h,g,f,e,d,c,b,a};

            ll tmpans = 0;

            rep(k,Q){

                if(Z[B[k]] - Z[A[k]] == C[k]) tmpans += D[k];

            }

            ans = max(ans,tmpans);

        }

    }



    cout << ans << endl;

}

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

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

#define r(i, j) for (int i = 1; i <= j; i++)

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

using ll = long long;

using namespace std;

template <typename T>

using vec = std::vector<T>;



int N, M, Q;

int A[55], B[55], C[55], D[55];

int ans = 0;



void dfs(vec<int> Z) {

    int size = Z.size();

    if (size == N) {

        int tmpans = 0;

        rep(i, Q) {

            if (Z[B[i]] - Z[A[i]] == C[i]) tmpans += D[i];

        }

        ans = max(ans, tmpans);

        return;

    }



    Z.push_back(Z.back());

    for (int i = Z.back(); i <= M; ++i) {

        Z[size] = i;

        dfs(Z);

    }

}



int main() {

    cin >> N >> M >> Q;



    rep(i, Q) {

        cin >> A[i] >> B[i] >> C[i] >> D[i];

        --A[i];

        --B[i];

    }

    dfs(vec<int>(1, 1));

    cout << ans << endl;

}


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

using namespace std;

typedef long long ll;



void DFS(int& ans, int m, int n, int q, vector<int>& A, const vector<int>& a, const vector<int>& b, const vector<int>& c, const vector<int>& d)

{

	if (A.size() == n) {

		int tmp = 0;

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

			if (A[b[i]] - A[a[i]] == c[i]) tmp += d[i];

		}

		ans = max(ans, tmp);

		return;

	}



	int start = A.empty() ? 1 : A.back();

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

		A.push_back(i);

		DFS(ans, m, n, q, A, a, b, c, d);

		A.pop_back();

	}

}



int main()

{

	int n, m, q;

	cin >> n >> m >> q;

	vector<int> a(q), b(q), c(q), d(q);

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

		cin >> a[i] >> b[i] >> c[i] >> d[i];

		a[i]--, b[i]--;

	}



	int ans = 0;

	vector<int> A;

	DFS(ans, m, n, q, A, a, b, c, d);

	cout << ans << endl;

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

#include<queue>

#include<string>

#include<cstring>

#include<cmath>

#include<map>

#include<iostream>

#include<algorithm>

#define ll long long

const ll mod = 1e9+7;

using namespace std;

int a,b,c;

int main()

{

	while(~scanf("%d%d%d",&a,&b,&c)) {

		int flag = 0;

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

			if((b*k+c) % a == 0) {

				printf("YES\n");flag = 1;break;

			}

		}

		if(!flag) printf("NO\n");

	}

	

	

	return 0 ;

}

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

#include<cmath>



int a,b,c;



int main()

{

	scanf("%d %d %d",&a,&b,&c);

	int flag = 0;

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

		if((b * k + c) % a == 0) {

			printf("YES\n");

			flag = 1;

			break;

		}

	}

	if(!flag) printf("NO\n");

	return 0;

}

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

using ll = long long;

using namespace std;



int main() {

  int A,B,C;

  cin>>A>>B>>C;

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

    if ((A*i-C)%B==0&&(A*i-C)>=0){

      cout << "YES"<<endl;

      return 0;

    }

  }

  cout << "NO"<<endl;

  return 0;

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

#include <vector>

#include <map>

using namespace std;



int N, K;

int h[111111];

int dp[111111];

int main(){

    cin >> N >> K;

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

        cin >> h[i];



    fill_n(dp, 111111, 1<<30);

    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+j]-h[i]));

        }

    }

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

    return 0;

}


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

using namespace std;

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

template <class T> ostream& operator<<(ostream& os, const vector<T>& v){ os << "{"; for(int i=0; i<v.size(); i++) os << v[i] << (i+1==v.size() ? "" : ", "); os << "}"; return os; }

template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }



int main(){

    int N, K;

    cin >> N >> K;

    vector<int> h(N);

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



    vector<int> dp(N, 1<<30);

    dp[0] = 0;



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

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

            int to = i + j;

            if(to >= N) continue;

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

        }

    }

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

using namespace std;

long long dp[100200];

int main(){

	int n, K;

	cin >> n >> K;

	vector<int> h(n);

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

		cin >> h[i];

	}

  

for(int i = 0; i < 100200;i++) dp[i] = 10000000000;

  	dp[0] = 0;

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

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

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

      }

    }

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

	

}


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

using namespace std;

long long  dp[100004];

int main(){

	int n, K;

	cin >> n >> K;

	vector<int> h(n);

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

		cin >> h[i];

	}

  	long long tmp;

  	dp[0] = 0;

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

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

      tmp = 10000000000;

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

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

      }

      dp[i] = tmp;

    }

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

	

}


// 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 main() {

    int k, x;

    cin >> k >> x;

    int begin = x - (k-1);

    int end = x + (k-1);



    vector<int> v(2000001);

    for (int i = begin; i <= end; ++i) {

        cout << i << " "s;

    }

}

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

using namespace std;





int main() {

    int k, x;

    cin >> k >> x;

    int begin = x - (k-1);

    int end = x + (k-1);



    for (int i = begin; i <= end; ++i) {

        cout << i << " "s;

    }

}

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

#include<algorithm>

#include<string>

#include<iomanip>

#include<cmath>

using namespace std;



int main() {

	int k;

	cin >> k;

	string s;

	cin >> s;

	int len = s.size();

	if (len <= k)cout << s;

	else {

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

			cout << s[i];

		}

		cout << "...";

	}



}


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

#include <string.h>

int main(){

	int k;

	scanf("%d",&k);

	char s[100];

	scanf("%s",&s);

	int lol = strlen(s);

	s[k] = '\0';

	for(int i=0;s[i]!='\0';i++){

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

	}

	if(k<lol){

		printf("...");

	}

return 0;

}

// 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):
/*

				" اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "

*/





#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#define f for

#define ll long long

#define ull unsigned long long

#define endl "\n"

using namespace std;

#define BI  acos(-1)

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

void Open()

{

#ifndef ONLINE_JUDGE

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

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

#endif

}

ll gcd(ll a, ll b)

{

	return b == 0 ? a : gcd(b, a % b);

}

void madesetprobblem()

{

	//$ $

	//less than or equal => $ 1 \leq 100 $

	//  $(1 \le A,B \le 10^5 )$

	// a[i] => $a_i$

	// a[i][j] => $a_{i,j}$

	// double qoutes ''yext''

	/*

		image

		\begine{center}

		\includegraphics{image.jpg}

		\end{center}

	*/

	/*

		list

		\begin{itemize}

			\item first one

			\item second one

		\end{itemize}

	*/

}

int n,k;

vector<int>a(1e5 + 2),dp(1e5+2,-1);

int ans(int c = 0)

{

	if (c == n-1)

		return 0;

	int ans1 = INT_MAX, ans2 = INT_MAX;

	if (dp[c] != -1)

		ans1 = dp[c];

	else

	{

		f(int i = 1; i <= k; i++)

		{

			if (c + i < n)

				ans1 = min(ans1,(abs(a[c] - a[c + i]) + ans(c + i)));

		}

		dp[c] = ans1;

	}

	//cout <<c<<" "<< ans1 << " " << ans2 << endl;

	return ans1	;

}

int main()

{

	// توكلت علي الله 

//Open();

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin >> n>>k;

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

	{

		cin >> a[i];

	}

	cout << ans();

}

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

using namespace std;



const int N=1e5+30;

typedef long long ll;

int n,k,arr[N];

int dp[N];

ll solve(int i){

    if(i>=n)return 1e15;

    if(i==n-1){

        return 0;

    }



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

    ll mn=1e18;

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

        ll path1=abs(arr[i]-arr[i+j])+solve(i+j);

        mn=min(path1,mn);

    }

    return dp[i]= mn;

}

int main()

{

    memset(dp,-1,sizeof dp);

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

    for(int i=0;i<n;i++)scanf("%d",arr+i);

    cout<<solve(0)<<endl;

}




// 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<stdio.h>

#include<math.h>

#include<algorithm>

#include<iostream>

#include<cstring> 

#include<cstdlib>

#include<set>

#include<vector>

using namespace std;

#define ll long long

const int maxn = 2e5 + 5;

const int inf =9999999;



int main()

{     int t;

	cin>>t;

    char a[100];

    cin>>a;

    if(strlen(a)<=t)

    cout<<a<<endl;

    else{

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

    cout<<a[i];

    cout<<"..."<<endl;

}

    

	

}

	 

		

	  	

	  

	  


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

#include <string.h>

int main(){

	int k;

	scanf("%d",&k);

	char s[100];

	scanf("%s",&s);

	int lol = strlen(s);

	s[k] = '\0';

	for(int i=0;s[i]!='\0';i++){

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

	}

	if(k<lol){

		printf("...");

	}

return 0;

}

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

#define ll long long

#define ull unsigned long long

using namespace std;

ll n,k,a[100009],dp[100009];

	

int main (){

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

	cin >> n >> k;

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

		cin >> a[i];

	}

	for (int i=n; i>=1; i--){

		ll tmp=1e15;

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

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

		}

		if (tmp!=1e15)dp[i]=tmp;

	}

	cout << dp[1] << endl;

}

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

}


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





}