import java.util.Scanner;

 class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = input.nextInt();
        int[] arr = new int[n];
        int[] dp = new int[n];
        for(int i=0;i<n;++i){
            arr[i]=input.nextInt();
        }
        if(n==0 || n==1)
            System.out.println("0");
        else {
            dp[1]=Math.abs(arr[1]-arr[0]);
            for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            }
        }
        System.out.println(dp[n-1]);

    }
} import java.util.Scanner; Scanner java.util class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = input.nextInt();
        int[] arr = new int[n];
        int[] dp = new int[n];
        for(int i=0;i<n;++i){
            arr[i]=input.nextInt();
        }
        if(n==0 || n==1)
            System.out.println("0");
        else {
            dp[1]=Math.abs(arr[1]-arr[0]);
            for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            }
        }
        System.out.println(dp[n-1]);

    }
} Main public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = input.nextInt();
        int[] arr = new int[n];
        int[] dp = new int[n];
        for(int i=0;i<n;++i){
            arr[i]=input.nextInt();
        }
        if(n==0 || n==1)
            System.out.println("0");
        else {
            dp[1]=Math.abs(arr[1]-arr[0]);
            for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            }
        }
        System.out.println(dp[n-1]);

    } main {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = input.nextInt();
        int[] arr = new int[n];
        int[] dp = new int[n];
        for(int i=0;i<n;++i){
            arr[i]=input.nextInt();
        }
        if(n==0 || n==1)
            System.out.println("0");
        else {
            dp[1]=Math.abs(arr[1]-arr[0]);
            for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            }
        }
        System.out.println(dp[n-1]);

    } Scanner input = new Scanner(System.in); input new Scanner(System.in) int n = input.nextInt(); n input.nextInt() input.nextInt input int k = input.nextInt(); k input.nextInt() input.nextInt input int[] arr = new int[n]; arr new int[n] n int[] dp = new int[n]; dp new int[n] n for(int i=0;i<n;++i){
            arr[i]=input.nextInt();
        } int i=0; int i=0; i 0 i<n i n ++i ++i i {
            arr[i]=input.nextInt();
        } {
            arr[i]=input.nextInt();
        } arr[i]=input.nextInt() arr[i] arr i input.nextInt() input.nextInt input if(n==0 || n==1)
            System.out.println("0");
        else {
            dp[1]=Math.abs(arr[1]-arr[0]);
            for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            }
        } n==0 || n==1 n==0 n 0 n==1 n 1 System.out.println("0") System.out.println System.out System System.out "0" {
            dp[1]=Math.abs(arr[1]-arr[0]);
            for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            }
        } dp[1]=Math.abs(arr[1]-arr[0]) dp[1] dp 1 Math.abs(arr[1]-arr[0]) Math.abs Math arr[1]-arr[0] arr[1] arr 1 arr[0] arr 0 for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            } int i = 2; int i = 2; i 2 i < n i n ++i ++i i {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            } {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            } int ans = Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE int j = i-k; j i-k i k if(j<0)j=0; j<0 j 0 j=0 j 0 for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                } int l=j; int l=j; l j l<i l i ++l ++l l {
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                } {
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                } int val = dp[l]+Math.abs(arr[i]-arr[l]); val dp[l]+Math.abs(arr[i]-arr[l]) dp[l] dp l Math.abs(arr[i]-arr[l]) Math.abs Math arr[i]-arr[l] arr[i] arr i arr[l] arr l ans = Math.min(ans,val) ans Math.min(ans,val) Math.min Math ans val dp[i]=ans dp[i] dp i ans 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
import java.util.Scanner;

 class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = input.nextInt();
        int[] arr = new int[n];
        int[] dp = new int[n];
        for(int i=0;i<n;++i){
            arr[i]=input.nextInt();
        }
        if(n==0 || n==1)
            System.out.println("0");
        else {
            dp[1]=Math.abs(arr[1]-arr[0]);
            for (int i = 2; i < n; ++i) {
                int ans = Integer.MAX_VALUE;
                int j = i-k;
                if(j<0)j=0;
                for(int l=j;l<i;++l){
                    int val = dp[l]+Math.abs(arr[i]-arr[l]);
                    ans = Math.min(ans,val);
                }
                dp[i]=ans;
            }
        }
        System.out.println(dp[n-1]);

    }
}
