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

#define F first

#define S second

#define pb push_back

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

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

#define fill(x,v) memset(x, v, sizeof(x))

#define PI (ld)acos(-1)

#define endl '\n'

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

#define dbg2(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl; 

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

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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

typedef pair<int,int> pi;

typedef pair<ll,ll> pl;

const ld eps=(ld)1e-9;

const ll inf=(ll)1e18;

const ll mod=(ll)1e9+7;

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

int f(int i){

	if(i==n-1){

		return 0;

	}

	if(dp[i]!=-1){

		return dp[i];

	}

	int ans=INT_MAX;

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

	{

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

	}

	return dp[i]=ans;

}





void solve(){

	cin>>n>>k;

	fill(dp,-1);

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

	{

		cin>>h[i];

	}

	cout<<f(0);

}



int main(){fio;

int test=1;

// cin>>test;

while(test--){

	solve();

	cout<<endl;

}

return 0;

}

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

#define F first

#define S second

#define pb push_back

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

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

#define fill(x,v) memset(x, v, sizeof(x))

#define PI (ld)acos(-1)

#define endl '\n'

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

#define dbg2(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl; 

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

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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

typedef pair<int,int> pi;

typedef pair<ll,ll> pl;

const ld eps=(ld)1e-9;

const ll inf=(ll)1e9;

const ll mod=(ll)1e9+7;





void solve(){

	int n,k;

	cin>>n>>k;

	int h[n];

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

	{

		cin>>h[i];

	}

	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[j]-h[i]));

			}

		}

	}

	cout<<dp[n-1];

}



int main(){fio;

int test=1;

// cin>>test;

while(test--){

	solve();

	cout<<endl;

}

return 0;

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

using namespace std;

typedef long long int ll;



int main() {

  ll n, k;

  cin >> n >> k;

  vector<ll> h(100010, 0);

  vector<ll> dp(100010, 1000000007);



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

    cin >> h[i + 1];

  }



  dp[0] = 0;

  dp[1] = 0;



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

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

      if (i - j > 0) {

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

      }

    }

  }



  cout << dp[n] << endl;

}


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

using namespace std;



typedef long long int ll;

ll MOD = 1000000007;

ll INFL = 1ll << 60;

ll INF = 1 << 28;



// ====================================================================

int main() {

  // 入力

  int n, k;

  cin >> n >> k;

  vector<int> v(n);

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

    cin >> v[i];

  }



  vector<ll> dp(n);

  for (int i = 0; i < n; i++) dp[i] = INFL;

  dp[0] = 0;

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

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

      if (i - j < 0) break;

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

    }

  }



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

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

using namespace std;



vector<int> dp(100005);



int main(){

    

    int n,k; 

    cin>>n>>k;

    

    vector<int> v (n);

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

        cin>>v[i];

    }

    

    dp[0] = 0;

    

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

        int x = INT_MAX;

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

            

            if(j<=i){

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

            x = dp[i];

            }

        }

        

    }

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

    

}




// Faster C++ solution for the same problem (measured speedup: 1.4320247558453048x):
/*Rabbi Zidni Ilma*/



#include<bits/stdc++.h>

using namespace std;

#define ll long long

#define scl(n) scanf("%lld",&n)

#define scll(n,m) scanf("%lld%lld",&n,&m)

#define pb push_back

#define mp make_pair

#define fr first

#define sc second

#define mod 1000000007

#define read freopen("input.txt","r",stdin)

#define write freopen("output.txt","w",stdout)



ll dp[100005],ar[100005];



int main()

{

	ll n,k,i,j;

    scll(n,k);

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

    scl(ar[i]);

    

    dp[n]=0;

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

    {

    	dp[i]=INT_MAX;

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

    	{

    		if(i+j<=n)

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

    		else

    		break;

		}

	}

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

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

using namespace std;

#define N (int)2e5

int n,k,memo[N],c[N];



int dp(int curr) {

    if (curr == n) return 0;

    if (curr > n) return 1e9;

    int &sol = memo[curr];

    if (sol != -1) return sol;

    int keep = INT_MAX;

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

        keep = min(

            dp(curr + i) + abs(c[curr] - c[curr + i]) ,

            keep

        );

    }

    return sol = keep;

}



int main() {

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

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

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

    memset (memo , -1 , sizeof memo);

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

}


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



#define ll long long

#define ull unsigned long long

#define ld long double



#define tiii tuple < int, int, int >

#define pii pair < int, int >

#define pll pair < ll, ll >

#define pci pair < char, int >

#define psi pair < string, int >

#define pis pair < int, string >



#define vi vector < int >

#define vd vector < double >

#define vld vector < ld >

#define vll vector < ll >

