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

#include <string.h>

#include <stdlib.h>

//#include <math.h>

//#include <algorithm>

#include <vector>



#define MIN(a, b) ((a)>(b)? (b): (a))



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

{

    long n, k;

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



    std::vector<long> h(n);

    long i, j;

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

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

    }



    long c[100001]={0};



    c[0]=0;

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

        c[i]=c[i-1]+abs(h[i-1]-h[i]);

        for(j=2; j<=k && i-j>=0; j++) {

            long tmp = c[i-j]+abs(h[i-j]-h[i]);

            c[i] = MIN(c[i], tmp);

        }

    }

    printf("%ld\n", c[n-1]);



    return 0;

}


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

#include <string>

#include <cstring>

#include <stdlib.h>

#include <math.h>

#include <algorithm>

#include <vector>

#include <set>

#include <map>

#include <queue>

#include <list>

#include <iterator>

#include <assert.h>

#pragma warning(disable:4996) 



typedef long long ll;

#define MIN(a, b) ((a)>(b)? (b): (a))

#define MAX(a, b) ((a)<(b)? (b): (a))

#define LINF 9223300000000000000

#define INF 2140000000

const long long MOD = 1000000007;

using namespace std;



ll dp[100005];



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

{

    int n,K;

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



    vector<int> h(n);

    int i;

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

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

    }

    dp[0]=0;

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

        dp[i]=INF;

        int j;

        for(j=MAX(0,i-K); j<i; j++) {

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

        }

    }

    printf("%lld\n", dp[n-1]);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする

#include <math.h> //数学関数と数学定数を利用する

using namespace std;

typedef long long int ll;



void chmin(ll &a, ll b){

  if (a > b) a = b;

}

ll INF = 1LL << 60;

ll dp[100010]; //DPテーブル

int h[100010];

int K;



ll rec(int N){

  

  ll res = INF;

  //ベースケース

  if (N == 0){

    return 0;

  }



  //計算済みであればその値を返す

  if (dp[N] < INF){

    return dp[N];

  }



  //各ジャンプ毎にmin値を計算する（再帰）

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

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

  }



  return dp[N] = res; //DPテーブルに記録しながらリターン

}





int main(){

  int N; cin >> N >> K;

  

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

    cin >> h[i];

  }

  

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

    dp[i] = INF;

  }  

  

  cout << rec(N-1);

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.2857054914043975x):
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする

#include <math.h> //数学関数と数学定数を利用する

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

#define PI 3.141592653589793

using namespace std;

typedef long long ll;

template<class T> void chmax(T &a,T b) { if (a<b) a=b;}

template<class T> void chmin(T &a,T b) { if (a>b) a=b;}

int gcd(int a, int b){//ユークリッドの互除法

  if (a < b) gcd(b,a); //aの方がbよりでかいのが前提

  if (b == 0) return a; //aをbで割り切れたらreturn

  else gcd(b, a % b);

}



const int MOD = 1000000007;

const int INF = 1 << 30;

int dp[100005],h[100005];



int main(){

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

  rep(i,n){

    cin >> h[i];

  }

  

  rep(i,100005) dp[i] = INF;

  

  dp[0] = 0;

  

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

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

      if(i+j < n){

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

      }

    }

  }

  

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

  

  return 0;

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

#include <cstring>

#include <set>

#include <stack>

#include <list>

#include <vector>

#include <map>

#include <math.h>

#include <algorithm>



typedef long long ll;

using namespace std;



int main(){

    ll N, K;

    cin >> N >> K;

    ll a[N+1];

    ll dp[N+1];



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

        cin >> a[i];

    }



    dp[0]=0;

    dp[1]=0;

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

        dp[i] = 0x3f3f3f3f;

    }



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

        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;

}

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

#include <cstring>



using namespace std;



const int maxn = 1e5+5;

int n,K;

int h[maxn],f[maxn];



int abs(int x,int y) {

	if (x - y < 0) return y - x;

	return x - y;

}



int min(int x,int y) {

	if (x < y) return x;

	return y;

}



int main() {

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

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

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

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

	}

	f[0] = f[1] = 0;

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

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

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

		}

	}

	printf ("%d\n",f[n]);

	return 0;

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

using namespace std;

typedef long long int ll;

const int INF = 1000000000;

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

#define rep(i,n) REP(i, 0, n)

int dp[1001][1001];

