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

#include <string> // string, to_string, stoi

#include <vector> // vector

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound

#include <utility> // pair, make_pair

#include <tuple> // tuple, make_tuple

#include <cstdint> // int64_t, int*_t

#include <cstdio> // printf

#include <map> // map

#include <queue> // queue, priority_queue

#include <set> // set

#include <stack> // stack

#include <deque> // deque

#include <unordered_map> // unordered_map

#include <unordered_set> // unordered_set

#include <bitset> // bitset



#include <math.h>

#include <stdlib.h>





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

using namespace std;

typedef long long ll;



template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true; } return false; }

template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true; } return false; }



//INT_MAX	2,147,483,647 = 2*1.0e9



int calc(int a){

  if(a%2 == 0) return a/2;

  else return 3 * a + 1;

}



int main(){

  int s;

  cin >> s;



  vector<int> a(1000010,-1);

  a[0] = s;



  int ans = -1;

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

    a[i] = calc(a[i-1]);

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

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

        cout << i+1 << endl;

        return 0;

      }

    }

  }

  

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.050849963184556x):
#include <iostream> // cout, endl, cin

#include <string> // string, to_string, stoi

#include <vector> // vector

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound

#include <utility> // pair, make_pair

#include <tuple> // tuple, make_tuple

#include <cstdint> // int64_t, int*_t

#include <cstdio> // printf

#include <map> // map

#include <queue> // queue, priority_queue

#include <set> // set

#include <stack> // stack

#include <deque> // deque

#include <unordered_map> // unordered_map

#include <unordered_set> // unordered_set

#include <bitset> // bitset



#include <math.h>

#include <stdlib.h>





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

using namespace std;

typedef long long ll;



template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true; } return false; }

template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true; } return false; }



//INT_MAX	2,147,483,647 = 2*1.0e9



int main(){

  int s;

  cin >> s;



  set<int> st;



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

    st.insert(s);

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

    else s = 3*s+1;

  }



  cout << st.size() + 1 << endl;

  

  return 0;

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

#include<string>

using namespace std;

int main(){

	string s;

	int n;

	while(cin>>n){

		cin>>s;

		if(n<s.size()){

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

			cout<<s[i];	

		}

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

	}

		else	

			cout<<s<<endl;	

	}

	return 0;

}

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

#include<string.h>



int main()

{

	int n;

	scanf("%d",&n);

	char str[102];

	scanf("%s",&str);

	int len=strlen(str);

	if(len>n)

	{

		str[n]='.';

		str[n+1]='.';

		str[n+2]='.';

		str[n+3]='\0';

	}

	printf("%s",str);

	return 0;

}
// Slower C++ solution:
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#include <unordered_map>

#define stop(x) {cout<<x<<endl;exit(0);}

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

#define szz(s) int(s.size())

#define endl '\n'

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

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

typedef long long ll;

using namespace std;



void file() {

    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE

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

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

#endif

}







int main() {

    //file();



    ll n, m;

    cin >> n >> m;

    vector < ll > v(n);



    for (auto& e : v)cin >> e;



    vector < ll > dp(n + 1, 1e11);



    dp[0] = 0;



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

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

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

        }

    }



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



        

}

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

namespace chtholly{

typedef long long ll;

#define re0 register int

#define rel register ll

#define rec register char

#define gc getchar

#define pc putchar

#define p32 pc(' ')

#define pl puts("")

/*By Citrus*/

inline int read(){

  int x=0,f=1;char c=gc();

  for (;!isdigit(c);c=gc()) f^=c=='-';

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return f?x:-x;

  }

template <typename mitsuha>

inline bool read(mitsuha &x){

  x=0;int f=1;char c=gc();

  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';

  if (!~c) return 0;

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return x=f?x:-x,1;

  }

template <typename mitsuha>

inline int write(mitsuha x){

  if (!x) return 0&pc(48);

  if (x<0) pc('-'),x=-x;

  int bit[20],i,p=0;

  for (;x;x/=10) bit[++p]=x%10;

  for (i=p;i;--i) pc(bit[i]+48);

  return 0;

  }

inline char fuhao(){

  char c=gc();

  for (;isspace(c);c=gc());

  return c;

  }

}using namespace chtholly;

