stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int main()

{

    int arr[100000], pen[100000], i;

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

    {

        arr[i]=0;

        pen[i]=0;

    }

    int n, m, penal= 0, score =0, num;

    string s;

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

    getchar();

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

    {

        scanf("%d", &num);

        getchar();

        cin >> s;

        getchar();

        if(arr[num]==0)

        {

            if(s[0]=='W')

                pen[num]++;

            else

            {

                arr[num]=1;

                penal+=pen[num];

                score++;

            }

        }

    }

    printf("%d %d", score, penal);



    return 0;

}


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

#include<iostream>

using namespace std;

int a[100010]={0};

int main()

{

	int n,m,i;

	cin>>n>>m;

	int x=0,y=0;

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

	{

		int s;string ss;

		cin>>s>>ss;

		if(a[s]>=0)

		{

			if(ss=="WA")

			{

				a[s]++;

			}

			else

			{

				x+=a[s];

				a[s]=-1;

				y++;

				

			}

		}

	}

	printf("%d %d\n",y,x);

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

using namespace std;

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

#define REP(i,p,n) for(int i=p;i<(int)(n);++i)

#define rep(i,n) REP(i,0,n)

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

#define INF 999999999

typedef long long int Int;

using ll = long long;

using VI = vector<int>;



int main(){

  Int s;cin >> s;

  Int a[1000000] = {0};

  Int x = s;

  Int i = 1;

  a[x-1] = i;

  Int ans=0;

  bool flag = false;

  while(1){

    if(flag) break;

    i++;

    if(x%2==0){

      x/=2;

      if(a[x-1]==0) a[x-1] = i;

      else{

        ans = i;

        flag = true;

      }

    }

    else if(x%2==1){

      x=3*x+1;

      if(a[x-1]==0) a[x-1] = i;

      else{

        ans = i;

        flag = true;

      }

    }

    if(flag) break;

  }

  cout << ans << endl;

}


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

using namespace std;

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

#define REP(i,p,n) for(int i=p;i<(int)(n);++i)

#define rep(i,n) REP(i,0,n)

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

#define INF 999999999

typedef long long int Int;

using ll = long long;

using VI = vector<int>;



int main(){

  Int s;

  cin >> s;

  map<Int,Int> mp;

  Int i=1;

  while(!mp.count(s)){

    mp[s]=i++;

    if(s&1) s=s*3+1;

    else s/=2;

  }

  cout << i << endl;

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

#include<vector>

#include<map>

using namespace std;

typedef long long int ll;

vector<ll>dat;

vector<ll>res;

ll m(ll i,ll j){

    if(res[i]!=-1)return res[i];

    if(i>=dat.size()-1){

        return 0;

    }

    res[i]=1e9;

    for(ll k=1;k<=j&&i+k<dat.size();k++){

        res[i]=min(res[i],abs(dat[i]-dat[i+k])+m(i+k,j));

    }

    return res[i];

}

int main(){

    ll a,aux,k;

    cin>>a>>k;

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

        cin>>aux;

        dat.push_back(aux);

    }

    res=vector<ll> (a,-1);

    cout<<m(0,k)<<endl;

    return 0;

}


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

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

#include<iostream>

#include<cstring>

#include<algorithm>

using namespace std;



const int MAXN = 1e5 + 100;

const int inf = 0x3f3f3f3f;

int n, k;

int a[MAXN], dp[MAXN];



int main(){

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

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

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

	}

	memset(dp, 0x3f, sizeof dp);

	dp[1] = 0;

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

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

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

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

		}

	}

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

}

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



int jump(int stone){

	if(dp[stone]==-1){

		if(stone == n-1){

			return 0;

		}

		else{

			dp[stone] = INT_MAX;

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

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

			}

			return dp[stone];

		}

	}

	else{

		return dp[stone];

	}

}



main(){

	cin >> n >> k;

	

	dp.assign(n, -1);

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

		int x;

		cin >> x;

		h.push_back(x);

	}



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

}
// Slower C++ solution:
																//  SMOKE SHISHA PLAY FIFA  // 

#include<bits/stdc++.h>

using namespace std;



typedef long long int ll;

typedef long double ld;



