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

using namespace std;

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

#define scn(n) scanf("%d",&n)

#define lscn(n) scanf("%lld",&n)

typedef long long ll;

#define pri(n) printf("%d\n",n)

#define lpri(n) printf("%lld\n",n);

#define rep(i,st,ed) for(int i=st;i<ed;i++)

#define F first

#define S second 

#define pb(n) push_back(n)

const int N=1e5+6;

const ll M=1e9+7;

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

void  rec(ll i)

{

	// printf("i= %d\n",i);

	if(dp[i]!=-1)

	{

		// printf("reached here1\n");

		return ;

	}

	

	if(i>=n-1)

	{

		dp[i]=0;

		// printf("reached here2 %d %d\n",i,n-1);

		return ;

	}

	ll ans=1e18,pos=-1;

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

	{

		rec(j);

		ll val=abs(a[i]-a[j])+dp[j];

		if(val<ans)

		{

			ans=val;

			pos=j;

		}

	}

	// printf("for i= %d and n= %lld ans 1 = %lld ans2 = %lld\n",i,n,ans1,ans2);	

	// if(i==0)

	// 	printf("pos = %lld\n",pos);

	dp[i]=ans;

}

int main()

{

	lscn(n);lscn(k);

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

		lscn(a[i]);

	

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

	

	rec(0ll);

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

	// 	printf("%lld ",dp[i]);

	lpri(dp[0]);

}

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

using namespace std;

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

#define scn(n) scanf("%d",&n)

#define lscn(n) scanf("%lld",&n)

typedef long long ll;

#define pri(n) printf("%d\n",n)

#define lpri(n) printf("%lld\n",n);

#define rep(i,st,ed) for(int i=st;i<ed;i++)

#define var(n) int n; scn(n)

#define F first

#define S second 

#define pb push_back

const int N=1e6+5;

// const ll M = 998244353;

const ll M=1e9+7;

const ll inf=1e18;







int main()

{

	var(n);var(k);

	int a[n],dp[n];

	rep(i,0,n)

	{		

		scn(a[i]);

		dp[i]=M;

	}

	dp[0]=0;

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

	{

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

		{

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

		}

	}

	pri(dp[n-1]);

}

// 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 endl "\n"

#define ll long long

#define ld long double

#define ull unsigned long long

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

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

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

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





const ll maxLONG = 9223372036854775807;

const int maxINT = 2147483647;

const ll mod = 1000000007;

const ld EPS = 1e-9;

const int N = 1e5 + 9;

const ll INF = LONG_LONG_MAX;



ll GetMod(ll n, ll z) { // func to get mod for positive num and negitive nums

    return (n % z + z) % z;

}



//          U   R  D  L

int dx[] = {-1, 0, 1, 0, -1, +1, +1, -1}; // dx for the 8 adjacent places in matrix

int dy[] = {0, 1, 0, -1, +1, +1, -1, -1};// dy for the 8 adjacent places in matrix

int n, k;

vector<int> v;

int dp[N] = {};



int check(int pos) {



    if (pos == v.size() - 1)return 0;

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

    int u = 1e9;

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

        if (pos + i < v.size())

            u = min(u, abs(v[pos] - v[pos + i]) + check(pos + i));

        else

            break;

    }



    dp[pos] = u;

    return dp[pos];

}



int main() {



    ios_base::sync_with_stdio(false), cin.tie(NULL);

    cin >> n;

    cin >> k;

    v.resize(n);

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

    fill(dp, dp + N, -1);

    cout << check(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):
#include <bits/stdc++.h>

using namespace std ;

#define ll long long

inline int D(){

    int t ;

    scanf( "%d" , &t ) ;

    return t ;

}

inline ll LLD(){

    ll t ;

    scanf( "%lld" , &t ) ;

    return t ;

}

//################################################################

ll n;

ll a[1000005];

ll mem[1000005];

int k;

int main() {

    n = LLD(),k=D();

    for (int i = 0; i < n; i++)a[i] = LLD();

    mem[0]=0;

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

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

        ll mn=LONG_LONG_MAX;



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

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

        }

        mem[i]=mn;

    }

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



}

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





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

using namespace std;

long long int n,k,minimum[100010];

vector<long long int>v;

long long int dp(long long index)

{

    //---------------------------------------------------

    /*long long int temp1,temp2=INT_MAX;

    if(index==n-1)

        return 0;

    if(minimum[index+1]==-1)

    {

        minimum[index+1]=dp(index+1);

    }

    temp1=abs(v[index]-v[index+1])+minimum[index+1];

    if(minimum[index+2]==-1 && index<n-2)

    {

        minimum[index+2]=dp(index+2);

    }

    if(index<n-2)

        temp2=abs(v[index]-v[index+2])+minimum[index+2];

    return min(temp1,temp2);*/

    //----------------------------------------------------

    long long int temp=INT_MAX,i,flag=0;

    if(minimum[index]!=-1)

        return  minimum[index];

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

    {

        flag=1;

        if(minimum[index+i]!=-1)

        {

            temp=min(temp,abs(v[index]-v[index+i])+minimum[index+i]);

            continue;

        }

        minimum[index+i]=dp(index+i);

        temp=min(temp,abs(v[index]-v[index+i])+minimum[index+i]);

    }

    if(!flag)

        temp=0;

    minimum[index]=temp;

    return minimum[index];

}

int main()

{

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

    long long int i,j,temp;

    cin>>n>>k;

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

    {

        cin>>temp;

        v.push_back(temp);

    }

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

    cout<<dp(0);

}


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

