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

using namespace std;

int a,b,c,d,cost[1000000],weight[100],INF=1e9;

vector<int>dp(1000000,1e9);



void func(int a,int k)

{

    dp[1]=0;

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

    {

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

        {

            if(j<=a)

            {

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

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

            }

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

        }

    }

}



int main()

{

    int k;

    cin>>a>>k;

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

    {

        cin>>cost[i];

    }

    func(a,k);

    cout<<dp[a]<<endl;

    //cout<<nCr(a,b)<<endl;



}


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





int main()

{

	int n, k;

	cin >> n >> k;



	vector<int> h(n), dp(n, 1 << 30);    



	for(int &x : h)

		cin >> x;



	dp[0] = 0;

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

	{

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

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

	}



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



}

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

#include <algorithm>

#include <cmath>

using namespace std;



int h[(int)1e5], dp[(int)1e5], K, N;





int main()

{

	K = N = 0;

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

	{

		dp[j] = 1e9;

	}

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

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

	{

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

	}

	dp[1] = 0;

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

	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\n", dp[N]);

	return 0;	

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

#include <algorithm>



using namespace std;



int main(void) {

    int n, k;



    cin >> n >> k;



    int h[n];



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

        cin >> h[i];

    }



    int m[n + 1];



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

        m[i] = 999999999;

    }



    m[0] = 0;



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

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

            if (i + j < n) {

                m[i + j] = min(m[i + j],

                               m[i] + abs(h[i] - h[i + j]));

            }

        }

    }



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



    return 0;

}


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

using namespace std;



int main(){

	int i, n, k, x, tot=0;

	cin>>n>>k;

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

		cin>>x;

		if(x>=k){

			tot++;

		}

	}

	cout<<tot<<endl;

	return 0;

}

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

int main(){

	int i,m,k,t=0,cnt=0;

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

	int a[m];

	for(i=0;i<m;i++){

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

	}

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

		if(a[i]>a[i+1]){

			t=a[i+1];

			a[i+1]=a[i];

			a[i]=t;

		}

	}

	if(a[m-1]<k){

		printf("0");

	}else{

	

	for(i=0;i<m;i++){

		if(a[i]>=k)

		cnt++;

	}

	printf("%d",cnt);

}

	return 0;

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

#include <cmath>

using namespace std;



typedef long long ll;



const int maxn = 1e5 + 5, maxh = 1e4 + 5;



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

{

    ll n, k, h[maxn], dp[maxn] = {0};

    cin >> n >> k;

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

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



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

        dp[i] = maxn * maxh;

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

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

    }



    cout << dp[n] << endl;

    return 0;

}

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

#include<iomanip>

#include<cstdio>

#include<cmath>

#include<vector>

#include<string>

#include<algorithm>

#include<queue>

#include<set>

#include<map>

#include<limits>

#include<stack>



using namespace std;



typedef unsigned long long ll;

typedef pair<int,int> ii;

typedef vector<pair<int,int> > vii;

typedef vector<vii> vvii;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<ll> vll;

typedef vector<vll> vvll;

typedef vector<bool> vb;

typedef vector<vb> vvb;

typedef vector<char> vc;

typedef vector<vc> vvc;

typedef vector<string> vs;



int INF = std::numeric_limits<int>::max();

int NINF = std::numeric_limits<int>::min();

int MOD = 1e9+7;



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

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



//knight moves

int cc[] = {1,2,2,1,-1,-1,-2,-2};

int rr[] = {-2,-1,1,2,-2,2,1,-1};



int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);



    int n,k;

    cin >> n >> k;



    vi hight(n);

    vll cost(n,INF);



    cin >> hight[0];

    cost[0] = 0;



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

    {

        cin >> hight[i];



        if(i > k)

        {

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

            {

                ll one = abs(hight[i]-hight[j]);



                cost[i] = min(one+cost[j],cost[i]);

            }

            //cout << i << " " << cost[i] << endl;

        }

        else

        {

            cost[i] = abs(hight[0]-hight[i]);

        }

    }



    cout << cost[n-1];



    return 0;

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

using namespace std;

#define MxV (1<<30)

long long int N, K, p;