#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);

#define pll pair<ll,ll>

#define MP make_pair

#define ff first

#define ss second

#define PB push_back

#define mod 1000000007

#define lp(i,start,end) for(ll i=start;i<=end;i++) 

#define deb1(a) cout<<#a<<" = "<<(a)<<endl;

#define deb2(a,b) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<endl;

#define deb3(a,b,c) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<", "<<#c<<" = "<<(c)<<endl;





ll modu(ll a, ll b)

{

	ll ans =1;

	while(b>0)

	{

		if(b&1)

		ans = (ans*a)%mod;

		b/=2;

		a = (a*a)%mod;

	}

	return ans;

}



int main()

{

	fastio

	ll t=1;

	//cin >> t;

	while(t--)

	{

		ll n,k;

		cin>>n>>k;

		ll a[n+1];

		ll cost[n+1];

		lp(i,1,n){

			cin>>a[i];

			cost[i]=mod;

		}

		cost[1]=0;

		lp(i,2,n){

			for(ll j=i-1; j>= max((ll)1,i-k) ;j--){

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

			}

		}

		cout<<cost[n]<<endl;

	}

	return 0;	

}


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

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

#include<bits/stdc++.h>

#include<unordered_map>

using namespace std;

//<DEFINE>

void Fast() {

	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

}

void File() {

#ifndef ONLINE_JUDGE

	freopen("Input.txt", "r", stdin);

	freopen("Output.txt", "w", stdout);

#endif

}

#define ll long long

#define ull unsigned long long

#define mod 1000000007

ll gcd(ll a, ll b) {

	return!b ? a : gcd(b, a % b);

}

#define fix(n) cout << fixed << setprecision(n);

#define numOfOnes(n) __builtin_popcount(n)

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

#define skip continue

#define stop(n) return cout<<n,0

#define pi  acos(-1)

#define endl "\n"

#define ceil(x,y) (x+y-1)/y

//</DEFINE>



/*█▀▀▄░░░░░░░░░░░▄▀▀█

░█░░░▀▄░▄▄▄▄▄░▄▀░░░█

░░▀▄░░░▀░░░░░▀░░░▄▀

░░░░▌░▄▄░░░▄▄░▐▀▀

░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█

░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█

▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█

█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀

░▀▄░░▀░░▀▀▀░░▀░░░▄█▀

░░░█░░░░░░░░░░░▄▀▄░▀▄

░░░█░░░░░░░░░▄▀█░░█░░█

░░░█░░░░░░░░░░░█▄█░░▄▀

░░░█░░░░░░░░░░░████▀

░░░▀█▄▄▄▀▀▀▀▄▄▄█▀*/

int n,k;

vector<int>v;

ll res[100001];

//<FUNCTIONS>

ll solve(int idx = 0) {

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

	if (~(res[idx]))return res[idx];

	ll mn = INT_MAX;

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

		if (idx + i >= n)break;

		mn = min(mn, solve(idx + i) + abs(v[idx] - v[idx + i]));

	}

	return res[idx] = mn;

}

//</FUNCTIONS>		

int main() {

	Fast();

	cin >> n >> k;

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

		res[i] = -1;

	v.resize(n);

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

	cout << solve() << endl;

}


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

const int Maxn=1e5+10;

int N,K;

int H[Maxn],Dp[Maxn];

int main()

