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

#include<cstring>

using namespace std;

int main()

{

	int k;

	string s;

	cin>>k>>s;

	if(s.length()>k)

	{

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

		cout<<s[i];

		cout<<"...";

	}

	else

	cout<<s;

} 

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

#include <string.h>

int main(){

	int k;

	scanf("%d",&k);

	char s[100];

	scanf("%s",&s);

	int lol = strlen(s);

	s[k] = '\0';

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

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

	}

	if(k<lol){

		printf("...");

	}

return 0;

}

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

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const double PI = acos(-1);



int main()

{

	int k;

	string s;

	cin >> k >> s;

	if (s.size() <= k) cout << s << endl;

	else cout << s.substr(0, k) + "..." << endl;

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

using namespace std;

#define ll long long

#define vi vector<int>

#define pb push_back

#define pii pair<int , int>

#define fast ios_base::sync_with_stdio(0)

#define mx 100005

#define inf 100000000000000000LL

ll arr[mx], dp[mx];

int n, k;

ll solve(int pos)

{

    if(pos==n) return 0;

    ll&ret = dp[pos];

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

    ret = inf;

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

        int nxt = pos+i;

        if(nxt>n) break;

        ret = min(ret, llabs(arr[pos] - arr[nxt]) + solve(nxt));

    }

    return ret;

}

int main()

{

    //fast;

    while(scanf("%d%d",&n,&k)){

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

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

        memset(dp, -1, sizeof dp);

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

        return 0;

    }

}




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

using namespace std;

#define ll long long

#define vi vector<int>

#define pb push_back

#define pii pair<int , int>

#define fast ios_base::sync_with_stdio(0)

#define mx 100005

#define inf 100000000000000000LL

ll arr[mx], dp[mx];

int main()

{

    int n,k;

    while(scanf("%d%d",&n, &k)){

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

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

        dp[n] = 0;

        for(int i = n-1; i>=1; i--){

            dp[i] = inf;

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

                int nxt = i+j;

                if(nxt>n) break;

                dp[i] = min(dp[i], llabs(arr[i] - arr[nxt]) + dp[nxt]);

            }

        }

        printf("%lld\n", dp[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;

int main(){

	int k;

	string s;

	cin >> k >> s;

	if(s.length() > k){

		for(int i = 0;i < k;i++) cout << s[i];

		cout << "...";

	}else {

		cout << s;

	}

	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 main() {

	// your code goes here

	cin.tie(NULL);

	cout.tie(NULL);

	

	long long n,k;

	

	while(cin>>n && cin>>k)

	{

		vector<long long>cost(n);

		

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

		

		vector<long long>dp(n,INT_MAX);

		

		dp[0] = 0;

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

		

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

		{

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

			{

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

			}

		}

		

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

	}

	return 0;

}

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



using namespace std;



const int MAXN = 1000000;

int A[MAXN + 1];



int f(int n) {

	if (n % 2 == 0) {

		return n / 2;

	} else {

		return 3 * n + 1;

	}

}



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  int s, count = 1;

  cin >> s;



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

  	A[i] = 0;

  }



  A[s] += 1;

  while (true) {

  	s = f(s);

  	count++;

  	if (A[s] != 0) {

  		cout << count << '\n';

  		break;

  	}

  	A[s]++;

  }

  return 0;

}


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



using namespace std;



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  int s;

  cin >> s;



  set<int> c;

  while (c.count(s) == 0) {

    c.insert(s);

    if (s % 2 == 0)

      s /= 2;

    else

      s = 3 * s + 1;

  }

  cout << c.size() + 1 << '\n';

  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;

typedef long long ll;

const ll inf = 1e9;

#define sz              300005

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



string s;



int main(){

    Fast;

    int a,b,c, i,j, n,m,t, cs=1;

    cin>>n>>s;



    if(s.size()<=n){

        cout<<s<<endl;

        return 0;

    }

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

        cout<<s[i];

    }

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





    return 0;

}




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

	# include <string.h>

	int main () {

		int a = 0;

		int c =0;

	char name [100];

		scanf ("%d",&a);

		scanf("%s%[^\n]",&name);

		c = strlen (name);

		if (c<=a) {

			printf ("%s",name);

		} else if (c>a) {

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

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

			}

			printf("...");

		}

		return 0;

	}

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

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const double PI = acos(-1);



int main()

{

	int k;

	string s;

	cin >> k >> s;

	if (s.size() <= k) cout << s << endl;

	else cout << s.substr(0, k) + "..." << endl;

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

  using namespace std;



typedef long long int int64;

typedef unsigned long long int uint64;

typedef vector<int64> vi;

typedef vector<bool> vb;

typedef vector<char> vc;

typedef pair<int64,int64> pi; 

bool doubleSame(double a, double b)

{ const double eps = 1e-15; return ( abs(a-b)<= eps*abs(a) ); }

 

int main()

{

	int64 n,k;

	cin>>n>>k;

 

	vector<int64> h(n);

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

		cin>>h[i];

 

	vector<int64> ans(n);

	ans[0] = 0;

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

 

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

	{

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

		for(int64 j = 2 ; j<=i && j<=k ; j++)

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

	}

	cout<<ans[n-1];

}

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

using namespace std;



long long n,a,m,b,qq,k;

string ss;

int main() {

    cin>>n>>k;

    long long l[n],l1[n];

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

        l[i]=0;

        l1[i]=0;

    }

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

        cin>>l[i];

    }

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

        l1[i]=l1[i-1]+abs(l[i-1]-l[i]);

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

            l1[i]=min(l1[i], l1[i-j]+abs(l[i-j]-l[i]));

        }

    }

    cout<<max(0*1ll,l1[n-1])<<endl;

}


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

