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

#include <string>

#include <vector>

using namespace std;



int main()

{

    string s;

    long long n, z=0, r = 0, g = 0, b = 0;

    cin >> n;

    cin >> s;

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

    {

        if (s[i] == 'R')

        {

            z+=g*b;

            r++;

        }

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

        {

            z+=r*b;

            g++;

        }

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

        {

            z+=r*g;

            b++;

        }

    }

        for (long long o = 3; o-1 < n; o += 2) {

      for (long long i = o-1; i < n; i++){

        if (s[i] != s[i-o+1] && s[i-o+1] != s[i-o/2] && s[i-o/2] !=s[i])

          z--;

        }

    }

    cout<<z<<endl;

    return 0;

}

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

#include<algorithm>



using namespace std;



typedef long long LL;



const int N = 4010;



int r[N], g[N], b[N];

char s[N];



int main()

{

    int n;

    scanf("%d", &n);

    scanf("%s", s + 1);

    

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

    {

        r[i] = r[i - 1], g[i] = g[i - 1], b[i] = b[i - 1];

        if(s[i] == 'R')  r[i] ++;

        else if(s[i] == 'G')  g[i] ++;

        else  b[i] ++;

    }

    

    LL res = 0;

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

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

            if(s[i] != s[j])

            {

                char c = (int)'R' + 'G' + 'B' - s[i] - s[j];

                if(c == 'R')  res += r[i - 1];

                else if(c == 'G')  res += g[i - 1];

                else  res += b[i - 1];

                if(i - (j - i) >= 1 and s[i - (j - i)] == c)  res --;

            }

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

    return 0;

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

#include<cmath>

#include<cstdio>

#include<cstring>

using namespace std;

int n,m,f[1100000],w[1100000],k;

int main ()

{

	cin>>n>>k;

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

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

	{

		cin>>w[i];

	}

	f[1]=0;

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

	{

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

		{

			if(i-j<=0) break;

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

		}

	}	

	cout<<f[n];

}

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

#include<iostream>

#include<string.h>

#include<algorithm>

using namespace std;

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

int xg(int a){

	if(a>0)

		return a;

	return -a;

}

int main(){

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

	a[0]=0;

	cin>>n>>k;

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

		cin>>dp[i];

	}

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

		a[i]=xg(dp[i]-dp[0]);

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

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

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

	}

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

//		cout<<a[i]<<' ';

	if(a[n-1]==0x3f3f3f3f)

		cout<<0<<endl;

	else

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

	return 0;

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

using namespace std;

typedef long long ll;

ll cost[100002], n, k;

ll dp[100002];

ll jump(ll pos)

{

    if(dp[pos]!=-1)

    return dp[pos];

    if(pos == n)

    return 0;

    if(pos > n)

    return INT_MAX;

    ll ans = 0, prev = INT_MAX;

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

            ans = (abs(cost[pos] - cost[pos+i]) +  jump(pos+i));

            prev = min(prev, ans);

        }   

    return dp[pos] = prev;

}







int main(){

    cin >> n >> k;

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

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

        cin >> cost[i];

    ll ans = jump(1);

    cout << ans << endl;

return 0;

}

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

using namespace std;

typedef long long ll;

typedef double dd;

const int INF = 1e9 + 5;

void solve()

