import std.stdio;
import std.conv;
import std.string;
import std.format;
import std.algorithm;
import std.math;

//string lines = q"[
//abcdeffg]";

long INF = pow(10, 5);
long[] H = [0L];

long cost(int i, int j)
{
    return abs(H[i]-H[j]).to!long;
}

void main()
{
  string lines;
  string buf;

  while (!stdin.eof) {
    buf = stdin.readln();
    lines ~= buf;
  }

  string[] array = splitLines(lines);
    
  int N = array[0].split(" ")[0].to!int;
  int K = array[0].split(" ")[1].to!int;
  array[1].split(" ").each!(a => H ~= a.to!long);
  
  long[][] dp = new long[][](N+1,N+1);
  foreach (ref long[] line; dp) {
    foreach (ref long e; line) {
        e = INF;
    }
  }
  dp[1][1] = 0L;
  
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i+K; j++) {
      if (j>N) continue;
      if (i==1&&j==1) continue;
      if (i > j) {
        dp[i][j] = dp[i-1][j];
      } else {
        dp[i][j] = min(dp[i][i] + cost(i, j), dp[i-1][j]);
      }
    }
  }
  writeln(dp[N][N]);
}