import java.util.*;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		}

		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		}

		System.out.println(dp[n-1]);
	}
} import java.util.*; util.* java public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		}

		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		}

		System.out.println(dp[n-1]);
	}
} Main public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		}

		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		}

		System.out.println(dp[n-1]);
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		}

		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		}

		System.out.println(dp[n-1]);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int[] arr = new int[n]; arr new int[n] n for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			arr[i] = sc.nextInt();
		} {
			arr[i] = sc.nextInt();
		} arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc int[] dp = new int[n]; dp new int[n] n dp[0] = 0 dp[0] dp 0 0 for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		} {
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		} int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			} int j=i-1; int j=i-1; j i-1 i 1 j>=Math.max(0,i-k) j Math.max(0,i-k) Math.max Math 0 i-k i k j-- j-- j {
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			} {
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			} min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i])) min Math.min(min,dp[j]+Math.abs(arr[j]-arr[i])) Math.min Math min dp[j]+Math.abs(arr[j]-arr[i]) dp[j] dp j Math.abs(arr[j]-arr[i]) Math.abs Math arr[j]-arr[i] arr[j] arr j arr[i] arr i dp[i] = min dp[i] dp i min System.out.println(dp[n-1]) System.out.println System.out System System.out dp[n-1] dp n-1 n 1 String[] args args public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		}

		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		}

		System.out.println(dp[n-1]);
	}
} public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		}

		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		}

		System.out.println(dp[n-1]);
	}
} Main
import java.util.*;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i] = sc.nextInt();
		}

		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++)
		{
			int min = Integer.MAX_VALUE;
			for(int j=i-1;j>=Math.max(0,i-k);j--)
			{
				min = Math.min(min,dp[j]+Math.abs(arr[j]-arr[i]));
			}

			dp[i] = min;
		}

		System.out.println(dp[n-1]);
	}
}