using namespace std;

const int yuzu=1e5;

typedef int fuko[yuzu<<1|10];

struct _bit{

fuko c;

void init(){memset(c,0,sizeof c);}

void add(int x){for (;x<=yuzu<<1;x+=x&-x) c[x]++;}

int query(int x){

  int llx=0;

  for (;x;x&=x-1) llx+=c[x]; 

  return llx;

  }

}my_;

fuko b,a;

bool judge(int x,int n,int i=1){

my_.init();

for (;i<=n;++i) b[i]=b[i-1]+(a[i]>=x?1:-1);

ll llx=0;

for (i=0;i<=n;++i) llx+=my_.query(b[i]+yuzu),my_.add(b[i]+yuzu);

return llx>=1ll*n*(n+1)/4;

}

int main(){

int i,n=read(),l=0,r=0;

for (i=1;i<=n;++i) r=max(a[i]=read(),r);

for (;l<=r;){

  int mid=l+r>>1;

  judge(mid,n)?l=mid+1:r=mid-1;

  }

write(l-1);

}

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

//CLOCKS_PER_SEC

#define se second

#define fi first

#define ll long long

#define Pii pair<int,int>

#define Pli pair<ll,int>

#define ull unsigned long long

#define pb push_back

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

const int N=1e5+10;

const int INF=0x3f3f3f3f;

const int mod=1e9+7;

using namespace std;

int n,val[N<<1];

int a[N],b[N];

int sum[N];

void add(int x){

	while(x<N*2){

		val[x]+=1;

		x+=x&-x;

	}

}

ll get(int x){

	ll ans=0;

	while(x>0){

		ans+=val[x];x-=x&-x;

	}

	return ans;

}

bool check(int x){

	for(int i=1;i<=n;++i)sum[i]=sum[i-1]+(a[i]>=x?1:-1);

	ll tot=0;

	memset(val,0,sizeof(val));

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

		tot+=get(sum[i]+N);

		add(sum[i]+N);

	}

	return tot>=1LL*n*(n+1)/4;

}

int main(){

	scanf("%d",&n);

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

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

	}

	sort(b+1,b+1+n);

	int l=1,r=n;

	int ans=1;

	while(l<=r){

		int mid=(l+r)>>1;

		if(check(b[mid])){

			ans=mid;

			l=mid+1;

		}

		else r=mid-1;

	}

	cout<<b[ans]<<endl;

	return 0;

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



using namespace std;



const int N = 200001;

int h[N], dp[N];

int n, k;



int solve(int pos) {

    if (pos == 1) {

        return 0;

    }

    int& ans = dp[pos];

    if (ans != -1) {

        return ans;

    }

    ans = 2e9;

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

        if (pos - i >= 1) {

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

        }

    }

    return ans;

}



int main() {

    cin >> n >> k;

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

        cin >> h[i];

    }

    memset(dp, -1, sizeof dp);

    cout << solve(n);

    return 0;

}


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



using namespace std;



const int N = 1e5 + 1;

int dp[N], h[N];



int main() {

    int n, k;

    cin >> n >> k;

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

        cin >> h[i];

    }

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

        dp[i] = 1e9;

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

            if (i - j >= 1) {

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

            }

        }

    }

    cout << dp[n];

    return 0;

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

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <cmath>

#include <algorithm>

#include <queue>

#include <map>

#include <set>

#include <vector>

#include <string>

#include <stack>

#include <bitset>

#define maxn 100009

using namespace std;

const int MOD=1e9+7;

int p[maxn];

long long bin[maxn];

