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

public class Main {
    public static int minCost(int[] h, int i, int[] dp, int k) {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    public static int minCost(int[] h, int i, int[] dp, int k) {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    }
} Main public static int minCost(int[] h, int i, int[] dp, int k) {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    } minCost {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    } if (i == 0) {
            return 0;
        } i == 0 i 0 {
            return 0;
        } return 0; 0 int cost = Integer.MAX_VALUE; cost Integer.MAX_VALUE Integer Integer.MAX_VALUE if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        } dp[i] == -1 dp[i] dp i -1 1 {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        } for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            } {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            } if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                } i - j >= 0 i - j i j 0 {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                } cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j])) cost Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j])) Integer.min Integer cost minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]) minCost(h, i - j, dp, k) minCost h i - j i j dp k Math.abs(h[i] - h[i - j]) Math.abs Math h[i] - h[i - j] h[i] h i h[i - j] h i - j i j dp[i] = cost dp[i] dp i cost return dp[i]; dp[i] dp i int[] h h int i i int[] dp dp int k k public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    } main {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        } {
            scanner.close();
        } scanner.close() scanner.close scanner {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } int size = scanner.nextInt(); size scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int[] h = new int[size]; h new int[size] size for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            } int i = 0; int i = 0; i 0 i < h.length i h.length h h.length 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[] dp = new int[size]; dp new int[size] size Arrays.fill(dp, -1) Arrays.fill Arrays dp -1 1 System.out.println(minCost(h, h.length - 1, dp, k)) System.out.println System.out System System.out minCost(h, h.length - 1, dp, k) minCost h h.length - 1 h.length h h.length 1 dp k String[] args args public class Main {
    public static int minCost(int[] h, int i, int[] dp, int k) {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    }
} public class Main {
    public static int minCost(int[] h, int i, int[] dp, int k) {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static int minCost(int[] h, int i, int[] dp, int k) {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    }
}