{

		int n;

		cin >> n;

		vector <int> h(n, 0);

		int k;

		cin >> k;

		for(int &x : h)

			cin >> x;

			

		vector <int> dp(n, INF);

		dp[0] = 0;

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

		{

			for(int 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];

		

}



int main()

{

		ios::sync_with_stdio(false);

		cin.tie(nullptr);

		ll t = 1;

	//	cin >> t

		while(t--)

		{

			solve();

		}

	return 0;

}
// Slower C++ solution:
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

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

typedef long long ll;

using namespace std;

ll n , k ,arr[100005] , memo[100005];

ll solve(ll i)

{

	if(i == n - 1)

		return 0;

	ll &ans = memo[i];

	if(~ans)

		return ans;

	ans = 1e9;

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

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

	return ans;

}

int main()

{

	abdelrahman010

	cin >> n >> k;

	memset(memo , -1 , sizeof memo);

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

		cin >> arr[i];

	cout << solve(0);

	return 0;

}


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



#define ll int64

#define pb push_back

#define ff first

#define ss second

#define sz size()

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

#define allr(a) a.rbegin(), a.rend()

#define mod 1000000007

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

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

     

using namespace std;



const int maxn=110000;

int n,h[maxn],f[maxn],k;

int main(){

    IO;    

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

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

	memset(f,0x3f,sizeof(f)),f[1]=0;

	for(int i=2;i<=n;i++) for(int j=1;j<i&&j<=k;j++) f[i]=min(f[i],f[i-j]+abs(h[i]-h[i-j]));

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

    return 0;

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

using namespace std;

#define ALL(x) begin(x),end(x)

typedef long long ll;

int OO = 0x3f3f3f3f;



int n, k;

int arr[100005];

int mem[100005];



int solve(int x)

{

    if(x == n-1)

        return 0;



    if(mem[x] != OO)

        return mem[x];



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

    {

        mem[x] = min(mem[x], solve(x+i)+abs(arr[x] - arr[x+i]));

    }



    return mem[x];

}



int main()

{

    cin >> n >> k;

    memset(mem, OO, sizeof mem);

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

    {

        cin >> arr[i];

    }

    cout << solve(0) << endl;

    return 0;

}


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

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



using namespace std;



typedef long long ll;

const double eps=1e-6,pi=acos(-1);

const int N=1e5+5,M=1e9+7,SEGM=4*N,OO=0x3f3f3f3f;

int n,m,a[N],b[N],c[N];

int mem[N];



int solve(int i=0){

    if(i>=n) return 1e9;

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

    int &ret=mem[i];

    if(~ret) return ret;

    ret=1e9;

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

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

    return ret;

}



int main(){

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

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

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

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

    memset(mem,-1,sizeof mem);

    printf("%d",solve());

    return 0;

}


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

using namespace std;

using ll = long long;

using ii = pair<int, int>;

using iii = pair<ii, int>;

#define vt vector

#define pq priority_queue

#define pb push_back

#define pf push_front

#define mp make_pair

#define fi first

#define se second

#define __lcm(a, b) a * b / __gcd(a, b)

const int MOD = 1e9 + 7;

const int INF = 2e9;

const int N = 1e5 + 5;



int n, k;

int a[N], dp[N];



void getDP() {

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

        int &sol = dp[idx];

        if(idx == 1) sol = 0;

        else {

            sol = INF;

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

                if(idx >= i + 1) 

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

            }

        }

    }

}



void solve() {

    cin >> n >> k;

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

        cin >> a[i];

    }

    getDP();

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

}



int main() {

 

    ios_base::sync_with_stdio(0);

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

    

    solve();

    

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

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

const ll INF = 1 << 30;

typedef pair<ll, ll  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<ll,payar  > > vpp;

vector < payar >  vp;



ll n ;

int ara[200005];



ll dp[200005];



ll par[100005];



ll k ;

ll cal(int pos)

{

      if(pos>n)

    {

        return 10000000000000000;

    }

    if(dp[pos]!=-1)

    {

        return dp[pos];

    }

    if(pos==n )

    {

        return 0 ;

    }







    dp[pos]=abs(ara[pos+1]-ara[pos]) +cal(pos+1);



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

    {



            dp[pos]=min( abs(ara[pos]-ara[pos+i])+ cal(pos+i), dp[pos] );



    }

    return dp[pos];

}

int main()

{



    cin>>n>>k;

    memset(dp,-1,sizeof dp);

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

    {

        //  cin>>ara[i];

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

    }

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

    {

        ara[i]=10000000000000000;

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

    }

    cout<<cal(1);







    return  0 ;

}


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

using namespace std;





int main()

{

    int n,k;

    cin>>n>>k;



    int ar[n+10],ans[n+10];



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

    {

        cin>>ar[i];

        ans[i]=INT_MAX;

    }



    ans[0]=0;





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

    {

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

        {

            int r1,r2;



            r1=abs(ar[j]-ar[i])+ans[i];

            ans[j]=min(r1,ans[j]);

        }



    }



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



    return 0;

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

#include<string>

using namespace std;

int main(){

	string s;

	int k;

	cin>>k>>s;

	if(s.length()>k){

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

			cout<<s[i];

		}

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

	}

	else cout<<s<<endl;

}

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

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

using namespace std;

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

#define per(i,a,n) for(int i=n;i>=a;i--)

#define pb push_back

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

typedef long long ll;

typedef pair<int,int> pii;

typedef double db;

ll n,ans,r,g,b;

char s[4010];

int main(){

	cin>>n;

	scanf("%s",s+1);

	rep(i,1,n){

		if(s[i]=='R')	r++;

		if(s[i]=='G')	g++;

		if(s[i]=='B')	b++;

	}

	ans=r*g*b;

	rep(len,1,n){

		rep(j,1,n-2*len){

			if(s[j]!=s[j+len]&&s[j]!=s[j+2*len]&&s[j+len]!=s[j+2*len])

				ans--;	

		}

	}

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

	return 0;

}


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

#include<algorithm>



using namespace std;



typedef long long LL;



const int N = 4010;



int r[N], g[N], b[N];

char s[N];



int main()

{

    int n;

    scanf("%d", &n);

    scanf("%s", s + 1);

    

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

    {

        r[i] = r[i - 1], g[i] = g[i - 1], b[i] = b[i - 1];

        if(s[i] == 'R')  r[i] ++;

        else if(s[i] == 'G')  g[i] ++;

        else  b[i] ++;

    }

    

    LL res = 0;

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

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

            if(s[i] != s[j])

            {

                char c = (int)'R' + 'G' + 'B' - s[i] - s[j];

                if(c == 'R')  res += r[i - 1];

                else if(c == 'G')  res += g[i - 1];

                else  res += b[i - 1];

                if(i - (j - i) >= 1 and s[i - (j - i)] == c)  res --;

            }

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

    return 0;

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



using namespace std;



int main(){

    int N, len;

    cin>>N;

    string s;

    cin>>s;

    len = s.size();

    if (len > N){

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

            s.pop_back();

        }

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

            s.push_back('.');

        }

        cout<<s;

    }

    else {

        cout<<s;

    }



    return 0;

}


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

