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



using namespace std;



const int maxn = (1<<18) + 5;

int n;

int a[maxn];

int x[maxn][2];

int num[maxn][2];



int main() {

	cin >> n;

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

		cin >> a[i];

	memset(x, 0, sizeof(x));

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

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

		if (x[i][0] < a[0]) {

			x[i][0] = a[0];

			num[i][0] = 0;

		}

		else if (x[i][1] < a[0]){

			x[i][1] = a[0];

			num[i][1] = 0;

		}

	int last = 0;

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

		if (a[i] > x[i][0]) {

			swap(x[i][0], x[i][1]);

			swap(num[i][0], num[i][1]);

			x[i][0] = a[i];

			num[i][0] = i;

		}

		else if (a[i] > x[i][1]) {

			x[i][1] = a[i];

			num[i][1] = i;

		}

		last = max(last, x[i][0]+x[i][1]);

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

		for (int j = 1; j < (1<<n); j <<= 1) {

			if (x[i|j][0] < x[i][0]) {

				swap(x[i|j][0], x[i|j][1]);

				swap(num[i|j][0], num[i|j][1]);

				x[i|j][0] = x[i][0];

				num[i|j][0] = num[i][0];

				if (x[i|j][1] < x[i][1] && num[i|j][1] != num[i][1]) {

					x[i|j][1] = x[i][1];

					num[i|j][1] = num[i][1];

				}

			}

			else if (x[i|j][1] < x[i][0] && num[i|j][0] != num[i][0]) {

				x[i|j][1] = x[i][0];

				num[i|j][1] = num[i][0];

			}

			else if (x[i|j][1] < x[i][1]) {

				x[i|j][1] = x[i][1];

				num[i|j][1] = num[i][1];

			}

		}

	}

	return 0;

}

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

#include<ext/pb_ds/hash_policy.hpp>

#include<ext/pb_ds/assoc_container.hpp>

#define N 500005

using namespace __gnu_pbds;

using namespace std;

typedef long long LL;

typedef unsigned long long uLL;

struct custom_hash {

    static uint64_t splitmix64(uint64_t x) {

        x += 0x9e3779b97f4a7c15;

        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;

        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;

        return x ^ (x >> 31);

    }

    size_t operator()(uint64_t x) const {

        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();

        return splitmix64(x + FIXED_RANDOM);

    }

};

LL z = 1;

int read(){

	int x, f = 1;

	char ch;

	while(ch = getchar(), ch < '0' || ch > '9') if(ch == '-') f = -1;

	x = ch - '0';

	while(ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - 48;

	return x * f;

}

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

	int s = 1;

	while(b){

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

		a = z * a * a % p;

		b >>= 1;

	}

	return s;

}

int a[N];

int f[N];

struct node{

	int a, b;

}d[N];

void work(node &t, int x){

	if(t.a == x || t.b == x) return;

	if(a[t.a] < a[x]) t.b = t.a, t.a = x;

	else if(a[t.b] < a[x]) t.b = x;

}

int main(){

	int i, j, t, n, m;

	n = read();

	for(i = 0; i < (1 << n); i++) a[i] = read(), d[i].a = i;

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

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

			if(1 << j & i){

				t = i ^ (1 << j);

				work(d[i], d[t].a);

				work(d[i], d[t].b);

			}

		}

		f[i] = a[d[i].a] + a[d[i].b];

	}

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

		f[i] = max(f[i], f[i - 1]);

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

	}

	return 0;

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

using namespace std;

typedef long long ll;

#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

#define VL vector<ll>

#define VS vector<string>

#define VB vector<bool>

#define VP vector<pair<ll,ll>>

#define VVL vector<vector<ll>>

#define VVP vector<vector<pair<ll,ll>>>

#define PL pair<ll,ll>

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

ll d1[4] = {1, -1, 0, 0};

ll d2[4] = {0, 0, 1, -1};



