
import java.util.Arrays;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner  scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for (int i = 2; i < n; i++) {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        }
        System.out.println(dp[n-1]);
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args) {
        Scanner  scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for (int i = 2; i < n; i++) {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        }
        System.out.println(dp[n-1]);
    }
} Main public static void main(String[] args) {
        Scanner  scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for (int i = 2; i < n; i++) {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        }
        System.out.println(dp[n-1]);
    } main {
        Scanner  scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for (int i = 2; i < n; i++) {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        }
        System.out.println(dp[n-1]);
    } Scanner  scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int[] arr = new int[n]; arr new int[n] n for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = scanner.nextInt();
        } {
            arr[i] = scanner.nextInt();
        } arr[i] = scanner.nextInt() arr[i] arr i scanner.nextInt() scanner.nextInt scanner int[] dp = new int[n]; dp new int[n] n Arrays.fill(dp,Integer.MAX_VALUE) Arrays.fill Arrays dp Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0] = 0 dp[0] dp 0 0 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++) {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        } int i = 2; int i = 2; i 2 i < n i n i++ i++ i {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        } {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        } for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            } int j = 1; int j = 1; j 1 j <=k j k j++ j++ j {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            } {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            } if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]); j<=i j i dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]) dp[i] dp i Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]) Math.min Math dp[i - j] + Math.abs(arr[i] - arr[i - j]) dp[i - j] dp i - j i j Math.abs(arr[i] - arr[i - j]) Math.abs Math arr[i] - arr[i - j] arr[i] arr i arr[i - j] arr i - j i j dp[i] dp i 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  scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for (int i = 2; i < n; i++) {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        }
        System.out.println(dp[n-1]);
    }
} public class Main{
    public static void main(String[] args) {
        Scanner  scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for (int i = 2; i < n; i++) {
            for (int j = 1; j <=k ; j++) {
                if(j<=i)
                    dp[i] = Math.min(dp[i - j] + Math.abs(arr[i] - arr[i - j]), dp[i]);
            }
        }
        System.out.println(dp[n-1]);
    }
} Main

import java.util.Arrays;
import java.util.Scanner;

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