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

}

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

}


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

}


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





}
// Original C++ solution (slower):
#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];



}

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

			}

		}

	}

	

}

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

} 

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



int main()

{

    int s;

    std::cin >> s;

    int list[1000000];

    int index = 0;

    int a_i;

    int a_pre = s;

    list[0] = s;

    index = index + 1;

    bool search_flag = false;

    while( true){

        if ( a_pre % 2 == 0){

            a_i = a_pre / 2;

        } else {

            a_i = 3 * a_pre + 1;

        }

        list[index] = a_i;

        index = index + 1;

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

            if ( list[i] == a_i) {

                search_flag = true;

                break;

            }

        }

        if (search_flag==true){

            break;

        } 

        a_pre = a_i;



    }

    std::cout << index << std::endl;

    return 0;

}
// Original C++ solution (slower):
#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;

}

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

}


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

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

using namespace std;

using ll = long long;

using P = pair<ll, ll>;

const string ln = "\n";

constexpr int INF = 1001001001;

constexpr int MOD = 1000000007;



int main() {

  cin.tie(nullptr);

  ios::sync_with_stdio(false);



  int n;

  cin >> n;



  ll ans = 1;



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

    ans *= i % MOD;

    ans %= MOD;

  }



  cout << ans << ln;



  return 0;

}
// Original C++ solution (slower):
#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;

}

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

}

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

#include <set>

#include <cstring>

#include <vector>

using namespace std;



//int node[101][101];

int visit[101];

int ans,last;

int die[101];

int flag;



void dfs(int now, int cnt, int *st, vector<vector<int> >& vv){

	if( cnt>ans ){

		ans=cnt;

		last = now;

		flag = 1;

	}

	for(int next=0; next<vv[now].size(); next++)if( !visit[vv[now][next]] ){

		int k = vv[now][next];

		visit[k] = die[k] = 1;

		st[cnt] = k;

		dfs(k,cnt+1,st,vv);

		visit[k] = 0;

	}

	return ;

}



int main(){

	int n;

	while(cin>>n,n){

		set<int> s;

		vector<vector<int> > vv(101);

		ans = 0;

		//memset(node,0,sizeof(node));

		memset(visit,0,sizeof(visit));

		memset(die,0,sizeof(die));

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

			int a,b; cin>>a>>b;

			//node[a][b] = node[b][a] = 1;

			vv[a].push_back(b);

			vv[b].push_back(a);

			s.insert(a);

			s.insert(b);

		}

		int tmp[101];

		/*

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

			printf("vv[%d].size = %d\n",i,vv[i].size());

			for(int j=0; j<vv[i].size(); j++)

				printf("%d ",vv[i][j]);

			puts("");

		}

		*/

		for(set<int>::iterator it=s.begin(); it!=s.end(); it++)if( !die[*it] ){

			flag = 0;

			

			visit[*it]=1;

			tmp[0] = *it;

			dfs(*it,1,tmp,vv);

			visit[*it]=0;

			

			die[last] = 0;

			//printf("*it:%d\n",*it);



		}

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

	}

	return 0;

}
// Original C++ solution (slower):
#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");

}




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

}

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

#include<string>

#include<vector>

#include<algorithm>

#include<cmath>

#include<set>

#include<map>

#include<iomanip>



#define INF 1<<29



using namespace std;



int main(void) {

	string s, t;

	cin >> s >> t;

	bool ans = false;

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

		if (s == t) {

			ans = true;

			break;

		}

		s = s.back() + s.substr(0, s.size() - 1);

	}

	if (ans) {

		cout << "Yes";

	}

	else {

		cout << "No";

	}

	return 0;

}
// Original C++ solution (slower):
#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;

}

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

}


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

}

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

} 

// 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 dp[100005];



int main(){

    int n,k,s;

    cin>>n>>k;



    int arr[n];



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

        cin>>arr[i];





    dp[0]=0;

    if(n==1)

        cout<<0<<endl;



    else{



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



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

           s=INT_MAX;

           if(k>i)

           {

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

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



                  if(dp[j]+abs(arr[i]-arr[j])<s)

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

               }

           }



           else{



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



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



                  if(dp[j]+abs(arr[i]-arr[j])<s)

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





               }



           }





        }





    }



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

    return 0;







}


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

#include <algorithm>

#include <vector>

using namespace std;

 

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

int main(){

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

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

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

	}

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

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

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

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

		}

	}

	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;





}
// Original C++ solution (slower):
/**

 *   Author: praveen_ojha

 *   Created: 17 January 2019

 *   Time: 11:50:41

**/

#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007

#define PI 3.141592653589793238510

typedef long long ll;

typedef long double ld;

typedef pair<int,int> pii;

typedef vector<int> vi;

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

int solve(int ind){

	if(ind==n-1){

		return 0;

	}

	if(dp[ind]!=-1)

		return dp[ind];

	int mini=INT_MAX;

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

		if(ind+i<n){

		dp[ind+i]=solve(ind+i);

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

		}

	}

	dp[ind]=mini;

	return dp[ind];



}

int main(){

    ios_base::sync_with_stdio(false); 

    cin.tie(NULL);

    memset(dp,-1,sizeof dp);

    cin>>n>>k;

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

    	cin>>a[i];

    cout<<solve(0);

return 0;

}

// Optimized solution to the same problem (faster):
/**

 *   Author: Praveen Ojha

 *   Created: 27 January 2019

 *   Time: 00:59:53

**/

#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007

#define PI 3.141592653589793238510

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

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

typedef long long ll;

typedef long double ld;

typedef pair<int,int> pii;



int main(){

    ios_base::sync_with_stdio(false); 

    cin.tie(NULL);

    int n,k;

    cin>>n>>k;

    int a[n],dp[n];

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

    	cin>>a[i];

    }

    memset(dp,0,sizeof dp);

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

    	int mini=INT_MAX;

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

    		if(i+j<n){

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

    		}

    	}

    	dp[i]=mini;

    }

    cout<<dp[0]<<"\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;





}