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

#include <string>

using namespace std;

int main(){

string a,b;

cin>>a>>b;

a=a+a;

 string::size_type idx;

 idx=a.find(b);

if(idx==string::npos){

	cout<<"No"<<endl;

}else

	cout<<"Yes"<<endl;

return 0;

}

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

#include<cstring>



using namespace std;



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

	int i = left, j = right;

	char temp;

	while (i < j) {

		temp = a[i];

		a[i] = a[j];

		a[j] = temp;

		i++;

		j--;

	}

}



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

	exchange(a, 0, i - 1);

	exchange(a, i, n - 1);

	exchange(a, 0, n - 1);

	return;

}



int main() {

	char str[110], str1[110];

	int n, round = 0;

	scanf("%s", str);;

	scanf("%s", str1);

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

		printf("No");

		return 0;

	}

	n = strlen(str);

	while (round <= n) {

		reserve(str, n, 1);

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

			printf("Yes");

			return 0;

		}

		round++;

	}

	printf("No");

	return 0;

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

#include <cstdio>

#include <cstring>

using namespace std;

  

typedef long long llt;

  

const int MaxN = 16;

  

int N, fullSub;

int Log[1 << MaxN];

llt A[MaxN][MaxN];

llt dp[1 << MaxN];

  

void init() {

  scanf("%d", &N); fullSub = (1 << N) - 1;

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

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

  

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

    Log[i] = Log[i >> 1] + 1;

}

  

inline llt Calc_sum(int S) {

  int st[MaxN], tp = 0;

  llt res = 0;

  

  while (S > 0) {

    st[tp++] = Log[S & -S];

    S -= S & -S;

  }

  

  for (int i = 0; i < tp - 1; ++i) for (int j = i + 1; j < tp; ++j)

    res += A[st[i]][st[j]];

  return res;

}

  

void solve() {

  memset(dp, 0x80, sizeof dp); dp[0] = 0;

  for (int S = 0; S < (1 << N); ++S) {

    int Q = fullSub - S;

    for(int T = Q; T &= Q; --T)

      dp[S | T] = max(dp[S | T], dp[S] + Calc_sum(T));

  }

  

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

}

  

int main() {

  init();

  solve();

  return 0;

}

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



const int MaxN = 16;



int N;

int A[MaxN][MaxN];

long long Weight[1 << MaxN];

long long F[1 << MaxN];

bool Vis[1 << MaxN];



template <typename Typ>

inline Typ max(Typ x, Typ y) { return x > y ? x : y; }



void init() {

  scanf("%d", &N);

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

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

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

}



inline long long count(long long s) {

  long long ans = 0;

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

    if (s & (1 << i)) {

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

        if (s & (1 << j))

          ans += A[i][j];

    }

  return ans;

}



long long dfs(int s) {

  if (s == 0) return F[s] = 0;

  if (Vis[s] == true) return F[s];

  Vis[s] = true;

  for (int q = s; q &= s; --q) {

    int t = s &~ q;

    F[s] = max(F[s], dfs(t) + Weight[q]);

  }

  return F[s];

}



void solve() {

  for (int i = 0; i < (1 << N); ++i) Weight[i] = count(i);

  printf("%lld\n", dfs((1 << N) - 1));

}



int main() {

  init();

  solve();

  return 0;

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

#include<cstring>

#include<string>

using namespace std;

int main(){

	int k;

	cin>>k;

	string str;

	cin>>str;

	int t=str.length();

	if(k>t||k==t){

		cout<<str<<endl;

	}

	else if(k<t){

		string str1=str.substr(0,k);

		cout<<str1<<"...";

	}

	return 0;

}

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



int main(){

	int k;

	char word[100];

	scanf("%d", &k);

	scanf("%s", &word);

	

	int len = 0;

	

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

		len++;

	}

	

	if(len <= k){

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

	}

	else{

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

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

		}

		printf("...\n");

	}

	return 0;

}
// Slower C++ solution:


#include <bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323

#define ll long long int

#define vi vector <int>

#define vl vector <ll>

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

#define pb push_back

 

ll power(ll a, ll b){//a^b

    ll res=1;

    while(b>0){

        if(b&1){res=(res*a);b--;}

        a=(a*a);

        b>>=1;

    }

    return res;

}

vl dp(100005,-1);

vl h(100005);

ll k;

ll jump(ll i){

	if(dp[i]!=-1)

		return dp[i];

	if(i==1){

		dp[1]=0;

		return dp[i];

	}

	ll mi=jump(i-1)+abs(h[i]-h[i-1]);

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

		if(i-j<1)

			break;

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

	}

	dp[i]=mi;

	return dp[i];

} 

 

 

int main(){

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	ll n;

	cin>>n;

	

	cin>>k;

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

		cin>>h[i];

	cout<<jump(n)<<"\n";

}

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

