Scanf.scanf "%d %d" (fun n x ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in

    let rec loop i acc =
        if i = n then acc else
            if a.(i) + a.(i - 1) <= x then loop (i + 1) acc else
                let d = min a.(i) (a.(i) + a.(i - 1) - x) in
                let () = a.(i) <- a.(i) - d in
                let acc = acc + d in
                if a.(i) + a.(i - 1) <= x then loop (i + 1) acc else
                    let e = min a.(i - 1) (a.(i) + a.(i - 1) - x) in
                    let () = a.(i - 1) <- a.(i - 1) - e in
                    loop (i + 1) (acc + e)
    in
    loop 1 0 |> Printf.printf "%d\n"
)