import java.io.*;
import java.util.*;

class Main {

    // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
    public static void main(String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        long N;
        N = sc.nextLong();
        long K;
        K = sc.nextLong();
        long[] h = new long[(int)(N)];
        for(int i = 0 ; i < N ; i++){
                h[i] = sc.nextLong();
        }
        solve(N, K, h);
    }

    static void solve(long N, long K, long[] h){
        long[] dp = new long[(int)(N+1)];
        
        for (int i = 2; i <= N; i++){
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        }
        System.out.print(dp[(int)(N)]);

    }
} import java.io.*; io.* java import java.util.*; util.* java class Main {

    // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
    public static void main(String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        long N;
        N = sc.nextLong();
        long K;
        K = sc.nextLong();
        long[] h = new long[(int)(N)];
        for(int i = 0 ; i < N ; i++){
                h[i] = sc.nextLong();
        }
        solve(N, K, h);
    }

    static void solve(long N, long K, long[] h){
        long[] dp = new long[(int)(N+1)];
        
        for (int i = 2; i <= N; i++){
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        }
        System.out.print(dp[(int)(N)]);

    }
} Main // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
    public static void main(String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        long N;
        N = sc.nextLong();
        long K;
        K = sc.nextLong();
        long[] h = new long[(int)(N)];
        for(int i = 0 ; i < N ; i++){
                h[i] = sc.nextLong();
        }
        solve(N, K, h);
    } main {
        final Scanner sc = new Scanner(System.in);
        long N;
        N = sc.nextLong();
        long K;
        K = sc.nextLong();
        long[] h = new long[(int)(N)];
        for(int i = 0 ; i < N ; i++){
                h[i] = sc.nextLong();
        }
        solve(N, K, h);
    } final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N; N N = sc.nextLong() N sc.nextLong() sc.nextLong sc long K; K K = sc.nextLong() K sc.nextLong() sc.nextLong sc long[] h = new long[(int)(N)]; h new long[(int)(N)] (int)(N) for(int i = 0 ; i < N ; i++){
                h[i] = sc.nextLong();
        } int i = 0 ; int i = 0 ; i 0 i < N i N i++ i++ i {
                h[i] = sc.nextLong();
        } {
                h[i] = sc.nextLong();
        } h[i] = sc.nextLong() h[i] h i sc.nextLong() sc.nextLong sc solve(N, K, h) solve N K h String[] args args static void solve(long N, long K, long[] h){
        long[] dp = new long[(int)(N+1)];
        
        for (int i = 2; i <= N; i++){
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        }
        System.out.print(dp[(int)(N)]);

    } solve {
        long[] dp = new long[(int)(N+1)];
        
        for (int i = 2; i <= N; i++){
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        }
        System.out.print(dp[(int)(N)]);

    } long[] dp = new long[(int)(N+1)]; dp new long[(int)(N+1)] (int)(N+1) N 1 for (int i = 2; i <= N; i++){
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        } int i = 2; int i = 2; i 2 i <= N i N i++ i++ i {
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        } {
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        } long tmp = Integer.MAX_VALUE; tmp Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            } {
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            } if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                } i - j - 1 >= 0 i - j - 1 i - j - 1 i j 1 0 {
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                } tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1])) tmp Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1])) Math.min Math tmp dp[i-j] + Math.abs(h[i-1] -h[i-j-1]) dp[i-j] dp i-j i j Math.abs(h[i-1] -h[i-j-1]) Math.abs Math h[i-1] -h[i-j-1] h[i-1] h i-1 i 1 h[i-j-1] h i-j-1 i-j-1 i j 1 dp[i] = tmp dp[i] dp i tmp System.out.print(dp[(int)(N)]) System.out.print System.out System System.out dp[(int)(N)] dp (int)(N) long N N long K K long[] h h
import java.io.*;
import java.util.*;

class Main {

    // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
    public static void main(String[] args) throws Exception {
        final Scanner sc = new Scanner(System.in);
        long N;
        N = sc.nextLong();
        long K;
        K = sc.nextLong();
        long[] h = new long[(int)(N)];
        for(int i = 0 ; i < N ; i++){
                h[i] = sc.nextLong();
        }
        solve(N, K, h);
    }

    static void solve(long N, long K, long[] h){
        long[] dp = new long[(int)(N+1)];
        
        for (int i = 2; i <= N; i++){
            long tmp = Integer.MAX_VALUE;
            for (int j = 1; j <= K; j++){
                if (i - j - 1 >= 0){
                    tmp = Math.min(tmp, dp[i-j] + Math.abs(h[i-1] -h[i-j-1]));
                }
            }
            dp[i] = tmp;
        }
        System.out.print(dp[(int)(N)]);

    }
}