string s;

int main() {

	scanf("%d",&n);

	cin>>s;

	if((s.size())>n) {

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

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

		printf("...");

	}

	else

	    cout<<s;

}

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

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

using namespace std;

int n ,k;

vector<int>vi;

long long mem[200005];

bool vis[200005];

long long solve(int indx ){

	if(indx == n-1)

		return 0;

	if(vis[indx])

		return mem[indx];

	vis[indx] = true;

	mem[indx] = 1e13;

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

		mem[indx] = min(mem[indx] , solve(i ) + abs(vi[indx] - vi[i]));

	}

	return mem[indx];

}

//long long n , w;

//vector<pair<long long , long long>>vi;

//long long mem[105][100005];

//bool vis[105][100005];

//long long ans = 0;

//long long sol(int indx , int cost){

//	if(indx == n)

//		return 0;

//	if(vis[indx][cost])return mem[indx][cost];

//	mem[indx][cost] = 0;

//	long long fch = sol(indx + 1 , cost);

//	long long sch = 0;

//	if(mem[indx][cost] + vi[indx].first <= w){

//		sch = sol(indx + 1 , cost + vi[indx].second) + vi[indx].first;

//	}

//	return mem[indx][cost] = max()

//}

int main()

{

//	cin>>n>>w;

//	vi.resize(n);

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

//		cin>>vi[i].first>>vi[i].second;

//

//	cout<<sol(0,0);



	cin>>n>>k;

	vi.resize(n);

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

		cin>>vi[i];

	cout<<solve(0);

	return 0;

}




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

#include<queue>

#include<cstdio>

#include<vector>

#include<cstring>

#include<iostream>

#include<algorithm>

#define RG register

#define LL long long

#define fre(a) freopen(a".in","r",stdin);//freopen(a".out","w",stdout);

using namespace std;

const int MAXN=300000;

int n,m;

int X[MAXN],Y[MAXN],fa[MAXN];

double ans,ALL;

struct ed

{

   int from,to;

   double len;

}edge[MAXN];

bool comp(ed x,ed y){ return x.len>y.len; }

int find(int x)

{

   if(x!=fa[x])

      fa[x]=find(fa[x]);

   return fa[x];

}

void Union(int x,int y)

{

   x=find(x);

   y=find(y);

   fa[x]=y;

}

void work()

{

   int cnt=0;

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

      {

	 int A=edge[i].from,B=edge[i].to;

	 if(find(A)!=find(B))

	    {

	       Union(A,B);

	       ans+=edge[i].len;

	       cnt++;

	    }

	 if(cnt>=n-1)break;

      }

}

int main()

