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

public class Main {

    private static int frog1(int N, int K, int[] h) {
        // subproblem:
        // f(j) = cost when standing on stone j (0 <= j < N)
        // f(N-1)

        // f(0) = 0
        // f(j) = min:
        //        f(j-1) + abs(h[j], h[j-1])
        //        f(j-2) + abs(h[j], h[j-2])
        //        ...
        //        f(j-K) + abs(h[j], h[j-K])

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        }
        return dp[N-1];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        }
        int res = frog1(N, K, h);
        System.out.println(res);
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

    private static int frog1(int N, int K, int[] h) {
        // subproblem:
        // f(j) = cost when standing on stone j (0 <= j < N)
        // f(N-1)

        // f(0) = 0
        // f(j) = min:
        //        f(j-1) + abs(h[j], h[j-1])
        //        f(j-2) + abs(h[j], h[j-2])
        //        ...
        //        f(j-K) + abs(h[j], h[j-K])

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        }
        return dp[N-1];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        }
        int res = frog1(N, K, h);
        System.out.println(res);
    }
} Main private static int frog1(int N, int K, int[] h) {
        // subproblem:
        // f(j) = cost when standing on stone j (0 <= j < N)
        // f(N-1)

        // f(0) = 0
        // f(j) = min:
        //        f(j-1) + abs(h[j], h[j-1])
        //        f(j-2) + abs(h[j], h[j-2])
        //        ...
        //        f(j-K) + abs(h[j], h[j-K])

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        }
        return dp[N-1];
    } frog1 {
        // subproblem:
        // f(j) = cost when standing on stone j (0 <= j < N)
        // f(N-1)

        // f(0) = 0
        // f(j) = min:
        //        f(j-1) + abs(h[j], h[j-1])
        //        f(j-2) + abs(h[j], h[j-2])
        //        ...
        //        f(j-K) + abs(h[j], h[j-K])

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        }
        return dp[N-1];
    } 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 for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        } int j = 1; int j = 1; j 1 j < N j N j++ j++ j {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        } {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        } for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            } int i = Math.max(0, j-K); int i = Math.max(0, j-K); i Math.max(0, j-K) Math.max Math 0 j-K j K i < j i j i++ i++ i {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            } {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            } dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i])) dp[j] dp j Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i])) Math.min Math dp[j] dp j dp[i] + Math.abs(h[j] - h[i]) dp[i] dp i Math.abs(h[j] - h[i]) Math.abs Math h[j] - h[i] h[j] h j h[i] h i return dp[N-1]; dp[N-1] dp N-1 N 1 int N N int K K int[] h h public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        }
        int res = frog1(N, K, h);
        System.out.println(res);
    } main {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        }
        int res = frog1(N, K, h);
        System.out.println(res);
    } 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[] h = new int[N]; h new int[N] N for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            h[i] = scanner.nextInt();
        } {
            h[i] = scanner.nextInt();
        } h[i] = scanner.nextInt() h[i] h i scanner.nextInt() scanner.nextInt scanner int res = frog1(N, K, h); res frog1(N, K, h) frog1 N K h System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main {

    private static int frog1(int N, int K, int[] h) {
        // subproblem:
        // f(j) = cost when standing on stone j (0 <= j < N)
        // f(N-1)

        // f(0) = 0
        // f(j) = min:
        //        f(j-1) + abs(h[j], h[j-1])
        //        f(j-2) + abs(h[j], h[j-2])
        //        ...
        //        f(j-K) + abs(h[j], h[j-K])

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        }
        return dp[N-1];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        }
        int res = frog1(N, K, h);
        System.out.println(res);
    }
} public class Main {

    private static int frog1(int N, int K, int[] h) {
        // subproblem:
        // f(j) = cost when standing on stone j (0 <= j < N)
        // f(N-1)

        // f(0) = 0
        // f(j) = min:
        //        f(j-1) + abs(h[j], h[j-1])
        //        f(j-2) + abs(h[j], h[j-2])
        //        ...
        //        f(j-K) + abs(h[j], h[j-K])

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        }
        return dp[N-1];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        }
        int res = frog1(N, K, h);
        System.out.println(res);
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    private static int frog1(int N, int K, int[] h) {
        // subproblem:
        // f(j) = cost when standing on stone j (0 <= j < N)
        // f(N-1)

        // f(0) = 0
        // f(j) = min:
        //        f(j-1) + abs(h[j], h[j-1])
        //        f(j-2) + abs(h[j], h[j-2])
        //        ...
        //        f(j-K) + abs(h[j], h[j-K])

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int j = 1; j < N; j++) {
            for (int i = Math.max(0, j-K); i < j; i++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(h[j] - h[i]));
            }
        }
        return dp[N-1];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) {
            h[i] = scanner.nextInt();
        }
        int res = frog1(N, K, h);
        System.out.println(res);
    }
}