{

	cin>>N>>K;

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

	Dp[1]=0;

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

	{

		cin>>H[i]; 

		for(int j=i-1;j>=max(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.4005743319243291x):
#include "bits/stdc++.h"

using namespace std;

const int maxN = 1e5 + 10;

int n, k, h[maxN], dp[maxN];

int main()

{

	cin >> n >> k;

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

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

	{

		int mn = 2e9;

		for(int j = 1; j <= k && i - j >= 0; j++) mn = min(mn, dp[i - j] + abs(h[i] - h[i - j]));

		dp[i] = mn;

	}

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

	return 0;

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

using namespace std;

#define ll long long

#define ull unsigned long long

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

#define mp(a, b) make_pair(a, b)

#define pi acos(-1)

#define pii pair<int, int>

#define pb push_back

const int INF = 0x3f3f3f3f;

const double eps = 1e-10;

const int maxn = 1e5 + 7;

const int maxm = 5e5 + 10;

ll a[maxn];

ll dp[maxn];

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int n, m;

    cin >> n >> m;

    mst(dp, INF);

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

        cin >> a[i];

    dp[0] = 0;

    dp[1] = 0;

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

    {

        for (int j = i - 1; j >= max(1, i - m); --j)

        {

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

        }

    }

    cout << dp[n] << endl;

    return 0;

}

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

# define ll long long

# define fi first

# define se second



using namespace std ;



ll n, x, k;

ll ans[200001], vc[200001];



int main() {

    cin >> n >> k;

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

        cin >> vc[i] ;

    }

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

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

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

            if( j <= i ) ans[i] = min(ans[i], abs(vc[i]-vc[i-j]) + ans[i-j]) ;

        }

    }

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

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

#include <cstring>

#include <algorithm>



int Max[550000], cMax[550000]; 

int n, a[550000], Ans[550000];

int o[4];



int main()

{

	scanf("%d", &n);

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

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

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

	Max[0] = a[0];

	if(a[1] > a[0]) Max[1] = a[1], cMax[1] = a[0];

	else Max[1] = a[0], cMax[1] = a[1];

	Ans[1] = a[0] + a[1];

	printf("%d\n", a[0] + a[1]);

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

	{

		int S = 0, b;

		for(int j = 17; j >= 0; j--)

		{

			if((i >> j) & 1)

			{

				S = i ^ (1 << j);

				b = j;

				break;

			}

		}

		b = 1 << b;

		for(int j = S; ; j = ((j - 1) & S))

		{

			int u = a[b ^ j];

			if(u > Max[i]) cMax[i] = Max[i], Max[i] = u;

			else if(u > cMax[i]) cMax[i] = u;

			if(j == 0) break;

		}

		o[0] = Max[i]; o[1] = cMax[i]; o[2] = Max[S]; o[3] = cMax[S];

		std::sort(o, o + 4);

		Max[i] = o[3]; cMax[i] = o[2];

		if(cMax[i] == -1)

		{

			Ans[i] = Ans[i - 1];

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

		}

		else

		{

			Ans[i] = std::max(Max[i] + cMax[i], Ans[i - 1]);

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

		}

	}

	return 0;

}

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

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<algorithm>

#include<cmath>

using namespace std;

int n,ans=0,tot;

int num[300010];

struct node{

	int maxx,cmax;

}a[300010];

void chan(int x,int pos){

	if(x==a[pos].cmax || x==a[pos].maxx)  return ;

	if(num[x]>num[a[pos].maxx]){

		a[pos].cmax=a[pos].maxx;a[pos].maxx=x;

	}

	else{

		if(num[x]>num[a[pos].cmax])  a[pos].cmax=x;

	}

}

int main(){

	scanf("%d",&n);

	int i,j;

	tot=(1<<n);num[tot]=0;

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

		scanf("%d",&num[i]);a[i].maxx=i;a[i].cmax=tot;

	}

	for(i=1;i<tot;++i){

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

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

				int tt=i^(1<<j);

				chan(a[tt].maxx,i);

				chan(a[tt].cmax,i);

			}

		}

	}

	ans=0;

	for(i=1;i<tot;++i){

		ans=max(ans,num[a[i].maxx]+num[a[i].cmax]);

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

	}	

	return 0;

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

using namespace std;

#define gcd(a,b) __gcd(a,b)

#define ll long long int

#define ld unsigned long long int

#define pi pair<ll,ll>

#define pb push_back

#define pf push_front

#define pu push

#define po pop

#define fi first

#define se second

#define mk make_pair

#define ve vector

#define lr(n) for(ll i=0;i<n;i++)

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



ll dp[100001];

ve<ll>ar(100001);



