let () =
  let n, x = Scanf.scanf "%d %d\n" (fun n x -> n, x) in
  let a :: as_ = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in
  List.fold_left (fun (prev, acc) a ->
    let cost = max 0 (prev + a - x) in
    (max 0 (a - cost), cost + acc)) (a, 0) as_
  |> snd
  |> Printf.printf "%d\n"