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

using namespace std;

int a[1000010],n,r,s1,s2;

int main(){

	scanf("%d",&n);

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

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

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

		s1=a[0],s2=0;

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

			if(a[j]>s1){

				s2=s1;

				s1=a[j];

			}

			else if(a[j]>s2)

				s2=a[j];

		}

		r=max(r,s1+s2);

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

	}

}

// Optimized solution to the same problem (faster):
#include <algorithm>

#include <cstdio>

#include <cstring>



#define int long long



const int Maxv = 300010; 



int ans[Maxv], max[Maxv], num, n; 



signed main() {

    scanf("%lld", &n); 

    

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

        scanf("%lld", &num); 

    

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

            ans[j] = std::max(ans[j], max[j] + num); 

            max[j] = std::max(max[j], num); 

        }

    }

       

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

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

        ans[i + 1] = std::max(ans[i + 1], ans[i]); 

    }

    

    return 0; 

}

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

	#define mx 300005

	using namespace std;



	int a[mx], n;

	vector<vector<bool> > vis(mx, vector<bool> (20, false));

	vector<vector<pair<int,int> > > memo(mx, vector<pair<int, int> > (20, {0, 0}));



	void dp(int num, int pos) {

		if(pos == 0) {

			memo[num][0].first = a[num];

			vis[num][0] = true;

			return;

		}

		if(vis[num][pos]) return;

 		vector<int> cons;

		dp(num, pos-1);

		cons.push_back(memo[num][pos-1].first);

		cons.push_back(memo[num][pos-1].second);

		if(num & (1<<(pos-1))) {

			dp(num ^ (1<<(pos-1)), pos-1);

			cons.push_back(memo[num ^ (1<<(pos-1))][pos-1].first);

			cons.push_back(memo[num ^ (1<<(pos-1))][pos-1].second);

		}

		sort(cons.begin(), cons.end(), greater<int>());

		memo[num][pos].first = cons[0];

		memo[num][pos].second = cons[1];

		vis[num][pos] = true;

	}



	int main() {

		ios_base::sync_with_stdio(false);

	    cin.tie(NULL);

	    cout.tie(NULL);



		cin >> n;

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

			cin >> a[i];

			

		}

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

			if(!vis[i][n]) {

				dp(i, n);

			}

		}

		int mxv = 0;

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

			mxv = max(memo[i][n].first + memo[i][n].second, mxv);

			cout << mxv << "\n";	

		}

		return 0;

	}
// Original C++ solution (slower):




#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pii pair<int, int>

#define ff first

#define ss second



int main() {

    ll n, ai, bi, ci, sum = 0, q;

    int arra[100002];

    memset(arra, 0, sizeof(arra));

    cin >> n;

    while(n--){

        cin >> ai;

        arra[ai]++;

        sum += ai;

    }

    cin >> q;

    while(q--){

        cin >> bi >> ci;

        sum = sum - (bi*arra[bi]) + (ci*arra[bi]);

        arra[ci] += arra[bi];

        arra[bi] = 0;

        cout << sum << '\n';

    }

    return 0;

}

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

#define rush() int T;scanf("%d",&T);int kase=1;while(T--)

#define pb(x) push_back(x)

#define pr pair<int,int>

#define mem(a) memset(a,0,sizeof(a))

#define  fi first

#define  se second

using namespace std;

typedef long long ll;

const ll maxn=2e6+5;

const ll mod=1e9+7;

ll quickpow(ll x,ll y,ll mod){ll ans=1;while(y){if(y&1)ans=ans*x%mod;x=x*x%mod;y>>=1;}return ans;}



int a[maxn]={0};



int main()

{

    int n;

    cin>>n;

    ll sum=0;

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

    {

        int x;

        cin>>x;

        sum+=x;

        a[x]++;

    }

    int q;

    cin>>q;

    while(q--)

    {

        int b,c;

        cin>>b>>c;

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

        a[c]+=a[b];

        a[b]=0;

        cout<<sum<<endl;

    }

    return 0;

}


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