int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    string a, b;

    cin >> a >> b;

    int n = a.size(), m = b.size();

    memset(dp, 0, sizeof(dp));

    rep(i, m + 1) dp[0][i] = i;

    rep(i, n + 1) dp[i][0] = i;

    REP(i, 1, n + 1) REP(j, 1, m + 1){

        dp[i][j] = dp[i - 1][j - 1];

        if(a[i - 1] != b[j- 1]) dp[i][j] += 1;

        int temp = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);

        dp[i][j] = min(temp, dp[i][j]);

    }

    cout << dp[n][m] << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.071834653806733x):
class _in{struct my_iterator{int it;const bool rev;explicit constexpr my_iterator(int it_, bool rev=false):it(it_),rev(rev){}constexpr int operator*(){return it;}constexpr bool operator!=(my_iterator& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const my_iterator i,n;public:explicit constexpr _in(int n):i(0),n(n){}explicit constexpr _in(int i,int n):i(i,n<i),n(n){}constexpr const my_iterator& begin(){return i;}constexpr const my_iterator& end(){return n;}};



#include <bits/stdc++.h>

using namespace std;



int main() {

    string s1, s2;

    cin >> s1 >> s2;

    int N = s1.size(), M = s2.size();

    vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));

    for(int i : _in(N + 1)) dp[i][0] = i;

    for(int j : _in(M + 1)) dp[0][j] = j;

    for(int i : _in(N)) for(int j : _in(M)) {

        dp[i + 1][j + 1] = dp[i][j];

        if(s1[i] != s2[j]) ++dp[i + 1][j + 1];

        int temp = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1);

        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], temp);

    }

    cout << dp[N][M] << endl;

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

#include<algorithm>

#include<iostream>

using namespace std;

typedef long long ll;

struct ee{

	int fi,se;

}p[524290];

int n,a[524290],N,ans;

void f(int x,int y)

{

	if(x==p[y].fi||x==p[y].se) return;

	if(a[x]>a[p[y].fi]) p[y].se=p[y].fi,p[y].fi=x;

	else if(a[x]>a[p[y].se]) p[y].se=x;

}

int main()

{

	scanf("%d",&n);

	N=1<<n;

	for(int i=0;i<N;i++) scanf("%d",&a[i]),p[i].fi=i,p[i].se=N;

	for(int i=1,j,ls,now;i<N;i++)

	{

		ls=i;

		j=(i-1)&i;

		for(;ls;)

		{

				int k=i^(ls-j);

				f(p[k].fi,i);

				f(p[k].se,i);

				ls=j;

				j=(j-1)&j;

		}

	}

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

	{

		ans=max(ans,a[p[i].fi]+a[p[i].se]);

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

	}

}

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

#include <cctype>

#include <algorithm>

#define MAXN 1100005

using namespace std;

int a[MAXN];

int fst[MAXN]={0};

int sec[MAXN]={0};

int tmp[25];

int temp[5];

int f[MAXN];

int read(void);

void write(int x);

int main(void){

    int len,n;

    len=read();

    n=1<<len;

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

        fst[i]=read();

    for (int l=0;l<len;l++){

        int tpow=1<<l;

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

            if (i&tpow){

                //Use i^tpow;

                int tfore=i^tpow;

                temp[0]=fst[i],temp[1]=sec[i];

                temp[2]=fst[tfore],temp[3]=sec[tfore];

                sort(temp,temp+4);

                fst[i]=temp[3],sec[i]=temp[2];

            }

        }

    }

    f[0]=fst[0];

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

        f[i]=max(f[i-1],fst[i]+sec[i]);

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

        write(f[i]);

    return 0;

}



int read(void){

    int ans=0;

    char ch;

    do {

        ch=getchar();

    } while (!isdigit(ch));

    while (isdigit(ch)){

        ans=ans*10+(ch-'0');

        ch=getchar();

    }

    return ans;

}



