#include <iostream>
	#include <bits/stdc++.h>
	using namespace std;
	#define ll long long int
	#define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++)
	#define repr(i,n) for (ll i = n - 1; i >= 0; i--)
	#define MAX 1000000
	#define mo 1000000007 
	#define ld long double
	#define pb push_back
	#define mp make_pair
	#define ff first
	#define ss second
 
	bool prime[MAX + 1]; 
	void SieveOfEratosthenes() 
	{ 
	    memset(prime, true, sizeof(prime)); 
	  
	    prime[1] = false; 
	  
	    for (int p = 2; p * p <= MAX; p++) { 
	  
	        // If prime[p] is not changed, then it is a prime 
	        if (prime[p] == true) { 
	  
	            // Set all multiples of p to non-prime 
	            for (int i = p * 2; i <= MAX; i += p) 
	                prime[i] = false; 
	        } 
	    } 
	} 
	ll gcd(ll a, ll b) 
	{ 
	    if (a == 0) 
	        return b; 
	    return gcd(b % a, a); 
	} 
	ll modexp(ll x, ll y, ll p)
	{
	    ll res = 1;   
	    x = x % p;  
	    while (y > 0)
	    {
	        if (y & 1)    
	            res = (res*x) % p;
	        y = y>>1;     
	        x = (x*x) % p; 
	    }
	    return res;
	}
	class gfg 
	{ 
	 public : ll gcd(ll a, ll b)  
	 {  
	    // Everything divides 0  
	    if (a == 0 || b == 0)  
	        return 0;  
	   
	    // Base case     
	    if (a == b)  
	        return a;  
	   
	    // a is greater  
	    if (a > b)  
	        return gcd(a-b, b);  
	    return gcd(a, b-a);  
	 } 
	  
	   
	// Function to return LCM of two numbers  
	  ll lcm(ll a, ll b)  
	 {  
	    return (a*b)/gcd(a, b);  
	 }  
	} ;
	ll mypow(ll b, ll e) {
	    ll ans=1; for(;e;b=b*b%mo,e/=2) if(e&1) ans=ans*b%mo; return ans;}
	ll power2(ll x, ll y) 
	{ 
	    if (y == 0) 
	        return 1; 
	    else if (y % 2 == 0) 
	        return power2(x, y / 2) * power2(x, y / 2); 
	    else
	        return x * power2(x, y / 2) * power2(x, y / 2); 
	} 
	ll mod=100000000;
	#define MAXN 100001
	ll spf[MAXN]; 
	void sieve() 
	{ 
	    spf[1] = 1; 
	    for (int i=2; i<MAXN; i++) 
	  
	        // marking smallest prime factor for every 
	        // number to be itself. 
	        spf[i] = i; 
	  
	    // separately marking spf for every even 
	    // number as 2 
	    for (int i=4; i<MAXN; i+=2) 
	        spf[i] = 2; 
	  
	    for (int i=3; i*i<MAXN; i++) 
	    { 
	        // checking if i is prime 
	        if (spf[i] == i) 
	        { 
	            // marking SPF for all numbers divisible by i 
	            for (int j=i*i; j<MAXN; j+=i) 
	  
	                // marking spf[j] if it is not  
	                // previously marked 
	                if (spf[j]==j) 
	                    spf[j] = i; 
	        } 
	    } 
	}
	ll dp[100001];
	ll solve(vector <ll> h, ll pos)
	{
		if(dp[pos]!=-1)
		return dp[pos];
		ll n=h.size();
		ll i=pos;
		if(pos==n-1)
		return 0;
		if(pos==n-2)
		return (abs(h[n-1]-h[n-2]));
		dp[i]=min(abs(h[i+1]-h[i])+solve(h,pos+1),abs(h[i+2]-h[i])+solve(h,pos+2));
		return dp[i];
	}
	int main()
	{
		ios_base::sync_with_stdio(false);
		cin.tie(NULL);
		cout.tie(NULL);
		ll n,k;
		cin>>n>>k;
		memset(dp,10001,sizeof(dp));
		dp[0]=0;
		vector <ll> h(n);
		rep(i,n) cin>>h[i];
		dp[1]=abs(h[1]-h[0]);
		ll c;
		for(ll i=2;i<=n-1;i++)
		{
			c=i-k;
			if(c<0)
			c=0;
			while(c!=i)
			{
				dp[i]=min(dp[i],dp[c]+abs(h[i]-h[c]));
				c++;
			}
		}
		cout<<dp[n-1];
		return 0;
	}

Measured memory_bytes: 1792