let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let rec f a n = if n = 0 then a else f ((n + 1) / 2 :: a) (n / 2)
let ds, s = Array.(fold_left f [] a_s |> List.sort (fun x y -> y - x) |> of_list, fold_left (+) 0 a_s)
let _ = Printf.printf "%d\n" @@ s - Array.(sub ds 0 (min m @@ length ds) |> fold_left (+) 0)