void write(int x){

    int lg=0;

    if (!x){

        putchar('0');

        return;

    }

    while (x){

        tmp[lg++]=x%10;

        x/=10;

    }

    for (int i=lg-1;i>=0;i--)

        putchar(tmp[i]+'0');

    putchar('\n');

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

#include <cmath>



using namespace std;

int k,j,i,n,h[100005];

long long dp[100005];

int main()

{

    cin>>n>>k;

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

        cin>>h[i];

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

        dp[i]=1e17;

    dp[1]=0;

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

    {

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

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

    }

    cout<<dp[n];

    return 0;

}


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

using namespace std;

int h[100001];

int dp[100001];

int n,k;

int solve(int stone)

{

    if(stone==n) return 0;

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

    int cost=1<<30;

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

    cost=min( cost,abs(h[stone]-h[stone+i])+solve(stone+i) );



    return dp[stone]=cost;

}



int main()

{

    int i;

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



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

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



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

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





    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.3697032913688334x):
/**

     **author : _KMS_

     **created : 28-03-2020

**/

#include<bits/stdc++.h>

using namespace std;

const long long M =2e5+10,MOD=998244353;

typedef long long ll;

int arr[M];

int dp[M];

int main() {

   

  ios::sync_with_stdio(false);

  cin.tie(0);

  int n,k;

  cin>>n>>k;

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

  cin>>arr[i];

 // dp[2]=abs(arr[1]-arr[2])

for(int i=2;i<=n;++i)dp[i]=INT_MAX;

 arr[0]=arr[1];

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

  {  int temp=k;

     int in=i-1;

     while(temp>0&&in>0){

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

    temp--;

    in--;

  }

  }

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



  return 0;

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

using namespace std;

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

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

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

#define ceil(n, m) ((n / m) + ( n % m ? 1 : 0))

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

#define file_in freopen("input.txt","r",stdin);

#define file_out freopen("output.txt","w",stdout);

#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);



int main(){

    //fast_IO;



    string s;

    cin >> s;

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

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

            s[i]=' ';

            bool f = 1;

            for(int j=i-1;j>=0 && f;j--){

                    if(s[j]!=' ')

                        s[j] = ' ',f=0;

            }

        }

    }

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

        if(s[i]!=' ') cout<<s[i];

    return 0;

}


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

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

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

    {

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

        {

            A--;

            if(A<0)

                A=0;

        }

        else

            H[A++]=G[i];

    }

    H[A]='\0';

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

    return 0;

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

using namespace std;



typedef long long ll;



#define INF 1e18

#define MAX_N 100005



ll N, K, h[MAX_N], memo[MAX_N];



ll dp(int p) {

  if (p < 1) {

  	return INF;

  }

  if (p == 1) {

  	return 0;

  }

  if (p <= K + 1) {

  	return abs(h[p] - h[1]);

  }

  ll opt = INF;

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

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

  }

  return memo[p] = opt;

}



int main() {

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

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

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

  }

  // printf("%lli\n", dp(N));

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

  	memo[i] = abs(h[i] - h[1]);

  }

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

  	ll opt = INF;

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

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

	}

	memo[i] = opt;

  }

  printf("%lli\n", memo[N]);

}

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

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

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

//比如从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>



using namespace std;

#define PROBLEM "B"



bool check[1000010];



int odd(int n){



	return 3*n + 1;

}



int even(int n){



	return n/2;

}





int main(){



	#ifdef DBG

	freopen("problem" PROBLEM ".in", "r", stdin);

	freopen("problem" PROBLEM ".out", "w", stdout);

	#endif



	memset(check, 0, sizeof(check));

	int n;

	int count = 0;

	while(cin >> n){



		while(!check[n]){



			check[n] = true;

			if(n % 2 == 0)

				n = even(n);

			else

				n = odd(n);

			count++;

		}

		cout << count+1 << endl;

	}



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.951890268151849x):
////////////////////////// Author

////////////////////////// Nasim Hossain Rabbi

////////////////////////// JU-CSE28

////////////////////////// CF - imnasim3.1415

////////////////////////// UVA - imnasim3.1415

////////////////////////// Mail - imnasim3.1415@gmail.com

#include<bits/stdc++.h>

using namespace std;

#define E end()

#define B begin()

#define sz size()

#define EM empty()

#define clr clear()

#define sqr(a) (a*a)

#define mk make_pair

#define pb push_back

string en="\n";

string t="hello";

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

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

#define per(i,n) for(i=n-1;i>=0;i--)

#define peR(i,n) for(i=n;i>0;i--)

#define mem(a,b) memset(a,b,sizeof(a))

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

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

#define pi 3.1415926535897932384626433832795

#define vi vector<int>

#define vl vector<long long>

#define vs vector<string>

#define vp(vi,x) cin>>x; vi.pb(x);

#define srch(a,x) binary_search(all(a),x)

#define LB(a,x) (lower_bound(all(a),x)-a.B)

#define UB(a,x) (upper_bound(all(a),x)-a.B)

