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

#include<cmath>

#include<cstdio>

#include<cstring>

using namespace std;

int n,m,f[1100000],w[1100000],k;

int main ()

{

	cin>>n>>k;

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

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

	{

		cin>>w[i];

	}

	f[1]=0;

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

	{

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

		{

			if(i-j<=0) break;

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

		}

	}	

	cout<<f[n];

}

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

#include<iostream>

#include<string.h>

#include<algorithm>

using namespace std;

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

int xg(int a){

	if(a>0)

		return a;

	return -a;

}

int main(){

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

	a[0]=0;

	cin>>n>>k;

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

		cin>>dp[i];

	}

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

		a[i]=xg(dp[i]-dp[0]);

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

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

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

	}

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

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

	if(a[n-1]==0x3f3f3f3f)

		cout<<0<<endl;

	else

		cout<<a[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;

typedef long long ll;

ll cost[100002], n, k;

ll dp[100002];

ll jump(ll pos)

{

    if(dp[pos]!=-1)

    return dp[pos];

    if(pos == n)

    return 0;

    if(pos > n)

    return INT_MAX;

    ll ans = 0, prev = INT_MAX;

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

            ans = (abs(cost[pos] - cost[pos+i]) +  jump(pos+i));

            prev = min(prev, ans);

        }   

    return dp[pos] = prev;

}







int main(){

    cin >> n >> k;

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

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

        cin >> cost[i];

    ll ans = jump(1);

    cout << ans << endl;

return 0;

}

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

using namespace std;

typedef long long ll;

typedef double dd;

const int INF = 1e9 + 5;

void solve()

{

		int n;

		cin >> n;

		vector <int> h(n, 0);

		int k;

		cin >> k;

		for(int &x : h)

			cin >> x;

			

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

				}

			}

		}

		

		cout << dp[n-1];

		

}



int main()

{

		ios::sync_with_stdio(false);

		cin.tie(nullptr);

		ll t = 1;

	//	cin >> t

		while(t--)

		{

			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):
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

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

typedef long long ll;

using namespace std;

ll n , k ,arr[100005] , memo[100005];

ll solve(ll i)

{

	if(i == n - 1)

		return 0;

	ll &ans = memo[i];

	if(~ans)

		return ans;

	ans = 1e9;

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

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

	return ans;

}

int main()

{

	abdelrahman010

	cin >> n >> k;

	memset(memo , -1 , sizeof memo);

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

		cin >> arr[i];

	cout << solve(0);

	return 0;

}


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

}

// 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 ALL(x) begin(x),end(x)

typedef long long ll;

int OO = 0x3f3f3f3f;



int n, k;

int arr[100005];

int mem[100005];



int solve(int x)

{

    if(x == n-1)

        return 0;



    if(mem[x] != OO)

        return mem[x];



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

    {

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

    }



    return mem[x];

}



int main()

{

    cin >> n >> k;

    memset(mem, OO, sizeof mem);

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

    {

        cin >> arr[i];

    }

    cout << solve(0) << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<unordered_map>

using namespace std;

//<DEFINE>

void Fast() {

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

}

void File() {

#ifndef ONLINE_JUDGE

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

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

#endif

}

#define ll long long

#define ull unsigned long long

#define mod 1000000007

ll gcd(ll a, ll b) {

	return!b ? a : gcd(b, a % b);

}

#define fix(n) cout << fixed << setprecision(n);

#define numOfOnes(n) __builtin_popcount(n)

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

#define skip continue

#define stop(n) return cout<<n,0

#define pi  acos(-1)

#define endl "\n"

#define ceil(x,y) (x+y-1)/y

//</DEFINE>



/*█▀▀▄░░░░░░░░░░░▄▀▀█

░█░░░▀▄░▄▄▄▄▄░▄▀░░░█

░░▀▄░░░▀░░░░░▀░░░▄▀

░░░░▌░▄▄░░░▄▄░▐▀▀

░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█

░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█

▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█

█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀

░▀▄░░▀░░▀▀▀░░▀░░░▄█▀

░░░█░░░░░░░░░░░▄▀▄░▀▄

░░░█░░░░░░░░░▄▀█░░█░░█

░░░█░░░░░░░░░░░█▄█░░▄▀

░░░█░░░░░░░░░░░████▀

░░░▀█▄▄▄▀▀▀▀▄▄▄█▀*/

int n,k;

vector<int>v;

ll res[100001];

//<FUNCTIONS>

ll solve(int idx = 0) {

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

	if (~(res[idx]))return res[idx];

	ll mn = INT_MAX;

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

		if (idx + i >= n)break;

		mn = min(mn, solve(idx + i) + abs(v[idx] - v[idx + i]));

	}

	return res[idx] = mn;

}

//</FUNCTIONS>		

int main() {

	Fast();

	cin >> n >> k;

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

		res[i] = -1;

	v.resize(n);

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

	cout << solve() << 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;



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)

        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 print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

const ll INF = 1 << 30;

typedef pair<ll, ll  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<ll,payar  > > vpp;

vector < payar >  vp;



ll n ;

int ara[200005];



ll dp[200005];



ll par[100005];



ll k ;

ll cal(int pos)

{

      if(pos>n)

    {

        return 10000000000000000;

    }

    if(dp[pos]!=-1)

    {

        return dp[pos];

    }

    if(pos==n )

    {

        return 0 ;

    }







    dp[pos]=abs(ara[pos+1]-ara[pos]) +cal(pos+1);



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

    {



            dp[pos]=min( abs(ara[pos]-ara[pos+i])+ cal(pos+i), dp[pos] );



    }

    return dp[pos];

}