int main(){

	cin >> N >> K;

	long long int arr[N];

	long long int mem[N];

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

		cin >> arr[i];

	mem[0] = 0;

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

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

		p = MxV;

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

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

		mem[i] = p;

	}

	cout << mem[N-1];

}


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

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

 

#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

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

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

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

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

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

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

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

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

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;

 

int n, dp[maxn], k;

int h[maxn];

main () {

	cin >> n >> k;

	forn(i, 1, n)

		cin >> h[i];

	dp[1] = 0;

	forn(i, 2, n){

		dp[i] = inf * inf;

		forn(step, 1, k){

			int j = i - step;

			if(j >= 1)

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

		}

	}

	cout << dp[n];



}

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

}
// Slower C++ solution:
////////////////////////////////////

//          ,.                    //

//         (_|,.                  //

//        ,' /, )_______   _      //

//     __j o``-'        `.'-)'    //

//    (")                 \'      //

//     `-j                |       //

//        `-._(           /       //

//           |_\  |--^.  /        //

//          /_]'|_| /_)_/         //

//             /_]'  /_]'         //

//                                //

//           PIGMEISTER™          //

//                                //

////////////////////////////////////



#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define ull unsigned long long

#define mod (ll)1000000007

#define sidha(i, x, n) for(ll i=(ll)(x);i<(ll)(n);i++)

#define ulta(i, x, n) for(ll i=(ll)(x);i>=(ll)(n);i--)

#define mii map<ll, ll>

#define vi vector<ll>

#define pii pair<ll, ll>

#define pb push_back

#define mp make_pair

#define modInv(x) powah(x, mod-2)

#define test ll t;cin>>t;while(t--)

#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define endl '\n'

//ll powah(ll, ll);



vi dp(100005, -1), h;



ll minCost(ll n, ll k) {

    if (dp[n] > -1) return dp[n];



    if (n == 1)

        dp[n] = 0;

    else {

        ll miin = LLONG_MAX, limit = min(k+1, n);

        sidha (i, 1, limit)

            if (miin > minCost(n-i, k) + abs(h[n] - h[n-i]))

                miin = minCost(n-i, k) + abs(h[n] - h[n-i]);

        dp[n] = miin;

    }

    return dp[n];

}



int main()

{

    jaldi;

    

    ll n, k, a;

    cin >> n >> k;

    h.pb(0);

    sidha (i, 0, n) {

        cin >> a;

        h.pb(a);

    }

    cout << minCost(n, k) << endl;

    

    return 0;

}



// ll powah(ll x, ll y) { 

//     ll res = 1;

//     x = x % mod;

//     while (y > 0) { 

//         if (y & 1) 

//             res = (res*x) % mod; 

//         y = y>>1;

//         x = (x*x) % mod; 

//     } 

//     return res; 

// }

// Faster C++ solution for the same problem (measured speedup: 1.8110589431498996x):
//   PIGMEISTER™   //



#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define ld long double

#define sidha(i,x,n) for(ll i=(ll)(x);i<(ll)(n);++i)

#define ulta(i,x,n) for(ll i=(ll)(x);i>=(ll)(n);--i)

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

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

#define test ll _;cin>>_;while(_--)

#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define MOD (ll)1000000007

//#define MOD (ll)998244353

#define PI (ld)3.141592653589793238

#define MAX 100005



int main()