int main(){

    ll N;

    cin >> N;



    VL a(N);

    rep(i, 0, N) cin >> a[i];



    ll L = -1, R = 1e18;

    while(R - L > 1){

        VL tmp(a);

        ll M = L + (R - L)/2;

        ll c = 0;

        auto itr = max_element(ALL(tmp));

        while(*itr >= N){

            ll d = (ll)(*itr/N);

            c += d;

            *itr -= d * (N + 1);

            rep(t, 0, tmp.size()) tmp[t] += d;

            itr = max_element(ALL(tmp));

        }

        if(c <= M) R = M;

        else L = M; 

    }

    cout << R << endl;

    return 0;

}

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

using namespace std;

typedef long long ll;

#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)



int main(){

    ll N;

    cin >> N;

    vector<ll> a(N);

    rep(i, 0, N) cin >> a[i];

    ll ans = 0;

    while(true){

        auto itr = max_element(a.begin(), a.end());

        if(*itr <= N-1) break;

        ll rem = *itr - (N - 1);

        ll red = (rem + N - 1)/N;

        *itr -= red * N;

        *itr -= red;

        ans += red;

        rep(i, 0, N) a[i] += red;

    }

    cout << ans << endl;

    return 0;

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

using namespace std;



int dp[100005];



int main(){

    int n,k,s;

    cin>>n>>k;



    int arr[n];



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

        cin>>arr[i];





    dp[0]=0;

    if(n==1)

        cout<<0<<endl;



    else{



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



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

           s=INT_MAX;

           if(k>i)

           {

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

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



                  if(dp[j]+abs(arr[i]-arr[j])<s)

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

               }

           }



           else{



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



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



                  if(dp[j]+abs(arr[i]-arr[j])<s)

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





               }



           }





        }





    }



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

    return 0;







}


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

#include <algorithm>

#include <vector>

using namespace std;

 

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

int main(){

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

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

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

	}

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

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

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

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

		}

	}

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

	return 0;

}
// Slower C++ solution:
/**

 *   Author: praveen_ojha

 *   Created: 17 January 2019

 *   Time: 11:50:41

**/

#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007

#define PI 3.141592653589793238510

typedef long long ll;

typedef long double ld;

typedef pair<int,int> pii;

typedef vector<int> vi;

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

int solve(int ind){

	if(ind==n-1){

		return 0;

	}

	if(dp[ind]!=-1)

		return dp[ind];

	int mini=INT_MAX;

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

		if(ind+i<n){

		dp[ind+i]=solve(ind+i);

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

		}

	}

	dp[ind]=mini;

	return dp[ind];



}

int main(){

    ios_base::sync_with_stdio(false); 

    cin.tie(NULL);

    memset(dp,-1,sizeof dp);

    cin>>n>>k;

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

    	cin>>a[i];

    cout<<solve(0);

return 0;

}

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

 *   Author: Praveen Ojha

 *   Created: 27 January 2019

 *   Time: 00:59:53

**/

#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007

#define PI 3.141592653589793238510

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

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

typedef long long ll;

typedef long double ld;

typedef pair<int,int> pii;



int main(){

    ios_base::sync_with_stdio(false); 

    cin.tie(NULL);

    int n,k;

    cin>>n>>k;

    int a[n],dp[n];

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

    	cin>>a[i];

    }

    memset(dp,0,sizeof dp);

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

    	int mini=INT_MAX;

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

    		if(i+j<n){

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

    		}

    	}

    	dp[i]=mini;

    }

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

return 0;

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



using namespace std;



const int nax = 1e5+103;

int n, k;

int dp[nax];

int a[nax];

int f(int i){

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

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

	int ans = abs(a[i]-a[i+1])+f(i+1);

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

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

	}

	return dp[i]=ans;

}

int main(){

	cin>>n>>k;

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

	memset(dp, -1, sizeof dp);

	cout<<f(0)<<endl;

	return 0;

}

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

using namespace std;

const int nax = 1e5+103;

int n, k;