using namespace std;

#define PI 3.14159265358979323

#define ll long long int

#define vi vector <int>

#define vl vector <ll>

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

#define pb push_back

#define watch(x) cout << (#x) << " is " << (x) << "\n"

#define MOD 1000000007

ll power(ll a, ll b) { //a^b

	ll res = 1;

	while (b > 0) {

		if (b & 1) {res = (res * a); b--;}

		a = (a * a);

		b >>= 1;

	}

	return res;

}

ll n;

ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}







int main() {

	

	ll n,k;

	cin>>n>>k;

	vector <ll> a(n);

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

		cin>>a[i];

	vector <ll> dp(n);

	dp[0]=0;

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

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

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

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

	}

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

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

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <sstream>

#include <queue>

#include <deque>

#include <bitset>

#include <iterator>

#include <list>

#include <stack>

#include <map>

#include <set>

#include <functional>

#include <numeric>

#include <utility>



using namespace std;

int abs(int a) {

	if (a < 0) {

		return -a;

	} else {

		return a;

	}

}

int main() {

	ios::sync_with_stdio(false);

	cin.tie(0);

	int n, k;

	cin >> n >> k;

	if (n < k) {

		int h1, h2;

		cin >> h1;

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

			cin >> h2;

		}

		cout << abs(h2 - h1);

		return 0;

	}

	vector<int> heights;

	vector<int> costs = {0};

	int h1;

	cin >> h1;

	heights.push_back(h1);

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

		int h2;

		cin >> h2;

		heights.push_back(h2);

		costs.push_back(abs(h2 - h1));

	}

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

		int h2;

		cin >> h2;

		int best_cost = costs.back() + abs(heights.back() - h2);

		heights.push_back(h2);

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

			int cost = costs[costs.size() - j];

			int h1 = heights[costs.size() - j];

			best_cost = min(best_cost, cost + abs(h2 - h1));

		}

		costs.push_back(best_cost);

	}

	cout << costs.back();

	return 0;

}

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

#include<cmath>

#include<algorithm>

using namespace std;

const int maxn=1e5 + 5;

int main()

{

	int dp[maxn];

	int h[maxn];

	int n,k;

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

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

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

		dp[1] = 0;

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

	    	dp[i]=1e9;

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

				if(i==j) break;

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

				}

	}

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

	}

	return 0;

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

using namespace std;

#define ll long long

ll h[100009];

ll dp[100009];

int main() {

    ios::sync_with_stdio(false);

    int n,k;

    cin>>n>>k;

    for(int i=1;i<=n;i++)cin>>h[i];

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

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

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

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

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

    }

    cout<<dp[n]<<endl;

    return 0;

}


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

using namespace std;

int arr[100005], memo[100005],k;



int dp(int n){

    if(n==0) return 0;

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

    int cost=1e9;

    for(int i=1; i<=k&&i<=n; ++i) cost=min(dp(n-i)+abs(arr[n]-arr[n-i]),cost);

    return memo[n]=cost;

}



int main(){

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

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

    ios_base::sync_with_stdio(0);

    cin.tie(0);

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

    int n;

    cin>>n>>k;

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

    cout<<dp(n-1)<<"\n";

    //for(int i=0; i<n; ++i) cout<<memo[i]<<"\n";

    return 0;

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

using namespace std;



int a[110], b[110];

int main() {

	int n;

	scanf("%d", &n);

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

		scanf("%d", b + i);

	}

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

		bool is = false;

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

			if (b[j] > j) break;

			if (b[j] == j) {

				a[i] = j;

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

					b[k] = b[k + 1];

				}

				is = true;

				break;

			}

		}

		if (!is) {

			puts("-1");

			return 0;

		}

	}

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

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

	}

}

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



using namespace std;



int a[101];

int b[101];

int main(){

	int n;

	scanf("%d",&n);

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

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

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

		int o=0;

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

			if(a[j]==j)o=j;

		if(o==0){

			printf("-1\n");

			return 0;

		}

		b[++b[0]]=o;

		for(int j=o;j<i;j++)

			a[j]=a[j+1];

	}

	for(int i=b[0];i;i--)

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

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

#include<algorithm>

#include<cmath>

#include<queue>

#include<list>

#include<cstdio>

using namespace std;

int A[100005];

int B[100005];

int C[100005];

int book[100005];

int  main()

{

    int n;

    cin>>n;

    long long sum=0;

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

    	{

    		cin>>A[i];

    		sum+=A[i];

    		book[A[i]]++;

    	}

    int m;

    cin>>m;

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

    	{

    		cin>>B[i]>>C[i];

    		sum=sum+book[B[i]]*(C[i]-B[i]);

    		cout<<sum<<endl;

    		book[C[i]] += book[B[i]];

    		book[B[i]]=0;

    	}



    return 0;

}




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

