Scanf.scanf "%d %d" (fun n x ->
  let rec f i a s =
    if i = n then s
    else
      Scanf.scanf " %d" (fun b ->
        let d = a + b - x in
        if d > 0 then f (i + 1) (max 0 (b - d)) (s + d)
        else f (i + 1) b s
      ) in
  let ans = f 1 (Scanf.scanf " %d" (fun a -> a)) 0 in
  Printf.printf "%d\n" ans
)