{

   //fre("1");

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

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

      {

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

	 fa[i]=i;

      }

   for(int i=1,a,b;i<=m;i++)

      {

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

	 edge[i].from=a;

	 edge[i].to=b;

	 edge[i].len=sqrt((X[a]-X[b])*(X[a]-X[b])+(Y[a]-Y[b])*(Y[a]-Y[b]));

	 ALL+=edge[i].len;

      }

   sort(edge+1,edge+1+m,comp);

   work();

   ans=ALL-ans;

   printf("%.3lf\n",ans);

   return 0;

}

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

#include <cstring>

#include <algorithm>

#include <cmath>



using namespace std;



int n, m, f[10010], x[10010], y[10010];

double ans = 0.0;



struct edge {

	int u, v;

	double w;

	bool operator < (const edge &b) const { return w < b.w; }

}e[1000010];



double getdis(int u, int v) {

	return sqrt((double)(x[u] - x[v]) * (x[u] - x[v]) + (double)(y[u] - y[v]) * (y[u] - y[v]));

}



int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }



int main() {

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

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

		f[i] = i;

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

	}

	int u, v;

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

		scanf("%d%d", &u, &v);

		e[i].u = u;

		e[i].v = v;

		e[i].w = getdis(u, v);

	}

	sort(e + 1, e + m + 1);

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

		int fu = find(e[i].u);

		int fv = find(e[i].v);

		if(fu != fv) f[fu] = fv;

		else ans += e[i].w;

	}

	printf("%.3lf\n", ans);

	return 0;

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

using namespace std;

typedef long long ll;

ll dp[100009];

int arr[100009];

ll sum = 0;

int k;

long long Frog1(int n){

 

    if(n==0) return 0;

 

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

    else {

            ll sum = INT_MAX;

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

 

                ll x;

                if(dp[i]!=-1) x = dp[i];

                else x = Frog1(i);

 

                x = abs(arr[i] - arr[n]) + x;

                sum = min(sum,x);

                //cout << n  << " " << i  << " " << j << endl;

            }

            dp[n] = sum;

            return sum;

    }

}

 

 

int main()

{

    int n;

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

    cin >> n >> k;

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

    cout << Frog1(n-1) << endl;

}

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

using namespace std;

#define ll long long

#define ld long double

#define sf scanf

#define pf printf

#define pb push_back

#define llu unsigned long long

#define U unsigned int

#define SIZE 10000000

#define pie 3.14159265358979323

#define minuss 1e-6

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



#define BIG 100000000000000



ll  lcm(ll a,ll b)

{

    ll GCD=__gcd(a,b);

    ll lcm=(a*b)/GCD;

    return lcm;

}

ll dp[1000005];

ll weight[1000005];

ll total,val;

int main()

{

    cin>>total>>val;

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

        cin>>weight[i];



    weight[0]=SIZE;

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

    {

        dp[i]=BIG;

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

            if(i-j>=1)

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



    }

    cout<<dp[total]<<endl;

    return 0;

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

#include <vector>

#include <algorithm>

#include <string.h>

using namespace std;

vector <int> x ;

int n , dp[100003] , k ;

int SAAD ( int idx ) {

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

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

	int res = 1e9 ;

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

		if (idx+i<n) res = min(res,SAAD(idx+i)+abs(x[idx]-x[idx+i]));

	}

	return dp[idx] = res ;

}

int main() {

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

	int p ;

	cin >> n >> k ;

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

		cin >> p; 

		x.push_back(p);

	}

	cout << SAAD(0) ;

	return 0;

}

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

#include <algorithm>

#include <math.h>

using namespace std;

int dp[int(1e5+2)] , x[int(1e5+2)] ;

int main() {

	int n , k ;

	cin >> n >> k ;

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

		cin >> x[i] ;

		if ( i == 0 ) continue ;

		int mn = 1e9 ;

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

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

		}

		dp[i] = mn ;

	}

	cout << dp[n-1] ;

	return 0;

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



using namespace std;



const int N = 1e5 + 9, OO = 0x3f3f3f3f;



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



int solve(int i){

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

    if(i >= n)   return 1e9;



    int &ret = dp[i];

    if(~ret)    return ret;



    ret = OO;

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

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



    return ret;

}



int main(){



    cin >> n >> k;

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

        cin >> h[i];



    memset(dp, -1, sizeof dp);



    cout << solve(0);

}


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

}