int findset(int x)

{

	return x==p[x]?x:p[x]=findset(p[x]);

}

void unionset(int x,int y)

{

	 p[findset(x)]=findset(y);

}

int main()

{

	int n,m,x,y;

	bin[0]=1;

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

		bin[i]=2*bin[i-1]%MOD;

	while(scanf("%d%d",&n,&m)!=EOF&&n+m)

	{

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

			p[i]=i;

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

		{

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

			unionset(x,y);

		}

		int cnt=0;

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

		{

			if(p[i]==i)

			{

				cnt++;

			}

		}

		int ans=bin[cnt];

		if(cnt!=n)

			ans=(ans+1)%MOD;

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

	}

	return 0;

}

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

using namespace std;

using LL = long long; using ll = LL;

using PLL = pair<LL, LL>; using pll = PLL;

using PII = pair<int, int>; using pii = PII;

const int INF = 1e9; const LL LINF = 1e18;

#define SZ(a) (int)a.size()

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

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

using VL = vector<LL>; using VLL = vector<VL>;



struct UnionFind {

	vector<int> data;

	UnionFind(int size) { init(size); }

	void init(int size) { data.clear(); data.resize(size, -1); }

	bool unite(int x, int y) {

		x = root(x); y = root(y);

		if (x != y) {

			if (data[y] < data[x]) swap(x, y);

			data[x] += data[y]; data[y] = x;

		}

		return x != y;

	}

	bool same(int x, int y) {

		return root(x) == root(y);

	}

	int root(int x) {

		return data[x] < 0 ? x : data[x] = root(data[x]);

	}

	int size(int x) {

		return -data[root(x)];

	}

};



void solve() {

	int N, M;

	while (cin >> N >> M, N) {

		UnionFind uf(N);

		FOR(i, 0, M) {

			int a, b;

			cin >> a >> b;

			a--, b--;

			uf.unite(a, b);

		}

		VL used(N, 0);

		int isgroup = 0;

		int cnt = 0;

		FOR(i, 0, N) {

			int root = uf.root(i);

			if (used[root])continue;

			cnt++;

			if (uf.size(root) != 1)isgroup=1;

			used[root] = 1;

		}

		LL nene = 1;

		const LL mod = 1e9 + 7;



		FOR(i, 0, cnt) {

			nene *= 2;

			nene %= mod;

		}

		nene += isgroup;

		nene %= mod;

		cout << nene << endl;

	}



}



int main() {

	cin.tie(0);

	ios_base::sync_with_stdio(false);

	solve();

	return 0;

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

using namespace std;



int main() {

    int a[1000010]={};

    int s;

    cin >> s;

    a[s]++;

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

        if(s%2==0){

            if(a[s/2]==1){

                cout << i+1 << endl;

                return 0;

            }

            else{

                a[s/2]++;

                s /= 2;

            }

        }

        else{

            if(a[s*3+1]==1){

                cout << i+1 << endl;

                return 0;

            }

            else{

                a[s*3+1]++;

                s = 3*s +1;

            }

        }

    }

}




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

using namespace std;



int main() {

    int s;

    cin >> s;

    set<int> set1;

    set1.insert(s);

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

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

        else s = 3*s+1;

        set1.insert(s);

        if(set1.size()!=i+1){

            cout << i+1 << endl;

            return 0;

        }

    }

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

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<algorithm>

//deque/functional/iterator/list/map/memory/numeric/queue/set/stack/utility/vector

//#include<bits/stdc++.h>

#define ll long long

using namespace std;

const int mod=1e9+7;



ll fact(ll n){

    if(n==0) return 1;

    else return (n*fact(n-1))%mod;

}



int main(){

    ios::sync_with_stdio(false);

    ll n;

    cin>>n;

    cout<<fact(n)<<endl;

    return 0;

}


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

const long long MOD=1e9+7;

int n;