using namespace std;

#define ll long long

#include <string>

const int mxN=1e5;

const int maxN=5e3;

#define ld long double

#define pb push_back

#define mp make_pair

#define ins insert

#define vi vector<int>



int main()

{

    int n;

    cin >> n;

    int cnt[100000]={};

    ll s=0;

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

    {

        int x;

        cin >> x;

        cnt[x-1]++;

        s+=x;

    }

    int q;

    cin >> q;

    while(q--)

    {

        int b,c;

        cin >> b >> c;

        s+=cnt[b-1]*(c-b);

        cnt[c-1]+=cnt[b-1];

        cnt[b-1]=0;

        cout << s << "\n";

    }

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



using namespace std;



#define ff first

#define ss second

#define int long long

#define pii pair<int,int>

#define mp make_pair



int memo[100000],v[100000],k;



int calcpulo(int id){

	if(memo[id]==INT_MAX){

		int menor,i,calc,aux;

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

			calc=id-k+i;

			if(calc>=0){

				menor=calcpulo(calc);

				aux=abs(v[id]-v[calc])+menor;

				if(memo[id]>aux)memo[id]=aux;

			}		

		}

	}

	return memo[id];	

}



signed main(){

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	int n,i;

	cin>>n>>k;

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

	memo[0]=0;

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

		memo[i]=INT_MAX;

	}

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

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#define inf 2000000000

int n,k;

int h[100002]={100000},f[100002]={};

int min(int a,int b)

{

	return a<b? a:b;

}

int max(int a,int b)

{

	return a>b? a:b;

}

int abs(int x)

{

	return x<0? -x:x;

}

int main()

{

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

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

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

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

	{

		f[i]=inf;

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

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

	}

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

	return 0;

}

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>

#include <numeric>

#include <cmath>

#include <set>

#include <tuple>

#include <functional>

#include <map>

#include <unordered_map>

#include <queue>

#include <tuple>



using namespace std;



#define endl '\n'



using ull = unsigned long int;

using ll = long long;



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);

  const int precision = 13;

  cout.precision(precision);

  cout.setf(ios_base::fixed);



  int n, k; cin >> n >> k;

  vector<ll> s(k+1), dp(k+1, 1e9 + 10);

  ++k;

  dp[1] = 0;

  cin >> s[1];

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

    cin >> s[i%k];

    dp[i%k] = dp[(i-1)%k] + abs(s[i%k] - s[(i-1)%k]);

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

      dp[i%k] = min(dp[(i-l)%k] + abs(s[(i-l)%k] - s[i%k]), dp[i%k]);

    }

  }



  cout << dp[n%k] << endl;



  return 0;

}


// Optimized solution to the same problem (faster):
#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;

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<queue>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int maxn = 1e5+5;



int a[maxn],dp[maxn];



int main(){

	int n,k;

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

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

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

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

			if(i - j > 0){

				if(i > k){

					if(j == k) dp[i] = dp[i-j] + abs(a[i] - a[i-j]);

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

				}

				else{

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

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

				}

			}

		}

	}

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

}

// Optimized solution to the same problem (faster):
#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;

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

typedef int int_;

#define int long long

#define maxn 200010

const int inf=1e9+7;

inline int read(){

	int ret=0,ff=1;

	char ch=getchar();

	while(ch<'0'||ch>'9'){

		if(ch=='-') ff=-ff;

		ch=getchar();

	}

	while(ch>='0'&&ch<='9'){

		ret=ret*10+ch-'0';

		ch=getchar();

	}

	return ret*ff;

}



int gcd(int a,int b){

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

}

int ksm(int a,int b,int p){

	int ret=1;

	while(b){

		if(b&1) ret=ret*a%p;

		a=a*a%p;

		b>>=1;

	}

	return ret%p;

}

int a[maxn],num[maxn];