using namespace std;



int main()

{

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

    long long int n,k;

    cin>>n>>k;

    long long int i,j,a[n],dp[n];

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

        cin>>a[i];

    dp[0]=0;

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

    {

        dp[i]=INT_MAX;

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

        {

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

        }

    }

    cout<<dp[n-1];

}


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

// Welcome to my source code!



static const int MAX = 1000010;



int main() {

    int s;

    cin >> s;

    int a[MAX] = {}; a[0] = s;

    bool flag = false;

    int ans;

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

        if (s % 2 == 0) s /= 2;

        else s = 3 * s + 1;

        a[i] = s;

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

            if (a[j] == a[i]) {

                ans = i + 1;

                flag = true;

                break;

            }

        }

        if (flag) break;

    }

    cout << ans << endl;

}

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

using namespace std;

typedef long long ll;

// Welcome to my source code!



int main() {

    int s;

    cin >> s;

    int ans = 1;

    while (s > 4 || s == 3) {

        s = (s % 2 == 0 ? s / 2 : s * 3 + 1 );

        ans++;

    }

    cout << ans + 3 << endl;

}

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

#include <algorithm>

#include <vector>

#include <set>

#include <queue>

#include <algorithm>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

 

#define pb push_back

#define mk make_pair

#define f0(i,b) for(int i=0;i<(b);i++)

#define f1(i,b) for(int i=1;i<=(b);i++)

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

#define sf(a) scanf("%d",&a);

#define sfl(a) scanf("%lld",&a);

#define pf(a) printf("%d\n",a);

#define pfl(a) printf("%lld\n",a);

#define TEST int test; cin >> test;while(test--)

#define ii pair<int,int>

#define iil pair<ll,ll>

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

#define INF 2147483647

 

 

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

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

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

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

const double pi = acos(-1.0);

 

 

ll n, m, x, k;

 

void solve()

{

   string s;

   cin >>k >>  s;

   ll n = s.size();

   if(n <= k) cout << s << endl;

   else{ cout << s.substr(0,k);

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

   cout <<'.';

   cout << endl;

   }

}

 

 

int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(NULL);

    cout.tie(NULL);

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

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

    solve();

    return 0;

}

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

#include<string.h>

int main(){

	int k;

	char s[105];

	scanf("%d",&k);

	getchar();

	scanf("%s",&s);

	if(strlen(s)>k){

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

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

		}

		printf("...");

		printf("\n");

	}

	else{

		int l = strlen(s);

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

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

		printf("\n");

	}

}

// 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, a[100100], dp[100100];



int f(int n) {

	if (n==1) return 0;

	if (n==2) return abs(a[2] - a[1]);

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

	dp[n] = 2e9;

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

		if (n-i>0)

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

				

	return dp[n];

}



int main() {

	cin >> N >> K;

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

		cin >> a[i];

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

	cout << f(N) << endl;

	return 0;

}

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

using namespace std;

#define int long long int



int n; //number of stones

int k; //farthest the frog can jump

vector<int> h; //height of the stones

vector<int> dp; //memorize heck



main(){

	cin >> n >> k;



	dp.assign(n, INT_MAX);

	

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

		int x;

		cin >> x;

		h.push_back(x);

	}



	dp[0] = 0;

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

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

			if(i-j>=0){

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

			}

		}

	}



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

	

}

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

#include<cmath>

using namespace std;

int main(){

	long long n,m,a,b;

	long long sum;

	a=pow(10,9)+7;

	while(scanf("%lld",&n)==1)

	{

		sum=1;

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

		{

			sum=sum*i;

		    sum=sum%a;

		}

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

	}

	return 0;

}

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

#include<string.h>

#define maxn 1000000007

int main()

{

    long long a,n,sum;

    while(scanf("%lld",&n)!=EOF)

    {

        sum=1;

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

        {

            sum*=a;

            sum%=maxn;

        }

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

    }

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



using namespace std;



int main() {

    int a, b;

    cin >> a >> b;

    if(a+b < 10){

        cout << a + b << endl;

    }

    else{

        cout << "error" << endl;

    }

    return 0;

}

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

int main(){

	int a,b;

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

	if(a+b<10){printf("%d\n",a+b);}

	else {printf("error\n");}

	return 0;

	}

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

using namespace std;

#define ll long long int

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

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

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

#define RREP(i,s,t) for( int i = s; i >= t; i-- )

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF 2000000000

#define mod 1000000007

#define INF2 1000000000000000000





int main(void)

{

    cin.tie(0);

    ios::sync_with_stdio(false);

    int A, B;

    cin >> A >> B;

    if (A + B >= 10) {

        cout << "error" << endl;

    } else {

        cout << A + B << endl;

    }



    return 0;

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

}

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

}








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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

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

using namespace std;

using ll = long long;

using P = pair<int,int>;



int INF = 1001001001;



template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}





int main() {

    int n, k;

    cin >> n >> k;

    vector<int> h(n);

    rep(i,n) cin >> h[i];

    vector<int> dp(110000, INF);

    dp[0] = 0;

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

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

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

        }

    }

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

    return 0;

}

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

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

using namespace std;

using ll = long long;

using P = pair<int,int>;



int INF = 1001001001;



template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}



int main() {

    int n, k;

    cin >> n >> k;

    vector<int> h(n);

    rep(i,n) cin >> h[i];

    vector<int> dp(n+k, INF);

    dp[0] = 0;

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

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

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

        }

    }

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

    return 0;

}

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}