open Batteries
let n, k = Scanf.sscanf (read_line ()) "%d %d" (fun n k -> n, k)
let h = read_line () |> String.split_on_char ' ' |> List.map int_of_string

let rec loop lst i sum =
  match lst with
  | [] -> sum
  | first :: rest ->
    if i <> 0 then min (loop rest (i-1) sum) (loop rest i (sum+first))
    else loop rest i (sum+first)

let _ = Printf.printf "%d\n" @@ loop h k 0