#define vs vector < string >

#define vc vector < char >

#define vpi vector < pii >

#define vpll vector < pll >

#define vpis vector < pis >



#define pb push_back

#define mt make_tuple

#define gt0 get < 0 >

#define gt1 get < 1 >

#define gt2 get < 2 >



#define pi acos(-1)



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



using namespace std;



const int maxn = 1e5 + 5;

const int mod = 1e9 + 9;



ll a[maxn], f[maxn];



int main()

{

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

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



    fast;



    int n, k;



    cin >> n >> k;



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



    f[n] = 0;



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

        f[i] = 1e18;



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

            if(i + j > n) continue;



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

        }

    }



    cout << f[1] << endl;



    return 0;

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



using namespace std;





int alpha[26];





int main()

{

    int n,m;

    cin>>m>>n;

    m-=1;

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

        cout<<i<<" ";

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.216016976732889x):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

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



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

        printf("%d ",i);

    }

    return 0;

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

using namespace std;

const int N = 2e5 + 100;

#define pb push_back

#define mp make_pair

#define f first

#define s second

#define ull unsigned long long

#define ll long long

#define pii pair <int,int>

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

#define FOR(l,i,n) for(int i = l;i < n;++i)

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

#define pll pair <ll,ll>

const ll inf = 1e9 + 7;

const ll ll_inf = 1e17 + 100;   

const double pi = 3.14159265358979323;

inline void fr(){

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

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

}

void boost()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}

 

ll binpow (ll a, ll n)

{

    ll res = 1;

    while (n) {

        if (n & 1)

        {

            res *= a;

            res %= inf;

        }

        a *= a;

        n >>= 1;

        a %= inf;

    }

    return res;

}

        

int n;

int h[N];

int dp[N];

int k;

 

main()	

{                                                                        

	cin >> n >> k;

	dp[0] = inf;

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

	{

	  	cin >> h[i];

	}

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

	{

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

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

		{

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

		}

	}

	cout << dp[n];



}	 


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

#define ll long long

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

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

#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)

#define rrep(i,n) RREP(i,n-1,0)

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

const int inf = 1e9+7;

const ll longinf = 1LL<<60;

const ll mod = 1e9+7;



int main() {

  cin.tie(0);

  ios::sync_with_stdio(false);

  int N, Q;

  cin >> N;

  vector<ll> A(N);

  vector<ll> cnt(100005);

  rep(i, N) {

    cin >> A[i];

    cnt[A[i]]++;

  }

  ll sum = accumulate(all(A), 0LL);

  cin >> Q;

  vector<ll> B(Q), C(Q);

  rep(i, Q) cin >> B[i] >> C[i];

  rep(i, Q) {

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

    cnt[C[i]] += cnt[B[i]];

    cnt[B[i]] = 0;

    cout << sum << endl;

  }

  return 0;

}


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

using namespace std;

#define ll long long

#define ld long double

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

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

#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)

#define rrep(i,n) RREP(i,n-1,0)

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

const int inf = 1e9+7;

const ll longinf = 1LL<<60;

const ll mod = 1e9+7;

const ld eps = 1e-10;



int main() {

  cin.tie(0);

  ios::sync_with_stdio(false);

  int n; cin >> n;

  vector<ll> A(n);

  map<ll, int> mp;

  ll sum = 0;

  rep(i, n) {

    cin >> A[i];

    mp[A[i]]++;

    sum += A[i];

  }

  int q; cin >> q;

  while(q--) {

    ll b, c; cin >> b >> c;

    mp[c] += mp[b];

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

    mp[b] = 0;

    cout << sum << "\n";

  }

  return 0;

}
// Slower C++ solution:
/*

Sharif Hasan - CSE, PUST

Apr 24, 2020 02: 14 PM

*/

#include<bits/stdc++.h>

#define br cout<<"\n"

#define what(x) cout<<"in here var= "<<x<<"\n";



/*STL definations*/

#define pb push_back



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

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

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



using namespace std;



int cnt[100001]={-1};

/*Main function*/

int main()

{

    long long n;long long sm=0;

    cin>>n;

    long long arr[n],cnt[100001]={0};

    FOR(i,n){

        cin>>arr[i];

        ++cnt[arr[i]];

        sm+=arr[i];

    }

    long long k;

    cin>>k;

    //cout<<sm<<endl;

    FOR(i,k){

        long long p,q;

        cin>>p>>q;

        if(cnt[p]!=0){

            long long reduced=cnt[p]*p;

            long long add=cnt[p];

           // cout<<p<<" r= "<<reduced<<" "<<add;br;

            cnt[p]=0;

            cnt[q]+=add;

            long long nxt=add*q;

            sm-=reduced;

           // cout<<sm<<" sm \n";

            sm+=nxt;

        }

        cout<<sm;br;

        

    }

    

    return 0;

}


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