#include <cmath>



using namespace std;



int main(){

  long long int n;

  cin >> n;

  long long int ans = 1;

  

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

    ans = (ans * i) % (int)(pow(10,9) + 7);

  }

  

  cout << ans << endl;

  return 0;

}

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

#include <math.h>



int main(void){

  int n;

  scanf("%d", &n);

  long int ans = 1;

  

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

    ans = (ans * i) % (int)(pow(10, 9) + 7);

  }

  

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

  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 k,x;

    cin >>  k >> x;

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

        cout << i<<" ";

    }

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

        cout << i<<" ";

    }

    cout <<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

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



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

        printf("%d ",i);

    }

    return 0;

}

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

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

using namespace std;



int main(void) {

	int k, x;

	cin >> k >> x;

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

		if (i == x+k) cout << i << endl;

		else cout << i << " ";

	}

	return 0;

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

using namespace std;

typedef long long ll;

typedef pair<int,int> pii;

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

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

#define read(x) scanf("%d",&x)

#define read2(x,y) scanf("%d%d",&x,&y)

#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)

#define readll(x) scanf("%lld",&x)

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

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

#define pb push_back

#define lowbit(x) x&-x

#define lc rt<<1

#define rc rt<<1|1

#define ls lc,l,m

#define rs rc,m+1,r

#define dbg(x) cout<<' '<<#x<<" = "<<x<<endl;

#define dbg2(x,y) cout<<' '<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<endl;

#define dbg3(x,y,z) cout<<' '<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<", "<<#z<<" = "<<z<<endl;

const int INF =0x3f3f3f3f;

const int mod = 1e9+7;

const double eps=1e-8;

const int N = 2e5+5;

int dir[4][2]={0,1,0,-1,1,0,-1,0};

int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

ll dp[N];

int h[N];

int main(){

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

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

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

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

	int n,k;

	read2(n,k);

	fe(i,1,n)read(h[i]);

	mst(dp,0x3f);

    dp[1] = 0;

    fe(i,1,n){

		fe(j,1,k){

			if(i-j<1)break;

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

		}

    }

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









	return 0;

}


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

using namespace std;

typedef long long int ll;

const int MAX=1e5+10;

int N,k,A[MAX],dp[MAX];

int solve(int idx)

{

    if(idx==N)return 0;

    if(idx>N)return 1e9;

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

    int res=1e9;

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

        res = min(res,solve(idx+i)+abs(A[idx+i]-A[idx]));

    }

    return dp[idx]=res;

}

int  main ()

{

    memset(dp,-1,sizeof dp);

    cin >> N >> k;

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

        cin >> A[i];

    }

    cout<<solve(1)<<endl;

    return 0;

}




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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

using namespace std;

const int N = 1e5 + 1;

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

int calc(int i){

    if(i == n - 1)

        return 0;

    if(i >= n)

        return 1e9 + 10;

    int &ret = dp[i];

    if(ret != -1)return ret;

    ret = 1e9 + 10;

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

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

    return ret;

}

int main(){

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

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

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

    }

    memset(dp , -1 , sizeof dp);

    cout<<calc(0);

    return 0;

}

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

using namespace std;

const int N = 1e5 + 1;

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

int main(){

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

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

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

    }

    dp[0] = 0;

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

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

        dp[i] = 1e9 + 10;

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

            if(i >= j)

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

    }

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

long long n;

long long k;

vector<long long> vec;



long long meme[100007];



long long solve(long long index){



    if(index==n-1){

        return 0;

    }

    

    if(meme[index]!=-1){

        return meme[index];

    }

    

    long long ret = 10000000000000000;

    long long sol = 10000000000000000;

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

        if(index+i<n){

            ret = abs (vec[index]- vec[index+i]) + solve(index+i); 

        }

        sol = min(sol,ret);

    }



    return meme[index] = sol;

}



int main(){

    cin>>n;

    cin>>k;

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

    long long num;

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

        cin>>num;

        vec.push_back(num);

    }

    cout<<solve(0)<<endl;

    return 0;

}

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

using namespace std;

#define fix(f,n) std::fixed<<std::setprecision(n)<<f

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

typedef long long int ll;

typedef unsigned long long int ull;

#define vi vector<int>

#define pii pair<int,int>

#define vii vector<pii>

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

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

#define max3(a,b,c) ((a>b)?(a>c)?a:c:(b>c)?b:c)

#define min3(a,b,c) ((a<b)?(a<c)?a:c:(b<c)?b:c)

#define REP(i,a,n) for(ll i=a;i<n;i++)

#define pb push_back

#define mp make_pair



int main(){

	fast;

	int test=1;

	//cin >> test;

	while(test--){

	    int n,k;

	    cin >> n >> k;

	    ll dp[n+1]={};

	    ll arr[n+1];

	    dp[1]=0;

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

	        cin >> arr[i];

	    }

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

	        ll temp=1000000000000000000;

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

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

	        }

	        dp[i]=temp;

	    }

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

	        ll temp=1000000000000000000;

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

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

	        }

	        dp[i]=temp;

	    }

	    cout << dp[n] << 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;





}