int_ main(){

	memset(num,0,sizeof(num));

	int n=read();

	int ans=0;

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

		a[i]=read();

		ans+=a[i];

		++num[a[i]];

	}

	

	int q=read();

	while(q--){

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

		int x=read(),y=read();

		ans+=(y-x)*num[x];

		num[y]+=num[x];

		num[x]=0;

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

	}

	return 0;

} 

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

using namespace std;

typedef long long ll;

map<ll,ll>mp;



int main(){

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

    ll sum=0;

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

        ll num;

        scanf("%lld",&num);

        mp[num]++;

        sum+=num;

    }

    ll q;scanf("%lld",&q);

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

        ll b,c;scanf("%lld%lld",&b,&c);

        mp[c]+=mp[b];

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

        mp[b]=0;

        cout<<sum<<endl;

    }

    return 0;

}

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

using namespace std;

#define ll long long

#include <string>

const int mxN=1e5;

const int maxN=5e3;

#define ld long double

#define pb push_back

#define mp make_pair

#define ins insert

#define vi vector<int>



int main()

{

    int n;

    cin >> n;

    int cnt[100000]={};

    ll s=0;

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

    {

        int x;

        cin >> x;

        cnt[x-1]++;

        s+=x;

    }

    int q;

    cin >> q;

    while(q--)

    {

        int b,c;

        cin >> b >> c;

        s+=cnt[b-1]*(c-b);

        cnt[c-1]+=cnt[b-1];

        cnt[b-1]=0;

        cout << s << "\n";

    }

}
// Original C++ solution (slower):
#include<iostream>

#include<cstring>

#include<algorithm>

#include<cstdio>

using namespace std;

#define MAXN 100000 + 10

#define ll long long int

ll N[MAXN];

ll dp[MAXN];

#define INF 0x3f3f3f3f;

int main()

{

    ll n,k;

    cin>>n>>k;

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

        cin>>N[i],dp[i] = INF;

    dp[0] = 0;

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

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

            if(j >= 0)

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

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

    return 0;

}

//

// Optimized solution to the same problem (faster):
#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;

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

using namespace std;

#define mod 1000000007



int main()

{

    int n;



    scanf("%d", &n);

    long long s = 1;

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

        s = s * i % mod;

    }

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

}


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

#include <algorithm>

using namespace std;



int main()

{

	int n,i;

	long long sum=1;

	scanf("%d",&n);

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

	{

		sum*=i;

		sum%=(1000000000+7);

	}

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

	return 0;

}


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

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

using namespace std;

using ll = long long;

using P = pair<ll, ll>;

const string ln = "\n";

constexpr int INF = 1001001001;

constexpr int MOD = 1000000007;



int main() {

  cin.tie(nullptr);

  ios::sync_with_stdio(false);



  int n;

  cin >> n;



  ll ans = 1;



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

    ans *= i % MOD;

    ans %= MOD;

  }



  cout << ans << ln;



  return 0;

}
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<algorithm>

#include<stack>

#include<queue>

#include<vector>

#include<set>

#include<string>

#include<cmath>

#include<map>

#include<bitset>

#include<utility>

//#include<unordered_map>

#ifdef LOCAL

#include"file.h"

#endif



using namespace std;



typedef long long LL;

typedef unsigned long long ULL;

const double eps = 1e-12;

const double Pi = 3.1415926535897932;

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

//const int dir[8][2] = { -2,-1,-2,1,2,-1,2,1,-1,2,-1,-2,1,2,1,-2 };

















const int maxn = 2e5 + 5;









int f[maxn];

int h[maxn];



int n, k;



int main() {

#ifdef LOCAL

	fileopen();

#endif



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

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

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



		memset(f, 63, sizeof(f));

		f[1] = 0;



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

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

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

			}

		}

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

	}





#ifdef LOCAL

	fileclose();

#endif

	return 0;

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstring>

#include<algorithm>

#include<cstdio>

using namespace std;

#define MAXN 100000 + 10