using namespace std;

int a[100005];

long long cnt[100005];

long long ans;

int main()

{

    int n;

	scanf("%d",&n);

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

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

    long long q;

    scanf("%lld",&q);

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

    {

    	int j,k;

    	cin>>j>>k;

    	ans+=(k-j)*cnt[j];

    	cnt[k]+=cnt[j];

    	cnt[j]=0;

		cout<<ans<<endl;

	}

	return 0;

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

using namespace std;



#define ll  long long

#define pb push_back

#define mp make_pair

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

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

#define F first

#define S second

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

#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)

typedef pair<int, int>	pii;

typedef pair<ll, ll>	pl;

typedef vector<int>		vi;

typedef vector<ll>		vl;

typedef vector<pii>		vpii;

typedef vector<pl>		vpl;

typedef vector<vi>		vvi;

typedef vector<vl>		vvl;

const int mod = 1000000007;

int h[100001];

int dp[100001];

int k;



int go(int n){

    if(n<=0) return INT_MAX;

    else if(n==1)return 0;

    else if(n==2) return abs(h[2]-h[1]);

    else {

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

            return dp[n];

        }else{

            int mi=INT_MAX;

            //dp[n]=min(go(n-1)+abs(h[n-1]-h[n]),go(n-2)+abs(h[n-2]-h[n]));

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

               if(n-i>0) mi=min(mi,go(n-i)+abs(h[n-i]-h[n]));

                



            }

            dp[n]=mi;

            return dp[n];

        }  



    }  

}





int main() {

    

    int t, n, m;

    cin>>n>>k;

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

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



    Fo(i,n){

        cin>>h[i];

    }

    dp[1]=0;

    cout<<go(n)<<endl;

    return 0;

}






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

using namespace std;



#define ll  long long

#define pb push_back

#define mp make_pair

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

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

#define F first

#define S second

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

#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)

typedef pair<int, int>	pii;

typedef pair<ll, ll>	pl;

typedef vector<int>		vi;

typedef vector<ll>		vl;

typedef vector<pii>		vpii;

typedef vector<pl>		vpl;

typedef vector<vi>		vvi;

typedef vector<vl>		vvl;

const int mod = 1000000007;



int main() {

    

    int t, n, k;

    cin>>n>>k;

    int dp[n+1]={};

    

//Fo(i,n)cout<<dp[n]<<"  ";

//cout<<endl;

    int h[n+1];

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

    dp[1]=0;

    dp[0]=0;

    Fo(i,n){

        if(i==1) continue;

        int mi =INT_MAX;

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

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

        }

        dp[i]=mi;

        

    }

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

   

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

#define ll long long

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

using namespace std;

const int N = 1e5+5, M=5e5+5, oo=0x3f3f3f3f, mod = 1e9+7;



ll n, arr[N], mem[N], k;

int solve(ll ind){

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

    if(mem[ind] != -1) return mem[ind];

    ll ret = oo;

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

        if(ind+i < n)

            ret = min(ret, solve(ind+i)+abs(arr[ind]-arr[ind+i]));

        else

            break;

    }

    return mem[ind] = ret;



}

int main()

{

    _69e27

    memset(mem, -1, sizeof mem);

    cin >> n >> k;

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

#define ll long long

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

using namespace std;

const int N = 1e5+5, M=1e5+5, oo=0x3f3f3f3f, mod = 1e9+7;



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

int main()

{

    _69e27

    memset(dp, oo, sizeof dp);

    dp[0] = 0;

    cin >> n >> k;

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

        cin >> arr[i];

    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(arr[i]-arr[j]));

        }

    }

    cout << dp[n-1];

    return 0;

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

using namespace std;



#define MAXC 1000000007



int n,k;

int h[100005];

int dp[100005];



int solve(int x){

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

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

    else {

        int aux = MAXC;

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

            if (x+i < n){

                aux = min(aux,abs(h[x]-h[x+i])+solve(x+i));

            }

        }

        return dp[x] = aux;

    }

}



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

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin >> n >> k;

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

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

        cin >> h[i];

    }

    cout << solve(0) << endl;

    return 0;

}

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



using namespace std;



#define endl '\n'



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);



  int n, k;

  cin >> n >> k;

  vector<int> stones(n);

  vector<int> dp(n, 1e9);

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

    cin >> stones[i];

  }

  dp[0] = 0;

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

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

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

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

    }

  }

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



	return 0;

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

using namespace std;