int dp[nax];

int a[nax];

int frog2(int i){

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

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

	int ans = abs(a[i]-a[i+1])+frog2(i+1);

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

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

	}

	return dp[i]=ans;

}

int main(){

	cin>>n>>k;

	for(int i=0; i<n; i++) {cin>>a[i]; dp[i]=-1;}



	cout<<frog2(0)<<endl;

	return 0;

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

 

typedef long long ll;

#define int ll

#define fi first

#define se second

#define SORT(a) sort(a.begin(),a.end())

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

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

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

#define pb(a) push_back(a)

#define INF LLONG_MAX/2

 

using namespace std;

int MOD = 1000000007;

 

signed main(){



    int n,k;

    cin >> n >> k;

    int h[100010] = {0};

    rep(i,n)cin >> h[i];



    int dp[100010] = {0};



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

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

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

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

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

        }

    }



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

    



    return 0;   

}



// g++ -std=c++14 code1.cpp

// rm -r -f test;oj dl https://dp.contest.atcoder.jp/tasks/dp_a

// rm -r -f test;oj dl http://yahoo-procon2017-qual.contest.atcoder.jp/tasks/yahoo_procon2017_qual_c

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

 

using namespace std;

typedef long long ll;

typedef vector<ll> vll;

#define int ll

#define fi first

#define se second

#define SORT(a) sort(a.begin(),a.end())

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

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

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

#define pb(a) push_back(a)

#define INF LLONG_MAX/2

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

#define debug(x) cerr<<#x<<": "<<x<<endl

#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl

//----------------------------------------------------------------



// int MOD = 998244353;

int MOD = 1000000007;





//----------------------------------------------------------------

signed main(){



	ll n,k;

	cin >> n >> k;

	vll h(n+k+1);

	rep(i,n)cin >> h[i];



	vll c(n+k+1,INF);

	c[0] = 0;

	rep(i,n){

		rep(j,k){

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

		}

	}

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



	return 0;

}



//----------------------------------------------------------------



// g++ -std=c++14 code1.cpp

// rm -r -f test;oj dl https://code-festival-2018-quala.contest.atcoder.jp/tasks/code_festival_2018_quala_c

// rm -r -f test;oj dl https://dp.contest.atcoder.jp/tasks/dp_a

// rm -r -f test;oj dl http://arc092.contest.atcoder.jp/tasks/arc092_b
// Slower C++ solution:
#include <iostream>

#include <algorithm>

using namespace std;

static const int LEFT = -1000000;

static const int RIGHT = 1000000;

int main() {

    int K, X;

    cin >> K >> X;

    for (int i = max(LEFT, X - K + 1); i <= min(RIGHT, X + K - 1); i++)

        cout << i << " ";

}

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

static const int left = -1000000;

static const int right = 1000000;

inline int max(int a, int b) {

    return a > b ? a : b;

}

inline int min(int a, int b) {

    return a < b ? a : b;

}