using namespace std;



int num[100050];



int main()

{

	long long sum=0;

	int N,Q,B,C,a;

	scanf("%d",&N);

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

		num[i]=0;

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

	{

		scanf("%d",&a);

		sum+=(long long)a;

		++num[a];

	}

	scanf("%d",&Q);

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

	{

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

		sum+=(long long)(C-B)*num[B];

		num[C]+=num[B];

		num[B]=0;

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

	}

return 0;

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

using namespace std;



#define pb push_back

#define eb emplace_back

#define mk make_pair

#define fi first

#define sec second

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

#define cc(x)	cout << #x << " = " << x << endl

#define ok		cout << "ok" << endl



typedef pair<int,int> ii;

typedef long long ll;

const int INF = 0x3f3f3f3f;

const double PI = acos(-1.0);



const long long N = 1e5 + 10; 

long long n, k, cost[N];

long long dp[N];



long long pog(long long at, long long n, long long k){

	if(at >= n) return 0;



	long long &r = dp[at];

	if(r != -1) return r;

	

	if(k == 1) r = pog(at+1, n, k) + abs(cost[at+1]-cost[at]);



	if(k != 1){

		long long L = pog(at+1, n, k) + abs(cost[at+1]-cost[at]);

		long long R = pog(at+2, n, k) + abs(cost[at+2]-cost[at]);



		r = min(L, R);



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

			r = min(r, pog(at+i, n, k) + abs(cost[at+i]-cost[at]));

	}	

	return r;

}



int main(){

	ios_base::sync_with_stdio(false);



	memset(dp, -1, sizeof dp);

	cin >>n >>k;



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

		cin >>cost[i];

	

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

		cost[n+i] = INF;



	cout <<pog(1, n, k) <<endl;

	return 0;

}


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

#include <algorithm> 

#include <vector> 



const int inf=0x3f3f3f3f;

using namespace std; 

int main()

{

  ios::sync_with_stdio(false); 

  cin.tie(0); cout.tie(0); 

  int n,k; 

  cin>>n>>k;

  vector <int> a(n);  

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

  vector <int> dp(n, inf); dp[0]=0; 

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

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

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

    }

  }

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

  return 0; 

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

using namespace std;

typedef long long ll;

#define MAXN 100100

const ll inf = (ll)1e10;

ll dp[MAXN];

int n, h[MAXN],k;

ll solve( int pos ) {

	if ( pos == n ) return 0;

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

	ll ret = inf;

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

		if ( pos + i <= n ) {

			ret = min(ret,solve(pos+i)+abs(h[pos]-h[pos+i]));

		}

	}

	return dp[pos] = ret;

}

int main()

{

	#ifdef __APPLE__

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

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

	#endif

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	cin >> n >> k;

	for ( int i = 1; i <= n; i++ ) cin >> h[i];

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

	cout << solve(1) << endl;

	return 0;

}




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

    Author : Simanta Deb Turja

*/

#include <bits/stdc++.h>

using namespace std;



#define Professor



using ll = long long;

using i64 = unsigned long long;



template <typename T> inline T Min (T a, T b, T c) { return min(a, min(b, c)); }

template <typename T> inline T Min (T a, T b, T c, T d) { return min(a, min(b, min(c, d))); }

template <typename T> inline T Max (T a, T b, T c) { return max(a, max(b, c)); }

template <typename T> inline T Max (T a, T b, T c, T d) { return max(a, max(b, max(c, d))); }



template <typename T> inline T Ceil(T a, T b) { return ((a % b == 0) ? (a / b) : (a / b + 1)); } 

template <typename T> inline T Floor(T a, T b) { return a / b; }



template <typename T> inline T Power (T a, T p) { T res = 1, x = a; while ( p ) { if ( p & 1 ) res = ( res * x ); x = ( x * x ); p >>= 1;}return res; }

template <typename T> inline T BigMod (T a,T p,T M) { a %= M;T ret = 1; while (p) { if (p & 1) ret = (ret * a) % M; a = (a * a) % M; p = p >> 1; } return ret; }

template <typename T> inline T InverseMod (T a,T M) { return BigMod (a, M - 2, M); }



template <typename T> inline T gcd ( T a, T b ) { a = abs(a); b = abs(b); while ( b ) { a = a % b; swap ( a, b ); } return a; }

template <typename T> inline T lcm(T x,T y) { return (((x)/gcd((x),(y)))*(y)); }





#define endl '\n'

#define eb emplace_back



const int N = (int)4e5 + 10;

const double EPS = 1e-7;

const double PI = acos(-1.0);

const ll LLINF = numeric_limits<ll>::max();

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

const ll MOD = (ll)1e9 + 7;



template <typename T> bool cmp (const pair <T, T> &a, const pair <T, T> &b) { return a.first < b.first; }