int main() {

    int k,x; cin>>k>>x;

    if (k==1){

        cout<<x<<endl;

        return 0;

    }

    int n = k - 1;

    int m = k - 1;

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

        cout<<x-n;

        cout<<" ";

        n--;

    }

    cout<<x<<" ";

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

        x++;

        cout<<x<<" ";

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.219932988841847x):
////sfef

#include<stdio.h>

int main(){



int k,x,i,p,q,r;

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

p=(x-(k-1));

q=(x+(k-2));

r=(x+(k-1));

if(k==1)

{

    printf("%d",x);

}

else{

    for(i=p;i<=q;i++)    

    {

        printf("%d ",i);

    }

    printf("%d",r);

}



return 0;

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

#include <vector>

using namespace std;



int main( void )

{

	// prime numbers table

	vector <bool> isPrime( 60000 + 1, false );

	isPrime[2] = true;

	for ( int i = 3; i < 60000; i += 2 )

	{

		isPrime[i] = true;

	}



	for ( int i = 3; i < 60000; i += 2 )

	{

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

		{

			isPrime[j] = false;

		}

	}



	vector <int> primes;

	primes.push_back( 2 );

	for ( int i = 3; i < 60000; i += 2 )

	{

		if ( isPrime[i] )

			primes.push_back( i );

	}



	int n;

	while ( cin >> n && n != 0 )

	{

		int kotae = 0;

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

		{

			if ( primes[i] <= n / 2 )

			{

				if ( isPrime.at( n-primes[i] ) )

					kotae++;

			}

		}

		cout << kotae << endl;

	}



}

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

#include <vector>

using namespace std;



int main( void )

{

	vector <bool> isPrime( 50001 + 1 );

	isPrime[2] = true;

	for ( int i = 3; i < 50001; i += 2 )

	{

		isPrime[i] = true;

	}



	for ( int i = 3; i < 50001; i += 2 )

	{

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

		{

			isPrime[j] = false;

		}

	}



	int cnt = 1;

	vector <int> primes( 5500 );

	primes[0] = 2;

	for ( int i = 3; i < 50001; i += 2 )

	{

		if ( isPrime[i] )

		{

			primes[cnt] = i;

			cnt++;

		}

	}



	int n;

	while ( cin >> n && n != 0 )

	{

		int kotae = 0;

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

		{

			if ( primes[i] <= n / 2 )

			{

				if ( isPrime.at( n-primes[i] ) )

					kotae++;

			}

		}

		cout << kotae << endl;

	}



	return 0;

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

#include<cstring>

using namespace std;

int main() 

{

    int k;

    char S[110];

    cin>>k>>S;

    int l=strlen(S);

    if(l>k) 

    {

        S[k]='.';

        S[k+1]='.';

        S[k+2]='.';

        S[k+3]='\0';

    }

    cout<<S<<endl;

    return 0;

}

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

#define Mod 1000000007

using namespace std;

int main()

{

	long long a;

	long long sum, i;

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

	{

		sum = 1;

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

		{

			sum *= i;

			if (sum >= Mod)

			{

				sum %= Mod;

			}

		}

		cout << sum << endl;

	}

	return 0;

}

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

#include <cmath>

#define m 1000000007

int main()

{

	long long sum;

	int n;

	while(~scanf("%d",&n)&&n)

	{

		sum=1;

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

		{

			sum*=i;

			sum=sum%m;

		}

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

	}

}
// Slower C++ solution:
/*

** author:mahmoud magdy

*/

#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define endl "\n"



inline int D()

{

    int m ;

    cin>>m;

    return m ;

}

inline ll lD()

{

    ll m ;

    cin>>m;

    return m ;

}

inline double dD()

{

    double m;

    cin>>m;

    return m;

}

void fast()

{

    ios_base :: sync_with_stdio( 0 ) ;

    cin.tie( 0 ) ;

    cout.tie( 0 ) ;

}

int a[100005];

int dp[100005];

int n , k;

int solve(int i){

 if(i>=n)return 1e9;

 if(i==n-1)

    return 0;

 if(dp[i]!=-1)

      return dp[i];

 int mn=1e9;

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

 int ch1=abs(a[i]-a[i+j])+solve(i+j);

 mn=min(ch1,mn);

 }

 return dp[i]=mn;

}

int main()

{

    fast();

    n=D();k=D();

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

        a[i]=D();

    }

    memset(dp,-1,sizeof dp);

    cout<<solve(0)<<endl;

}


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

using namespace std;



int main() {

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

    vector<int> alturas(n);

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

    vector<int> dp(n,1000000009); dp[0] = 0;

    int values[k];

    for(int i = 1; i <=k; i++) values[i-1] = i;

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

        for(int x: values){

            if(i+x >= n) break;

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

        }

    }

    cout << dp[n-1];

	return 0;

}