int main() {

    int K, X;

    scanf("%d %d", &K, &X);

    const int limit = min(right, X + K - 1);

    for (int i = max(left, X - K + 1); i <= limit; i++) printf("%d ", i);

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

#include <algorithm>

using std::min;

 

int m_abs(int z)

{

    if (z<0)

        return -z;

    else

        return z;

}

 

int main(void)

{

    int *a,*dp,N,i,j,K;

    scanf("%d%d",&N,&K);

    a=new int [N];

    dp=new int [N];

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

    {

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

    }

    dp[N-1]=0;

    if(K>N)

        K=N;

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

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

 

    for(i=N-1-K;i>=0;i--)

    {

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

        for(j=2;j<=K;j++)

        {

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

        }

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

 

    }

 

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

    delete [] dp;

    delete [] a;

 

    return 0;

}

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

using namespace std;

#define MAX(a,b) (a>b?a:b)

#define MOD(a) (a>0?a:-(a))

#define custoate(i)

int main(){

  int npedras, maxpulo;

  scanf("%d %d",&npedras,&maxpulo);

  int hpedra[npedras],custoate[npedras];

  for (size_t i = 0; i < npedras; i++)

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

  custoate[0] = 0;

  int tempcost = 2147483647,k,tempcost2;

  for (size_t i = 1; i < npedras; i++,tempcost = 2147483647) {

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

    for (k= (maxpulo>i ? i : maxpulo); k > 0; k--) {

      // printf("k = %d ",k);

      tempcost2 = custoate[i-k] + MOD(hpedra[i-k]-hpedra[i]);

      if (tempcost > tempcost2)

        tempcost = tempcost2;

    }

    // printf("custo[%d] = %d",i,tempcost);

    custoate[i] = tempcost;

    // printf("\n\n");

  }

  printf("%d\n", custoate[npedras-1]);



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

#include<cstdio>

#include<cmath>

using namespace std;

int main()

{

	int n,i,x=0;

	double k,a;

	cin>>n>>k;

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

	{

		cin>>a;

		if(a>=k)

		x++;

	}

	cout<<x;

	return 0;

}

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

int main()

{

	int a,n,k,h[100000],cnt=0;

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

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

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

		if(h[a]>=k){

			cnt++;

		}

	}

	printf("%d",cnt);

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

#include <cstring>

using namespace std;

int dp[100000];

int n,a[100000],k;



int topdown()

{

	dp[n-1]=0;



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

	{

		dp[i]=1e9;

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

		{

          	if(i+j<n)

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

		}

	}

	return dp[0];

}



int main()

{

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

	cin>>n>>k;

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

		cin>>a[i];



	cout<<topdown();



	return 0;

}


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

#include <cstring>

using namespace std;

int dp[100000];

int n,a[100000],k;



int topdown()

{

	dp[n-1]=0;



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

	{

		dp[i]=1e9;

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

		{

          	if(i+j<n)

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

		}

	}

	return dp[0];

}



int main()

{

  	ios_base::sync_with_stdio(0);            //fast input and output

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>k;

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

		cin>>a[i];



	cout<<topdown();



	return 0;

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

#include <string>

#include <cstring>

#include <stdlib.h>

#include <math.h>

#include <algorithm>

#include <vector>

#include <set>

#include <map>

#include <queue>

#include <list>

#include <iterator>

#include <assert.h>

#pragma warning(disable:4996) 



typedef long long ll;

#define MIN(a, b) ((a)>(b)? (b): (a))

#define MAX(a, b) ((a)<(b)? (b): (a))

#define LINF 9223300000000000000

#define INF 2140000000

const long long MOD = 1000000007;

using namespace std;





ll a[16][16];



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

{

    int n;

    scanf("%d", &n);



    int i,j;

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

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

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

        }

    }



    vector<ll> dp(1<<n);

    int k;

    for(k=0; k<(1<<n); k++) {

        vector<int> z;

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

            if(k & (1<<i)) z.push_back(i); 

        }

        int siz=(int)z.size();



        dp[k]=0;

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

            for(j=i+1; j<siz; j++) {

                dp[k] += a[z[i]][z[j]];

            }

        }



        ll max=0;

        for(i=0; i<(1<<siz); i++) {

            int tmp0=0, tmp1=0;

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

                if(i & (1<<j)) tmp0 |= (1<<z[j]);

                else           tmp1 |= (1<<z[j]);

            }

            ll val = dp[tmp0]+dp[tmp1];

            max = MAX(max, val);

        }

        dp[k] = max;

    }

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



    return 0;

}


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

#include <string>

#include <cstring>

#include <stdlib.h>

#include <math.h>

#include <algorithm>

#include <vector>

#include <set>

#include <map>

#include <queue>

#include <list>

#include <iterator>

#include <assert.h>

#pragma warning(disable:4996) 



typedef long long ll;

#define MIN(a, b) ((a)>(b)? (b): (a))