{

    jaldi;

    

    ll n, k;

    cin >> n >> k;

    ll h[n];

    sidha (i, 0, n) cin >> h[i];



    ll dp[n] = {0};

    sidha (i, 1, n) {

        dp[i] = 1e18;

        sidha (j, max(i-k, 0ll), i) {

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

        }

    }



    cout << dp[n-1] << '\n';

    

    return 0;

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

#include<vector>

using namespace std;

int main()

{

	int s;

	vector <int> vec;

	while(cin>>s)

	{

		vec.push_back(s);

		while(1)

		{

			int answer=0;

			if(s%2==0)

			{

				s=s/2;

				vec.push_back(s);

			}

			else

			{

				s=(3*s)+1;

				vec.push_back(s);

			}

			for(int i=0;i<vec.size();i++)

			{

				for(int j=i+1;j<vec.size();j++)

				{

					if(vec.at(i)==vec.at(j))

					{

						answer=j+1;

						cout<<answer;

					}

				}

			}

			if(answer!=0)

			{

				break;

			}

		}

	}

	

}

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



using namespace std;





int mp[150000];

int arr[150000];



int main(){

	int n;

	cin>>n;

	mp[n]=1;

	arr[1]=n;

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

		if(arr[i-1]%2==0){

			arr[i]=arr[i-1]/2;

		}else{

			arr[i]=arr[i-1]*3+1;

		}

		

		if(mp[arr[i]]!=0){

			cout<<i;

			return 0;

		}else{

			mp[arr[i]]=i;

		}

		

	}

	return 0;

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

using namespace std;

int main()

{

    long long n,f=1,p=1000000007;

    cin>>n;

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

    {

        f*=i;

        f=f%p;

    }

    cout<<f<<endl;

    return 0;

}

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

int n,i;

long long a;

int main ()

{

	scanf ("%d",&n);

	a=1;

	for (i=1;i<=n;i++) a=a*i%1000000007;

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

	return 0;

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

#include <iostream>

#include <algorithm>



using namespace std;



int N, A, B, used[100], rings[100], G[100][199];



int rec(int pos)

{

	int ret = 1; used[pos] = 1;



	for(int i = 0; i < rings[pos]; i++)

	{

		if(!used[G[pos][i]])

		{

			ret = max(ret, rec(G[pos][i]) + 1);

		}

	}



	used[pos] = 0;



	return ret;

}



int main()

{

	int N, A, B;



	while(true)

	{

		scanf("%d", &N);

 

		if(N == 0) { break; }



		memset(used, 0, sizeof(used));

		memset(rings, 0, sizeof(rings));



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

		{

			scanf("%d", &A); A--;

			scanf("%d", &B); B--;



			G[A][rings[A]] = B; rings[A]++;

			G[B][rings[B]] = A; rings[B]++;

		}



		int nodes = 0;



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

		{

			if(rings[i])

			{

				nodes++;

			}

		}



		int ret = 0;



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

		{

			ret = max(ret, rec(i));



 			if(ret == nodes) break;

		}

 

		printf("%d\n", ret);

	}



	return 0;

}

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

#include <algorithm>

#pragma warning(disable : 4996)

using namespace std;

int N, A, B, v[100], r[100], G[100][199];

int rec(int pos) {

	int ret = 0; v[pos] = 1;

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

		if (!v[G[pos][i]]) ret = max(ret, rec(G[pos][i]));

	}

	v[pos] = 0;

	return ret + 1;

}

int main() {

	while (scanf("%d", &N), N) {

		for (int i = 0; i < 100; i++) r[i] = 0;

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

			scanf("%d%d", &A, &B), A--, B--;

			G[A][r[A]++] = B, G[B][r[B]++] = A;

		}

		int ret = 0;

		for (int i = 0; i < 100; i++) ret = max(ret, rec(i));

		printf("%d\n", ret);

	}

	return 0;

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

#include <stdlib.h>

#include<iostream>

#include<vector>

#include<cmath>

#include<cctype>

#include<algorithm>

#include<cstring>

#include<queue>

#include<map>

#include<set>

#define INF 0x3f3f3f3f

#define MAXN 1e16

#define DEBUG 0



using namespace std;

typedef long long ll;



string a,b;

int main()

{

    #if DEBUG

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

    #endif // DEBUG

    cin>>a>>b;



    int ok=0;

    for(int i=0;i<a.length();i++)

    {

        if(a!=b)

        {

            string tem=b.substr(0,1);



            b.erase(b.begin());

            b=b+tem;

        }

        else {ok=1;printf("Yes");break;}

    }



    if(ok==0) printf("No");

}




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

#include<cstring>



using namespace std;



void exchange(char a[], int left, int right) {

	int i = left, j = right;

	char temp;

	while (i < j) {

		temp = a[i];

		a[i] = a[j];

		a[j] = temp;

		i++;

		j--;

	}

}



void reserve(char a[], int n, int i) {

	exchange(a, 0, i - 1);

	exchange(a, i, n - 1);

	exchange(a, 0, n - 1);

	return;

}



int main() {

	char str[110], str1[110];

	int n, round = 0;

	scanf("%s", str);;

	scanf("%s", str1);

	if (strlen(str) != strlen(str1)) {

		printf("No");

		return 0;

	}

	n = strlen(str);

	while (round <= n) {

		reserve(str, n, 1);

		if (strcmp(str, str1) == 0) {

			printf("Yes");

			return 0;

		}

		round++;

	}

	printf("No");

	return 0;

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

#include <cstring>

using namespace std;

int dp[100000];

int n,a[100000],k;

 

int ans(int i)

{

	if(i==n-1)

		return 0;

 

	if(dp[i]!=-1)

		return dp[i];

  

	int best=1e9;

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

	{

		if(i+j<n)

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

	}

	return dp[i]=best;

}

 

int main()

{

  	ios_base::sync_with_stdio(0);            

	cin.tie(NULL);

	cout.tie(NULL);

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

	cin>>n>>k;

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

		cin>>a[i];

 

	cout<<ans(0)<<endl;

 

	return 0;

}

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

#include <cstring>

using namespace std;

int dp[100000];

int n,a[100000],k;



int topdown()

{

	dp[n-1]=0;



	for(int i=n-2;i>=0;i--)

	{

		dp[i]=1e9;

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

		{

          	if(i+j<n)

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

		}

	}

	return dp[0];

}



int main()

{

  	ios_base::sync_with_stdio(0);            //fast input and output

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>k;

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

		cin>>a[i];



	cout<<topdown();



	return 0;

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



#define fst first

#define snd second

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

#define pb push_back

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

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

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

#define DBG(x) cerr <<#x << " = " << (x) << "\n"



#define SQR(x) ((x) * (x))



#define PI 3.1415926535897932385

#define INF 1000111222

#define eps 1e-7

#define maxN 11



using namespace std;



typedef long long ll;

typedef vector<ll> vll;

typedef pair<ll,ll> pll;

typedef pair<int,int> pii;

typedef vector<string> vstr;

typedef vector<vll> vvll;

typedef vector<bool> vbool;

typedef vector<pll> vpll;

typedef vector<vpll> vvpll;

typedef vector<vbool> vvbool;



vll ns,dp;

ll n,k;



ll solve(ll pos){

	ll & d = dp[pos];

	if(d != -1) return d;

	ll v=INF;

	fore(i,pos+1,min(pos+k+1,n)) v = min(v,abs(ns[i]-ns[pos])+solve(i));

	if(pos == (n-1)) v = 0;

	return d = v;

}



int main(){FIN

	

	cin >> n >> k;

	ns.resize(n);

	dp.assign(100010,-1);

	fore(i,0,n)cin >> ns[i];

	

	solve(0);

	

	cout << dp[0] << "\n";



	return 0;

}


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

 

using namespace std;

 

typedef long long ll;

typedef vector<ll> vi;

typedef long double td;

typedef vector<td> vd;

typedef pair<ll,ll> ii;

typedef vector<ii> vii; typedef vector<bool> vb;

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

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

#define forn(i, n) forr(i, 0, n)

#define pb push_back

#define mp make_pair

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

#define DBG(x) cerr << #x << " = " << (x) << endl

#define show(v,n) cerr << #v << " = "; forn(i,n) cerr << v[i] << " "; cerr << endl;

#define esta(x,c) ((c).find(x) != (c).end())

#define RAYA cerr << "===============================" << endl

int MAXN = 500005;





ll solve(vector <int> &v, int n, int k)

{

	ll MAXN = (ll)(1e18);

	vector <ll> dp(n,MAXN);

	dp[0]=0;

	forr(i,1,n)

	{

		forr(j,1,k+1)

		{

			if(i-j < 0) break;

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

		}

	}

	return dp[n-1];

}

 

int main()

{ 	

	FIN;

	#ifdef input

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

	#endif



	int n, k;

	cin >> n >> k;

	vector <int> v(n);

	forn(i,n) cin >> v[i];

	cout << solve(v,n,k) << endl;

    return 0;

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

}

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

using namespace std;

typedef long long ll;

const int inf=0x3f3f3f3f;

const int N=2e5+5;

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

int main(){

	cin>>n>>k;

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

		cin>>a[i];

	dp[1]=0;

    dp[0]=0;

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

    	dp[i]=inf;

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

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

        {

            if(i-j<1) break;

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

        }

		cout<<dp[n];

	return 0;

}