int main()

{



    cin>>n>>k;

    memset(dp,-1,sizeof dp);

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

    {

        //  cin>>ara[i];

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

    }

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

    {

        ara[i]=10000000000000000;

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

    }

    cout<<cal(1);







    return  0 ;

}


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

using namespace std;





int main()

{

    int n,k;

    cin>>n>>k;



    int ar[n+10],ans[n+10];



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

    {

        cin>>ar[i];

        ans[i]=INT_MAX;

    }



    ans[0]=0;





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

    {

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

        {

            int r1,r2;



            r1=abs(ar[j]-ar[i])+ans[i];

            ans[j]=min(r1,ans[j]);

        }



    }



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

#include<string>

using namespace std;

int main(){

	string s;

	int k;

	cin>>k>>s;

	if(s.length()>k){

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

			cout<<s[i];

		}

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

	}

	else cout<<s<<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>

using namespace std;

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

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

#define pb push_back

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

typedef long long ll;

typedef pair<int,int> pii;

typedef double db;

ll n,ans,r,g,b;

char s[4010];

int main(){

	cin>>n;

	scanf("%s",s+1);

	rep(i,1,n){

		if(s[i]=='R')	r++;

		if(s[i]=='G')	g++;

		if(s[i]=='B')	b++;

	}

	ans=r*g*b;

	rep(len,1,n){

		rep(j,1,n-2*len){

			if(s[j]!=s[j+len]&&s[j]!=s[j+2*len]&&s[j+len]!=s[j+2*len])

				ans--;	

		}

	}

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

	return 0;

}


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

#include<algorithm>



using namespace std;



typedef long long LL;



const int N = 4010;



int r[N], g[N], b[N];

char s[N];



int main()

{

    int n;

    scanf("%d", &n);

    scanf("%s", s + 1);

    

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

    {

        r[i] = r[i - 1], g[i] = g[i - 1], b[i] = b[i - 1];

        if(s[i] == 'R')  r[i] ++;

        else if(s[i] == 'G')  g[i] ++;

        else  b[i] ++;

    }

    

    LL res = 0;

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

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

            if(s[i] != s[j])

            {

                char c = (int)'R' + 'G' + 'B' - s[i] - s[j];

                if(c == 'R')  res += r[i - 1];

                else if(c == 'G')  res += g[i - 1];

                else  res += b[i - 1];

                if(i - (j - i) >= 1 and s[i - (j - i)] == c)  res --;

            }

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

    return 0;

}

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

using namespace std;

typedef long long ll;

const int mod = 1000000007;



int main()

{

	int n;

	string s;

	cin >> n >> s;



	ll r = 0, g = 0, b = 0;

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

		if (s[i] == 'R') r++;

		else if (s[i] == 'G') g++;

		else b++;

	}



	ll all = r * g * b;

	ll sub = 0;

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

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

			int k = 2 * j - i;

			if (k < n && s[i] != s[j] && s[j] != s[k] && s[k] != s[i]) sub++;

		}

	}

	cout << all - sub << endl;

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



using namespace std;



int main(){

    int N, len;

    cin>>N;

    string s;

    cin>>s;

    len = s.size();

    if (len > N){

        for (int i = len; i > N; i--){

            s.pop_back();

        }

        for (int j = 0; j < 3; j++){

            s.push_back('.');

        }

        cout<<s;

    }

    else {

        cout<<s;

    }



    return 0;

}


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



int main(){

	int k;

	char word[100];

	scanf("%d", &k);

	scanf("%s", &word);

	

	int len = 0;

	

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

		len++;

	}

	

	if(len <= k){

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

	}

	else{

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

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

		}

		printf("...\n");

	}

	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>

using namespace std;

int n;

string s;

int main() {

	scanf("%d",&n);

	cin>>s;

	if((s.size())>n) {

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

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

		printf("...");

	}

	else

	    cout<<s;

}

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

using namespace std;

int n ,k;

vector<int>vi;

long long mem[200005];

bool vis[200005];

long long solve(int indx ){

	if(indx == n-1)

		return 0;

	if(vis[indx])

		return mem[indx];

	vis[indx] = true;

	mem[indx] = 1e13;

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

		mem[indx] = min(mem[indx] , solve(i ) + abs(vi[indx] - vi[i]));

	}

	return mem[indx];

}

//long long n , w;

//vector<pair<long long , long long>>vi;

//long long mem[105][100005];

//bool vis[105][100005];

//long long ans = 0;

//long long sol(int indx , int cost){

//	if(indx == n)

//		return 0;

//	if(vis[indx][cost])return mem[indx][cost];

//	mem[indx][cost] = 0;

//	long long fch = sol(indx + 1 , cost);

//	long long sch = 0;

//	if(mem[indx][cost] + vi[indx].first <= w){

//		sch = sol(indx + 1 , cost + vi[indx].second) + vi[indx].first;

//	}

//	return mem[indx][cost] = max()

//}

int main()

{

//	cin>>n>>w;

//	vi.resize(n);

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

//		cin>>vi[i].first>>vi[i].second;

//

//	cout<<sol(0,0);



	cin>>n>>k;

	vi.resize(n);

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

		cin>>vi[i];

	cout<<solve(0);

	return 0;

}




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





}