let cost k cs xs y =
    let rec cost_sub k cs xs y m =
        match cs, xs with
        | c :: cs', x :: xs' when k > 0 ->
                cost_sub (k - 1) cs' xs' y (min m (c + abs (y - x)))
        | _ -> m
    in cost_sub k cs xs y max_int

let rec solve k cs xs ys =
    match ys with
    | y :: ys' ->
            let c = cost k cs xs y in
            solve k (c :: cs) (y :: xs) ys'
    | [] -> List.hd cs

let () =
    let line = read_line () in
    let k = Scanf.sscanf line "%d %d" (fun _ k -> k) in
    let h =
        read_line ()
        |> Str.split (Str.regexp " ")
        |> List.map int_of_string
    in
    match h with
    | x :: xs ->
            print_int (solve k [0] [x] xs);
            print_newline ()
    | _ -> ()