long long ans=1;

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++) ans=(ans*i)%MOD;

	printf("%lld",ans);

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

using namespace std;

int main(){

  int n,c,h[100005],dp[100005]={};

  int k;

  cin>>n>>k;

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

    cin>>h[i];

    dp[i]=1000000000;

  }

  dp[1]=0;

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

    c=min(i-1,k);

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

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

    }

  }

  cout<<dp[n]<<endl;

  return(0);

}


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

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

using namespace std;

int main(){

	cin.tie(0);

    ios::sync_with_stdio(false);

    long long n,k,h[100005],dp[100005];

    cin>>n>>k;

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

        cin>>h[i];

    }

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

        dp[i]=10000000000;

    }

    dp[0]=0;

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

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

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

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

        }

    }

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

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

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

    }

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

    return(0);

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

using namespace std;

long long a=1,n;

int main(){

cin>>n;

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

a=a*i%1000000007;

cout<<a;}

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

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

using namespace std;



int main(){

    string s;

    string ans = "";

    int t = 0;



    cin >> s;

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

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

            ans[t++] = '0';

        }

        else if(s[i]=='1'){

            ans[t++] = '1';

        }

        else if(s[i]=='B'&&t > 0){

            t--;

        }

    }



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

        cout << ans[i];

    }

}


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

#include<string.h>

int main(){

    char G[100], H[100];

    int A=0;

    scanf("%s", G);

    int len=strlen(G);

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

    {

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

        {

            A--;

            if(A<0)

                A=0;

        }

        else

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

    }

    H[A]='\0';

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

    return 0;

}
// Slower C++ solution:
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



int n;

int k;

int h[N];

int mem[N];



int go(int idx){

    if(idx >= n)

        return 0;

    if(mem[idx] != -1)

        return mem[idx];

    int best = 1e9;

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

        if(idx + i <= n){

            best = min(best , go(idx + i) + abs(h[idx] - h[idx + i]));

        }

    }

    return mem[idx] = best;

}



int main(){

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

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

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

    }

    memset(mem , -1 , sizeof mem);

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

}


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

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

#include <cmath>

#include <algorithm>

using namespace std;



const int INF = pow(2,30);

const int MAX_N = pow(10,5);



int dp[MAX_N];



int main() {

  int N, K; cin >> N >> K;

  int h[N]; for (int i=0; i<N; i++) cin >> h[i];



  fill_n(dp,MAX_N,INF); dp[0] = 0;

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

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

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

    }

  }

  // for (int i=0; i<N; i++) cout << dp[i] << endl;

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

  return 0;

}

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



using namespace std;

typedef long long ll;

typedef pair<ll, ll> p_ll;



template<class T>

void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }

#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)

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

#define rep(i,N) repr(i,0,N)

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



const ll MOD = pow(10,9)+7;

const ll LLINF = pow(2,61)-1;

const int INF = pow(2,30)-1;



vector<ll> fac;

void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }

ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }

ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }

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

ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }





int main() {

  int N, K; cin >> N >> K;

  int h[N]; rep(i,N) cin >> h[i];

  int result[N] = {};

  repr(i,1,N) {

    result[i] = INF;

    repr(j,1,min(i+1,K+1)) result[i] = min(result[i], result[i-j]+abs(h[i]-h[i-j]));

  }

  // debug(result,result+N);

  cout << result[N-1] << endl;

  return 0;

}
// Slower C++ solution:
/* BISMILLAHIR RAHMANIR RAHIM */

#include <bits/stdc++.h>

using namespace std;



const int mxn = 100005;

const int inf = 1e7;

int n, k, h[mxn];

int ff[mxn];



int dfs( int pos ) 

{

  if( pos > n ) return 0;

  int& ret = ff[pos];

  if( ~ret ) return ret;

  

  ret = 0;

  if( pos < n )

  {

    ret = abs(h[pos] - h[pos + 1]) + dfs( pos + 1 );

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

    {

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

    }

  }

  return ret;

}