ll fun(ll n,ll k){

	if(n==0){

		return 0;

	}

	if(dp[n]==-1){

		dp[n]=abs(ar[n]-ar[n-1])+fun(n-1,k);

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

			if(n>i){

				dp[n]=min(dp[n],abs(ar[n]-ar[n-i-1])+fun(n-i-1,k));

			}

			else{

				break;

			}

	}

	return dp[n];

}



	int main(){

		ios_base::sync_with_stdio(0);

		cin.tie(0);

		cout.tie(0);

		ll n,k;

		cin>>n>>k;

		lr(n){

			cin>>ar[i];

		}

		lr(100001){

			dp[i]=-1;

		}

		cout << fun(n-1,k) << "\n";

		return 0;

	}

// Faster C++ solution for the same problem (measured speedup: 2.5964018963709146x):
// #pragma GCC optimize("Ofast")

// #pragma GCC optimize ("unroll-loops")

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

 

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

 

using namespace __gnu_pbds;

using namespace std;

#define gcd(a,b) __gcd(a,b)

#define ll long long int

#define ld unsigned long long int

#define pi pair<ll,ll>

#define pb push_back

#define pf push_front

#define pu push

#define po pop

#define fi first

#define se second

#define mk make_pair

#define ve vector

#define lr(n) for(ll i=0;i<n;i++)

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

#define be begin

#define sz(a) (ll)a.size()

#define INF 1e18

 

typedef tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

// order_of_key(val): returns the number of values less than val

// find_by_order(k): returns an iterator to the kth largest element (0-based)

 

// ll mod=998244353;

// ll gcd(ll a, ll b){

//     if(a<b) swap(a,b);

//     if(b==0) return a;

//     return gcd(b,a%b);

// }

 

// ll fun(ll x, ll y){

//     ll rem=1;

//     while(y!=0){

//         if(y%2==1){

//             rem=(rem*x)%mod;

//         }

//         x=(x*x)%mod;

//         y/=2;

//     }

//     return rem;

// }



int main(){

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

	ll n,k;

	cin>>n>>k;

	ve<ll>ar(n+1);

	lr(n){

		cin>>ar[i+1];

	}

	ll dp[n+1]={0};

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

		dp[i]=INF;

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

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

		}

	}

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

	return 0;

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



using namespace std;



int main()

{

    int N;

    cin >> N;



    int A[N];

    int freq[100001];

    memset(freq, 0, sizeof freq);



    long long int sum=0;

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

    {

        cin >> A[i];

        freq[A[i]]++;

        sum+=A[i];

    }



    int Q;

    cin >> Q;



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

    {

        int B, C;

        cin >> B >> C;

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

        //cout << freq[C] << " " << freq[B] << endl;

        freq[C]+=freq[B];

        freq[B]=0;

        cout << sum << endl;

    }



}


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

#include <cstdio>



using namespace std;

const int MAXN = 2e5 + 5;



int n, q, a[MAXN], cnt[MAXN];

long long sum;

int main() {

	scanf("%d", &n);

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

		scanf("%d", a + i), cnt[a[i]]++, sum += a[i];

	}

	scanf("%d", &q);

	for (int b, c, i = 1; i <= q; i++) {

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

		sum = sum + (long long)cnt[b] * (c - b);

		cnt[c] += cnt[b];

		cnt[b] = 0;

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

	}

	return 0;

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

using namespace std;

int arr[100010],n,k;

int dp[100010];

int f(int pos){

	if(pos>=n-1)return 0;

	int &ret=dp[pos];

	if(ret^-1)return ret;

	ret=1000000010;

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

		return ret;

}



int main(int argc, char const *argv[])

{

	memset(dp,-1,sizeof dp);

	cin>>n>>k;

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

		cout<<f(0)<<endl;

	return 0;

}

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

#define ll long long

using namespace std;

ll f(ll a){

if(a<0){

    a=0;

}

return a;

}

int main(){

    ll n,k;

    cin>>n>>k;

    ll a[n],dp[n];

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

        cin>>a[i];

        dp[i]=9999999999;

    }

    dp[0]=0;

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

            ll q=f(i-k);

    //cout<<q<<endl;

        for(ll j=q;j<i;j++)

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

        //cout<<dp[i]<<" ";

    }

    cout<<dp[n-1];

return 0;

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

#include <cmath>



using namespace std;



int path(int pedra);



int n_pedras, pulo_maximo;



int altura_pedras[100001];



int custos[100001];



