// package com.patra.cp.atcoder.practice.dp;

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

public class Main {
    private static int solve(int[] arr, int[] dp, int currPos, int K) {
        if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        }
        if(dp[currPos] != -1) {
            return dp[currPos];
        }
        int count = 0;
        int res = Integer.MAX_VALUE;
        if(currPos == arr.length - 1) {
            res = 0;
        }
        for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        }
        dp[currPos] = res;
        return dp[currPos];
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] splits = line.split(" ");
        int N = Integer.parseInt(splits[0]);
        int K = Integer.parseInt(splits[1]);
        int[] arr = new int[N];
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
        line = sc.nextLine();
        splits = line.split(" ");
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        System.out.println(solve(arr, dp, 0, K));
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    private static int solve(int[] arr, int[] dp, int currPos, int K) {
        if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        }
        if(dp[currPos] != -1) {
            return dp[currPos];
        }
        int count = 0;
        int res = Integer.MAX_VALUE;
        if(currPos == arr.length - 1) {
            res = 0;
        }
        for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        }
        dp[currPos] = res;
        return dp[currPos];
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] splits = line.split(" ");
        int N = Integer.parseInt(splits[0]);
        int K = Integer.parseInt(splits[1]);
        int[] arr = new int[N];
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
        line = sc.nextLine();
        splits = line.split(" ");
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        System.out.println(solve(arr, dp, 0, K));
    }
} Main private static int solve(int[] arr, int[] dp, int currPos, int K) {
        if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        }
        if(dp[currPos] != -1) {
            return dp[currPos];
        }
        int count = 0;
        int res = Integer.MAX_VALUE;
        if(currPos == arr.length - 1) {
            res = 0;
        }
        for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        }
        dp[currPos] = res;
        return dp[currPos];
    } solve {
        if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        }
        if(dp[currPos] != -1) {
            return dp[currPos];
        }
        int count = 0;
        int res = Integer.MAX_VALUE;
        if(currPos == arr.length - 1) {
            res = 0;
        }
        for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        }
        dp[currPos] = res;
        return dp[currPos];
    } if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        } currPos >= arr.length currPos arr.length arr arr.length {
            return Integer.MAX_VALUE;
        } return Integer.MAX_VALUE; Integer.MAX_VALUE Integer Integer.MAX_VALUE if(dp[currPos] != -1) {
            return dp[currPos];
        } dp[currPos] != -1 dp[currPos] dp currPos -1 1 {
            return dp[currPos];
        } return dp[currPos]; dp[currPos] dp currPos int count = 0; count 0 int res = Integer.MAX_VALUE; res Integer.MAX_VALUE Integer Integer.MAX_VALUE if(currPos == arr.length - 1) {
            res = 0;
        } currPos == arr.length - 1 currPos arr.length - 1 arr.length arr arr.length 1 {
            res = 0;
        } res = 0 res 0 for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        } int i = currPos + 1; int i = currPos + 1; i currPos + 1 currPos 1 i < arr.length i arr.length arr arr.length i++ i++ i {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        } {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        } if(count == K) {
                break;
            } count == K count K {
                break;
            } break; count++ count int temp = solve(arr, dp, i, K); temp solve(arr, dp, i, K) solve arr dp i K res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i])) res Math.min(res, temp + Math.abs(arr[currPos] - arr[i])) Math.min Math res temp + Math.abs(arr[currPos] - arr[i]) temp Math.abs(arr[currPos] - arr[i]) Math.abs Math arr[currPos] - arr[i] arr[currPos] arr currPos arr[i] arr i dp[currPos] = res dp[currPos] dp currPos res return dp[currPos]; dp[currPos] dp currPos int[] arr arr int[] dp dp int currPos currPos int K K public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] splits = line.split(" ");
        int N = Integer.parseInt(splits[0]);
        int K = Integer.parseInt(splits[1]);
        int[] arr = new int[N];
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
        line = sc.nextLine();
        splits = line.split(" ");
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        System.out.println(solve(arr, dp, 0, K));
    } main {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] splits = line.split(" ");
        int N = Integer.parseInt(splits[0]);
        int K = Integer.parseInt(splits[1]);
        int[] arr = new int[N];
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
        line = sc.nextLine();
        splits = line.split(" ");
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        System.out.println(solve(arr, dp, 0, K));
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String line = sc.nextLine(); line sc.nextLine() sc.nextLine sc String[] splits = line.split(" "); splits line.split(" ") line.split line " " int N = Integer.parseInt(splits[0]); N Integer.parseInt(splits[0]) Integer.parseInt Integer splits[0] splits 0 int K = Integer.parseInt(splits[1]); K Integer.parseInt(splits[1]) Integer.parseInt Integer splits[1] splits 1 int[] arr = new int[N]; arr new int[N] N int[] dp = new int[N]; dp new int[N] N Arrays.fill(dp, -1) Arrays.fill Arrays dp -1 1 line = sc.nextLine() line sc.nextLine() sc.nextLine sc splits = line.split(" ") splits line.split(" ") line.split line " " for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            arr[i] = Integer.parseInt(splits[i]);
        } {
            arr[i] = Integer.parseInt(splits[i]);
        } arr[i] = Integer.parseInt(splits[i]) arr[i] arr i Integer.parseInt(splits[i]) Integer.parseInt Integer splits[i] splits i System.out.println(solve(arr, dp, 0, K)) System.out.println System.out System System.out solve(arr, dp, 0, K) solve arr dp 0 K String[] args args public class Main {
    private static int solve(int[] arr, int[] dp, int currPos, int K) {
        if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        }
        if(dp[currPos] != -1) {
            return dp[currPos];
        }
        int count = 0;
        int res = Integer.MAX_VALUE;
        if(currPos == arr.length - 1) {
            res = 0;
        }
        for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        }
        dp[currPos] = res;
        return dp[currPos];
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] splits = line.split(" ");
        int N = Integer.parseInt(splits[0]);
        int K = Integer.parseInt(splits[1]);
        int[] arr = new int[N];
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
        line = sc.nextLine();
        splits = line.split(" ");
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        System.out.println(solve(arr, dp, 0, K));
    }
} public class Main {
    private static int solve(int[] arr, int[] dp, int currPos, int K) {
        if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        }
        if(dp[currPos] != -1) {
            return dp[currPos];
        }
        int count = 0;
        int res = Integer.MAX_VALUE;
        if(currPos == arr.length - 1) {
            res = 0;
        }
        for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        }
        dp[currPos] = res;
        return dp[currPos];
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] splits = line.split(" ");
        int N = Integer.parseInt(splits[0]);
        int K = Integer.parseInt(splits[1]);
        int[] arr = new int[N];
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
        line = sc.nextLine();
        splits = line.split(" ");
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        System.out.println(solve(arr, dp, 0, K));
    }
} Main
// package com.patra.cp.atcoder.practice.dp;

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

public class Main {
    private static int solve(int[] arr, int[] dp, int currPos, int K) {
        if(currPos >= arr.length) {
            return Integer.MAX_VALUE;
        }
        if(dp[currPos] != -1) {
            return dp[currPos];
        }
        int count = 0;
        int res = Integer.MAX_VALUE;
        if(currPos == arr.length - 1) {
            res = 0;
        }
        for(int i = currPos + 1; i < arr.length; i++) {
            if(count == K) {
                break;
            }
            count++;
            int temp = solve(arr, dp, i, K);
            res = Math.min(res, temp + Math.abs(arr[currPos] - arr[i]));
        }
        dp[currPos] = res;
        return dp[currPos];
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] splits = line.split(" ");
        int N = Integer.parseInt(splits[0]);
        int K = Integer.parseInt(splits[1]);
        int[] arr = new int[N];
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
        line = sc.nextLine();
        splits = line.split(" ");
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(splits[i]);
        }
        System.out.println(solve(arr, dp, 0, K));
    }
}