int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr); cout.tie(nullptr);

    auto Solve = [&]() {

        #ifdef __APPLE__

            cout << "Running\n";

        #endif

        int n, k; 

        cin >> n >> k;

        vector <ll> h(n + 1, 0LL), dp(n + 1, (ll)1e10); 

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

            cin >> h[i];

        }

        dp[1] = 0LL;

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

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

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

            }

        }

        cout << dp[n] << endl;

    };

    Solve();

    

    return 0;

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

using namespace std;

int main ()

{

	int min, num, result{0};

	cin>>num>>min;

	int array;

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

	{

		cin>>array;

		if (array>=min) result++;

	}

	cout<<result;

}

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



int main()

{

    int n,k,ans=0;

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

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

    {

        int x;

        scanf("%d",&x);

        if(x>=k)ans++;

    }

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

    return 0;

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

#include <string>

using namespace std;



int main()

{

	int k;

	string s1;

	cin >> k;

	cin >> s1;

	if(s1.length() > k)

	{

		s1.erase(k,s1.length());

		s1 = s1 + "...";

	}

	cout << s1 << endl;

	

	

	return 0;

 } 

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



int main(){

	int k;

	char word[100];

	scanf("%d", &k);

	scanf("%s", &word);

	

	int len = 0;

	

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

		len++;

	}

	

	if(len <= k){

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

	}

	else{

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

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

		}

		printf("...\n");

	}

	return 0;

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

using namespace std;

int main()

{

    int n,i,k,j;

    cin>>n>>k;

    vector<int>v(200000,0);

    vector<int>pre(200000,0);

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

    {

        cin>>v[i];

    }

    int dp[200000];

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

    dp[i]=INT_MAX;

    dp[1]=0;

    dp[2]=abs(v[2]-v[1]);

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

    {

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

        {

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

        }

    }

    cout<<dp[n];

}

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

// Created by rajshah9914 on 29/03/20.

//



#include <bits/stdc++.h>

using namespace std;

const int INF=1e9+5;

int main() {

    long n,k,h[100000];

    cin>>n>>k;

    vector<long>dp(n,INF);

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

        cin>>h[i];

    dp[0]=0;

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

    {

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

        {

            if(j<n)

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

        }

    }

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

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

#include <queue>

#include <string>

#include <map>

#include <stdio.h>

#include <iomanip>

#include <math.h>

#include <vector>

#include <stack>

#include <algorithm>

#include <string.h>

#include <set>

#include <cstdlib>

#include <iostream>

#include <bits/stdc++.h>

#define pii pair<int,int> 

#define ll long long int 

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

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

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

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

#define fnm(n,m) for(ll i=n;i<m+1;++i)

#define pb push_back

#define pf printf

#define F first

#define S second

#define srt(v) sort(v.begin(),v.end());

#define srtc(v,cmp) sort(v.begin(),v.end(),cmp);

#define srta(v,n) sort(v,v+n);

const ll LL_INF=0x3f3f3f3f3f3f;

const ll MOD=1073741824;





using namespace std;





int T;

int n,k;

int arr[100005];

ll dp[100005];



ll gen(int i){

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

    ll &ret = dp[i];

    if(~ret)return ret;

    ret =LL_INF;

    fo(j,k){

        if(i+j<n)ret=min(gen(i+j)+abs(arr[i]-arr[i+j]),ret);



    }

    // ret=gen(i+1)+abs(arr[i]-arr[i+1]);

    return ret;

}





int main(){

    // pf("\t\t\t***************************************************************************\n\n");

    // pf("\t\t\t***************************************************************************\n\n");

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

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

    memset(dp,-1,sizeof dp);

    s(n);

    s(k);

    f(i,n)s(arr[i]);



    pf("%lld\n",gen(0));



    return 0;

} 

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

#define x first

#define y second

#define pii pair<int,int>

#define ll long long

#define pll pair<ll,ll>

#define mp make_pair

#define pb push_back

#define pf push_front

#define popb pop_back

#define popf pop_front

#define xmod 100000007

using namespace std;



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

int main (){

	ios::sync_with_stdio(false);

	cin >> n >> m;

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

		cin >> a[i];

	}

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

		dp[i]=1e9;

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

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

		}

	}

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

    return 0;

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

using namespace std;

const int N=100005;

int a[N],dp[N];

int main()

{

	int n,k;

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

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

		dp[i]=INT_MAX;

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

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

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

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

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

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

	return 0;

}


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

//hari hari

int main(){

	int n,k;

	cin>>n>>k;

	vector<int>arr(n+1);

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

		cin>>arr[i];

	}

	vector<int>dp(n+1,INT_MAX);

	dp[1]=0;

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

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

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

             }

        }

	cout<<dp[n]<<endl;

	return 0;

}

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

}