int main(){

    cin >> n_pedras >> pulo_maximo;



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

        cin >> altura_pedras[i];

    }



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

        custos[i] = -1;

    }



    int custo_minimo = path(1);



    cout << custo_minimo << endl;



    return 0;

}



int path(int pedra){

    if(pedra == n_pedras){

        return 0;

    }



    if(custos[pedra] == -1){

        int pulos[pulo_maximo + 1];



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

            if(pedra + i <= n_pedras){

                 pulos[i] = abs(altura_pedras[pedra] - altura_pedras[pedra + i]) + path(pedra + i);

            }

        }



        int custo_minimo = pulos[1];



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

            if(pedra + i <= n_pedras){

                if(pulos[i] < custo_minimo){

                    custo_minimo = pulos[i];

                }

            }

        }



        custos[pedra] = custo_minimo;

    }



    return custos[pedra];

}

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

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

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

int main()

{

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

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

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

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

	f[1]=0;

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

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

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

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

	return 0;

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

#include<cstring>

#include<vector>

#include<stack> 

#include<cstdio>

using namespace std;

string a;

int main()

{

	vector<char>q;

	while(cin>>a)

	{

		int len=a.size();

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

		{

			if(a[i]=='0')

				q.push_back('0');

			else if(a[i]=='1')

				q.push_back('1');

			else if(!q.empty())	

				q.pop_back();

		}

		int j=q.size();

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

		{

			cout<<q[i];

		}

		cout<<endl;

	}

	return 0;

}

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

#include<string.h>

#include<algorithm>

using namespace std;



int main(){

	int a,n=0;

	char s[15],s1[15];

	scanf("%s",s);

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

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

			s1[n++]='0';

		}

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

			s1[n++]='1';

		}

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

			if(n>0){

				s1[--n]='\0';

			}

		}

		

	}

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

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

		}

	

	printf("\n");

	return 0;

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

using namespace std;

int main()

{

    int x,y;

    cin>>x>>y;

    if(x+y<10) cout<<x+y<<endl;

    else cout <<"error"<<endl;

    return 0;

}


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



int main(){

	

	int a,b;

	

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

	

	if(a+b>=10){

		printf("error\n");

	}

	

	else{

		printf("%d\n",a+b);

	}

	

	return 0;

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

 

using namespace std;

 

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

#define pd(n) printf("%.12lf", n)

#define pf(n) printf("%d", n)

#define p() printf("\n")

#define ps() printf(" ")

#define ll long long

#define ull unsigned long long

#define MAX 1e9

const int mod = 1000 * 1000 * 1000 + 7;

#define ex 1e18

#define dbl 2e09

#define qu(q) queue<int> q

#define pqu(q) priority_queue<int> q

#define len(s) s.length()

#define sz(x) int(x.size())

#define pb(x) push_back(x)

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

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

#define F first

#define S second

#define clr(x) x.clear()

#define LOG(x) std::cout << x << std::endl;

 

const int INF = 0x3f3f3f3f;

const ll LL_INF = 0x3f3f3f3f3f3f3f3f;

const double PI = 3.14159265359;

 

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

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



int a[100005];

int dp[100005];

int n,k;

int solve(int idx){

    if(idx==(n-1))

        return 0;

    if(idx>=n)

        return INF;

    int &ret = dp[idx];

    if(~ret)

        return ret;

    ret = INF;

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

        ret=min(ret,solve(idx+i)+abs(a[idx+i]-a[idx]));

    }

    return ret;

}







int main(){



    cin>>n>>k;

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

        cin>>a[i];

    }

    memset(dp,-1,sizeof dp);

    cout<<solve(0)<<endl;

    return 0;

}

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



using namespace std;



#define F first

#define S second



int N, K;

int H[100005];

int dp[100001];



int main() {

	cin >> N >> K; 

	H[0] = INT32_MAX; // to avoid RTE

	for (int i = 1; i <= N; i++) cin >> H[i];

	for (int i = 0; i <= N; i++) dp[i] = INT32_MAX;

	int ans = 0;

	

	dp[N] = 0; // the base case

	K = min(N - 1, K);

	

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

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

			if (i - j < 0) break;

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

		}

	}



	cout << dp[1] << endl;



	return 0;

}