int main()

{

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

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

  { 

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

  }

  memset(ff, -1, sizeof ff);

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

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.406276455307806x):
/* BISMILLAHIR RAHMANIR RAHIM */

#include <bits/stdc++.h>

using namespace std;



const int mxn = 100005;

int n, k, h[mxn];

int ff[mxn];



void solution()

{

  ff[n] = 0;

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

  {

    ff[i] = INT_MAX;

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

    {

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

    }

  }

}



int main()

{

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

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

  { 

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

  }

  solution();

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

  return 0;

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

#include<iostream>

#include<algorithm>

using namespace std;

const int maxn = 1e5 + 5;

int main()

{

	int n , m ,k , a[maxn ] , dp[maxn];

	for(int i = 1;i <= 1e5 + 5;i++)dp[i] = 1e9;

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

	for(int i = 1;i <= n;i++)scanf("%d",&a[i]);

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

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

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

			if(j == i) break;

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

		}

	}

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

	return 0;

}


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

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

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

//比如从k上面跳过来

//dp[i] = dp[k] + abs(h[i] - h[k])

//关键是你k不知道啊

//有想法就好 不用先急着自我否认

//枚举

//dp[1] = 0

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

//写个函数来算看起来不错

//dp[3] = min(从1跳过来,从2跳过来)

//dp[4] = min(从1跳,从2跳，从3跳)用循环 最后求min

//差不多2层吧 先写了试试看

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <queue>

#include <stack>

#include <vector>

#include <algorithm>



using namespace std;



const int MAXN = 1e5;

int n,k;

int h[MAXN + 5],dp[MAXN + 5];//



int CHANGE(int i,int k)//

{

	if(i <= k + 1)//一跳就到

	{//从一开始选

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

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

		{

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

			{

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

			}

		}

		return min;

	}

	else

	{

		int min = dp[i - k] + abs(h[i] - h[i - k]);

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

		{

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

			{

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

			}

		}

		return min;

	}

}



int main()

{

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

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

	{

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

	}

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

	{

		if(i == 1)

		{

			dp[i] = 0;

		}

		else if(i == 2)

		{

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

		}

		else

		{

			dp[i] = CHANGE(i,k);

		}

	}

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

	return 0;

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

using namespace std;

long long n,h[100010],dp[100010],k;



int main()

{

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

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

	{

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

	}

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

	{

	//	printf("%d %d %d %d\n",dp[i-1],dp[i-2],h[i-1]-h[i],h[i-2]-h[i]);

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

		{

			if(i>j)

			{

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

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

			}

		}

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

	}

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

	return 0;

}

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

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

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

//比如从k上面跳过来

//dp[i] = dp[k] + abs(h[i] - h[k])

//关键是你k不知道啊

//有想法就好 不用先急着自我否认

//枚举

//dp[1] = 0

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

//写个函数来算看起来不错

//dp[3] = min(从1跳过来,从2跳过来)

//dp[4] = min(从1跳,从2跳，从3跳)用循环 最后求min

//差不多2层吧 先写了试试看

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <queue>

#include <stack>

#include <vector>

#include <algorithm>



using namespace std;



const int MAXN = 1e5;

int n,k;

int h[MAXN + 5],dp[MAXN + 5];//



int CHANGE(int i,int k)//

{

	if(i <= k + 1)//一跳就到

	{//从一开始选

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

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

		{

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

			{

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

			}

		}

		return min;

	}

	else

	{

		int min = dp[i - k] + abs(h[i] - h[i - k]);

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

		{

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

			{

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

			}

		}

		return min;

	}

}



int main()

{

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

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

	{

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

	}

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

	{

		if(i == 1)

		{

			dp[i] = 0;

		}

		else if(i == 2)

		{

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

		}

		else

		{

			dp[i] = CHANGE(i,k);

		}

	}

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

	return 0;

}