#define ll long long int

ll N[MAXN];

ll dp[MAXN];

#define INF 0x3f3f3f3f;

int main()

{

    ll n,k;

    cin>>n>>k;

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

        cin>>N[i],dp[i] = INF;

    dp[0] = 0;

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

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

            if(j >= 0)

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

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

    return 0;

}

//

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}
// Original C++ solution (slower):
//Inbornhandsome

#include <bits/stdc++.h>

using namespace std;

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

#define FIO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)

#define endl '\n'

#define int long long



const int N = 1e5 + 7;



int stones[N], cost[N], n, k;



int find_cost(int stone)

{

    if(stone > n)   return 1e9;

    if(stone == n)  return 0;



    int &ans = cost[stone];



    if(ans != -1) return ans;



    ans = 1e10;



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

    {   

        ans = min(ans, abs(stones[stone] - stones[stone + i]) + find_cost(stone + i));

    }

    

    

    return ans;

}



int32_t main()

{

    IOS;// FIO;



    cin >> n >> k;



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



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

        cin  >> stones[i];



    cout << find_cost(1);



    return 0;

}

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

using namespace std;



#define endl '\n'

#define int long long



const int N = 1e5+9;

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



int32_t main()

{

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



	cin >> n >> k;

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

		cin >> h[i];

	}

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

		cache[i] = 1e9;

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

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

		}

	}

	cout << cache[1] << endl;



	return 0;

}

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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





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

using namespace std;

#define ll long long

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

#define here(x) cout << " #x = " << x << endl;

#define show(x) {for( auto ii : x) cout << ii << en; cout << endl;}

#define shop(n,x) {for(int i = 0; i < n; i++) cout << x << en; cout << endl;}

#define ff first

#define ss second

#define sq(x) (x*x)

#define endl '\n'

#define en " "

#define pb push_back

#define mpair make_pair

ll mod=1000000007;

double pi=3.1415926536;

ll n, k;

ll dp[200003];

ll cost(ll a[], ll idx)

{

	if(dp[idx] != -1)

		return dp[idx];



	if(idx >= n-2)

		return abs(a[n-1] - a[idx]);

	

	ll m = mod, p = mod;

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

	{

		if(idx + i >= n)

			break; 

		p = abs(a[idx + i] - a[idx]) + cost(a, idx + i);

		m = min(m, p);

	}

	return dp[idx] = m;

}

int main()

{

	fastio();

	int tt = 1;

	//cin >> tt;

	while(tt--)

	{

		// go fast 		check		//		CHECK -> if()   else if()

		

		ll x = 0, m = 0, y = 0, d = 1;

		cin >> n >> k;

		ll a[n];

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

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

			cin >> a[i];



		cout << cost(a, 0) << endl;

	}

}

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

using namespace std;

#define ll long long

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

#define here(x) cout << " #x = " << x << endl;

#define show(x) {for( auto ii : x) cout << ii << en; cout << endl;}

#define shop(n,x) {for(int i = 0; i < n; i++) cout << x << en; cout << endl;}

#define ff first

#define ss second

#define sq(x) (x*x)

#define endl '\n'

#define en " "

#define pb push_back

#define mpair make_pair

ll mod=1000000007;

double pi=3.1415926536;

ll n;

// ll dp[200003];

int main()

{

	fastio();

	int tt = 1;

	//cin >> tt;

	while(tt--)

	{

		// go fast 		check		//		CHECK -> if()   else if()

		

		ll x=0, m=0, y=0, d=1, k=mod;

		cin >> n >> k;

		ll a[n], dp[n];

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

			cin >> a[i];



		dp[n - 1] = 0;

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



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

		{

			dp[i] = mod;

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

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

		}

				

		cout << dp[0] << endl;

	}

}


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

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

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

          cin>>h[i];

     }

     LL dp[100001];



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

           dp[i]=INF;

      }

            dp[0]=0;

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







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

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

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



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

          }

     }     



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





}