#define MAX(a, b) ((a)<(b)? (b): (a))

#define LINF 9223300000000000000

#define INF 2140000000

const long long MOD = 1000000007;

using namespace std;





ll a[16][16];

ll dp[1<<16];



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

{

    int n;

    scanf("%d", &n);



    int i,j;

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

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

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

        }

    }



    int mm=(1<<n);

    int z[17];

    int k;

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

        int siz=0;

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

            if(k & (1<<i)) z[siz++]=i;

        }



        dp[k]=0;

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

            for(j=i+1; j<siz; j++) {

                dp[k] += a[z[i]][z[j]];

            }

        }

    }



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

        int siz=0;

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

            if(k & (1<<i)) z[siz++]=i;

        }

        ll max=0;

        int MM=(1<<siz);

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

            int tmp0=0;

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

                if(i & (1<<j)) tmp0 |= (1<<z[j]);

            }

            ll val = dp[tmp0]+dp[k^tmp0];

            max = MAX(max, val);

        }

        dp[k] = max;

    }

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



    return 0;

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



using namespace std;



int n, k;

int a[100000];

int dp[100000];



int solve(int i)

{

	if (i == n - 1)

		return dp[i] = 0;

	if (dp[i] != -1)

		return dp[i];

	int ans = INT_MAX;

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

	{

		if (j + i < n)

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

		else

			break;

	}

	return dp[i] = ans;

}



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.4000831173019037x):
#include <bits/stdc++.h>



using namespace std;



int n, k;

int a[100000];

int dp[100000];



int main()

{

	cin >> n >> k;

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

		cin >> a[i];

	dp[0] = 0;

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

	{

		dp[i] = INT_MAX;

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

		{

			if (i - j >= 0)

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

		}

	}

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

	return 0;

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



using namespace std;

typedef long long ll;

typedef unsigned long long ull;



const int mod  = 100003;

const int maxn = 1e5+10;



int a[maxn];

int dp[maxn];

int f(int a)

{

	if (a<0)

		a=-a;

	return a;

}

int k;

int solve(int n)

{

	if (dp[n]!=-1)

		return dp[n];

	int ans=1e9;

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

	{

		int tem=solve(n-i)+f(a[n]-a[n-i]);

		ans=min(tem,ans);

	}

	dp[n]=ans;

	return dp[n];

}

int main()

{

	int n;

	cin>>n>>k;

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

		dp[i]=-1;

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

		cin>>a[i];

	dp[1]=0;

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

	cout<<solve(n);

	return 0;

}


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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <stack>

#include <queue>

#include <algorithm>

using namespace std;

typedef long long ll;

int main(void)

{

	ll i,ans,n,k,count,num;

	while(scanf("%lld%lld",&n,&k)!=EOF)

	{

		ans=0;

		count=1;

		if(k==0)

		{

			ans=n*n;

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

			continue;

		}

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

		{

			num=n%i;

			if((num-k+1)>0)

			ans+=(num-k+1);

			ans+=(count++)*((n-num)/i);

		}

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

	}

 } 

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

#include<algorithm>

using namespace std;

int N,K;

long long ans=0;

int main() {

	#ifdef LOACL

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

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

	#endif

	scanf("%d %d",&N,&K);

	if(K==0)ans=1ll*N*N;

	else {

		for(int b=K+1;b<=N;b++) {

			int x1=b-K;

			int x2=N/b;

			int k=N-x2*b;

			if(k>=K)ans+=k-K+1;

			ans+=x1*x2;

		}

	}

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

	return 0;

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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define pb push_back

#define ms(a, x) memset(a, x, sizeof(a))

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

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

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

const int mod = 1e9;

const int N = 1e5+10;

/*=======================================================================*/



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

int f[N];



int main() {

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

   for1(i, n) {

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

   }

   ms(f, inf);

   f[n] = 0;

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

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

         if(i - j < 1) break;

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

      }

   }

   cout << f[1] << endl;

   return 0;

}

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

}