typedef long long LL;

typedef unsigned long long ULL;

bool isprm(LL a){for(LL i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}

bool palin(string a){for(int i=0;i<a.sz;i++){if(a.at(i)!=a.at(a.sz-i-1))return 0;} return 1;}

bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}

int toInt(string s){int sm;stringstream ss(s);ss>>sm;return sm;}

template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}

int main()

{

//    fast;

    LL i,j,k,n,m,l,s=0,x,y,tc=1,c=0;

    while(cin>>n)

    {

        map<int,int> mp;

        s=1;

        x=n;

        while(1)

        {

            if(mp.count(x)==1)

            {

                break;

            }

            if(x%2==0)

            {

                mp[x]++;

                x=x/2;

            }

            else

            {

                mp[x]++;

                x=3*x+1;

            }

        }

        cout<<mp.sz+1<<en;

    }

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

using namespace std;



typedef signed long long ll;

const int MX = 1e5 + 5;

const int INF = 1e9 + 5;

int n, k, A[MX], dp[MX];



int solve(int pos) {

    if(pos >= n) return INF;

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

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

    int ans = INF;

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

        if(pos + i < n) {

            ans = min(ans, abs(A[pos] - A[pos + i]) + solve(pos + i));

        }

    }



    return dp[pos] = ans;

}



int main() {

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



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

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

    }

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

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

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.368588914401854x):
#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 IOS          ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define endl         "\n"

#define int          long long

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

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

#define pii          pair<int,int>

#define fi           first

#define se           second

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

#define deb(x)       cout << #x << ' ' << x << endl

#define clock        cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;



void INPUT() {

	#ifndef ONLINE_JUDGE

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

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

    #endif

}



const int N = 1e5 + 5;



int a[N], n, k;

int cache[N];

 

int dp(int idx) {

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

	int ans = (idx == n - 1 ? 0 : LLONG_MAX);

	forn(i, 1, k + 1) {

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

	}

	cache[idx] = ans;

	return cache[idx];

}



int32_t main(){

    IOS //INPUT();

    cin >> n >> k;

    forn(i, 0, n) cin >> a[i];

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

    int ans = dp(0);

    cout << ans << endl;

    return 0;

}

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



#define int         long long

#define pb          push_back

#define pii         pair<int,int>

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

#define x           first

#define y           second

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

#define endl        '\n'

#define hell        1000000007

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

using namespace std;

int dp[100005];

void solve(){

	int n,k;

	cin>>n>>k;

	int a[n];

	rep(i,0,n) cin>>a[i];

	rep(i,0,n) dp[i]=hell;

	dp[0]=0;

	rep(i,1,n) {

		rep(j,1,k+1) {

			dp[i]=min(dp[i],(i-j>=0)?dp[i-j]+abs(a[i-j]-a[i]):hell);

		}

	}

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

}



signed main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	int t=1;

    // cin>>t;

	while(t--){

		solve();

	}

	return 0;

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

using namespace std;

typedef long long int ll;

#define faster ios_base::sync_with_stdio(0);

#define faster_tie cin.tie(0);

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

#define NL cout<<'\n'



ll n,k,x;

vector<ll>height;

ll sol[110000];



ll solve(ll x)

{

    if(x==1)

    {

        return 0;

    }

    if(sol[x]!=-1)return sol[x];

    ll a=INT_MAX;



    for(ll i=x-1;i>x-1-k;i--)

    {

        if(i==0)break;

        a=min(a,solve(i)+abs(height[i]-height[x]));

    }

    sol[x]=a;

    return a;



}





int main()

{

  

  	memset(sol,-1,sizeof sol);

    cin>>n>>k;

    height.push_back(-1);

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

    {

        cin>>x;

        height.push_back(x);

    }



    ll ans=solve(n);

    cout<<ans;







    return 0;

}


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

using namespace std;



typedef long long ll;



const int MOD = 1e9 + 7;

const int INF = 0x3F3F3F3F;

const ll INFLL = 0x3F3F3F3F3F3F3F3FLL;



int n, k;

int h[112345];

int ans[112345];



int main(){

	

	ios_base :: sync_with_stdio(false);

	cin.tie(0);

	

	cin >> n >> k;

	

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

		cin >> h[i];

	

	memset(ans, INF, sizeof ans);

	ans[1] = 0;

	

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

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

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

	

	cout << ans[n] << "\n";

	

	return 0;

}