import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String firstLine = scanner.nextLine();
    String[] firstArgs = firstLine.split(" ");
    int N = Integer.parseInt(firstArgs[0]);
    int K = Integer.parseInt(firstArgs[1]);

    String secondLine = scanner.nextLine();
    String[] secondArgs = secondLine.split(" ");
    int[] H = new int[N];
    for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    }

    int[] minCumulativeCosts = new int[N];
    for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    }

    System.out.println(minCumulativeCosts[N - 1]);
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String firstLine = scanner.nextLine();
    String[] firstArgs = firstLine.split(" ");
    int N = Integer.parseInt(firstArgs[0]);
    int K = Integer.parseInt(firstArgs[1]);

    String secondLine = scanner.nextLine();
    String[] secondArgs = secondLine.split(" ");
    int[] H = new int[N];
    for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    }

    int[] minCumulativeCosts = new int[N];
    for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    }

    System.out.println(minCumulativeCosts[N - 1]);
  }
} Main public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String firstLine = scanner.nextLine();
    String[] firstArgs = firstLine.split(" ");
    int N = Integer.parseInt(firstArgs[0]);
    int K = Integer.parseInt(firstArgs[1]);

    String secondLine = scanner.nextLine();
    String[] secondArgs = secondLine.split(" ");
    int[] H = new int[N];
    for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    }

    int[] minCumulativeCosts = new int[N];
    for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    }

    System.out.println(minCumulativeCosts[N - 1]);
  } main {
    Scanner scanner = new Scanner(System.in);

    String firstLine = scanner.nextLine();
    String[] firstArgs = firstLine.split(" ");
    int N = Integer.parseInt(firstArgs[0]);
    int K = Integer.parseInt(firstArgs[1]);

    String secondLine = scanner.nextLine();
    String[] secondArgs = secondLine.split(" ");
    int[] H = new int[N];
    for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    }

    int[] minCumulativeCosts = new int[N];
    for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    }

    System.out.println(minCumulativeCosts[N - 1]);
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) String firstLine = scanner.nextLine(); firstLine scanner.nextLine() scanner.nextLine scanner String[] firstArgs = firstLine.split(" "); firstArgs firstLine.split(" ") firstLine.split firstLine " " int N = Integer.parseInt(firstArgs[0]); N Integer.parseInt(firstArgs[0]) Integer.parseInt Integer firstArgs[0] firstArgs 0 int K = Integer.parseInt(firstArgs[1]); K Integer.parseInt(firstArgs[1]) Integer.parseInt Integer firstArgs[1] firstArgs 1 String secondLine = scanner.nextLine(); secondLine scanner.nextLine() scanner.nextLine scanner String[] secondArgs = secondLine.split(" "); secondArgs secondLine.split(" ") secondLine.split secondLine " " int[] H = new int[N]; H new int[N] N for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      H[i] = Integer.parseInt(secondArgs[i]);
    } {
      H[i] = Integer.parseInt(secondArgs[i]);
    } H[i] = Integer.parseInt(secondArgs[i]) H[i] H i Integer.parseInt(secondArgs[i]) Integer.parseInt Integer secondArgs[i] secondArgs i int[] minCumulativeCosts = new int[N]; minCumulativeCosts new int[N] N for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    } {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    } if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      } i == 0 i 0 {
        minCumulativeCosts[i] = 0;
        continue;
      } minCumulativeCosts[i] = 0 minCumulativeCosts[i] minCumulativeCosts i 0 continue; int minCost = Integer.MAX_VALUE; minCost Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      } int j = Math.max(i - K, 0); int j = Math.max(i - K, 0); j Math.max(i - K, 0) Math.max Math i - K i K 0 j < i j i j++ j++ j {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      } {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      } int costBetweenNodes = Math.abs(H[i] - H[j]); costBetweenNodes Math.abs(H[i] - H[j]) Math.abs Math H[i] - H[j] H[i] H i H[j] H j int cumulativeCost = minCumulativeCosts[j]; cumulativeCost minCumulativeCosts[j] minCumulativeCosts j int totalCost = costBetweenNodes + cumulativeCost; totalCost costBetweenNodes + cumulativeCost costBetweenNodes cumulativeCost minCost = Math.min(minCost, totalCost) minCost Math.min(minCost, totalCost) Math.min Math minCost totalCost minCumulativeCosts[i] = minCost minCumulativeCosts[i] minCumulativeCosts i minCost System.out.println(minCumulativeCosts[N - 1]) System.out.println System.out System System.out minCumulativeCosts[N - 1] minCumulativeCosts N - 1 N 1 String[] args args public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String firstLine = scanner.nextLine();
    String[] firstArgs = firstLine.split(" ");
    int N = Integer.parseInt(firstArgs[0]);
    int K = Integer.parseInt(firstArgs[1]);

    String secondLine = scanner.nextLine();
    String[] secondArgs = secondLine.split(" ");
    int[] H = new int[N];
    for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    }

    int[] minCumulativeCosts = new int[N];
    for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    }

    System.out.println(minCumulativeCosts[N - 1]);
  }
} public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String firstLine = scanner.nextLine();
    String[] firstArgs = firstLine.split(" ");
    int N = Integer.parseInt(firstArgs[0]);
    int K = Integer.parseInt(firstArgs[1]);

    String secondLine = scanner.nextLine();
    String[] secondArgs = secondLine.split(" ");
    int[] H = new int[N];
    for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    }

    int[] minCumulativeCosts = new int[N];
    for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    }

    System.out.println(minCumulativeCosts[N - 1]);
  }
} Main
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String firstLine = scanner.nextLine();
    String[] firstArgs = firstLine.split(" ");
    int N = Integer.parseInt(firstArgs[0]);
    int K = Integer.parseInt(firstArgs[1]);

    String secondLine = scanner.nextLine();
    String[] secondArgs = secondLine.split(" ");
    int[] H = new int[N];
    for (int i = 0; i < N; i++) {
      H[i] = Integer.parseInt(secondArgs[i]);
    }

    int[] minCumulativeCosts = new int[N];
    for (int i = 0; i < N; i++) {
      if (i == 0) {
        minCumulativeCosts[i] = 0;
        continue;
      }

      int minCost = Integer.MAX_VALUE;
      for (int j = Math.max(i - K, 0); j < i; j++) {
        int costBetweenNodes = Math.abs(H[i] - H[j]);
        int cumulativeCost = minCumulativeCosts[j];
        int totalCost = costBetweenNodes + cumulativeCost;
        minCost = Math.min(minCost, totalCost);
      }
      minCumulativeCosts[i] = minCost;
    }

    System.out.println(minCumulativeCosts[N - 1]);
  }
}
