let bsearch predicate maxv =
  let rec loop minp maxp = match compare minp maxp with
      0 -> minp
    | _ ->
      let middle = (maxp+minp)/2 + 1 in
      match predicate middle with
        true -> loop minp middle
      | false -> loop middle maxp
  in
  loop 1 maxv

let check num_of_truck goods capacity =
  let rec loop acc = function
      _, [] -> true
    | 0, _  -> false
    | rem_truck, (first::rest as l) -> match first+acc with
        x when x > capacity -> loop 0 (rem_truck-1,l)
      | x -> loop x (rem_truck,rest)
  in
  loop 0 (num_of_truck,goods)

let () =
  let n,num_of_truck = Scanf.scanf "%d %d\n" (fun a b -> a,b) in
  let set_goods () =
    let rec loop l sum = function
        i when i >= n -> l,sum
      | i ->
        let v = read_int () in
        Printf.printf "%d %d\n" i v;
        loop (v::l) (sum+v) (i+1)
    in
    loop [] 0 0
  in
  let goods,sum = set_goods () in
  List.iter print_int goods;
  Printf.printf "%d\n" (bsearch (check num_of_truck goods) sum)