Scanf.scanf "%d" (fun n ->
    let h = Array.init n (fun _ -> Scanf.scanf " %d" (fun h -> h)) in
    let m = Array.fold_left max 0 h in

    let rec loop i x mode acc =
        if i > m then acc else
            if x = n then loop (i + 1) 0 0 (if mode = 0 then acc else acc + 1) else
            if h.(x) >= i then loop i (x + 1) 1 acc else
                let acc = if mode = 0 then acc else acc + 1 in
                loop i (x + 1) 0 acc
    in
    loop 1 0 0 0 |> Printf.printf "%d\n"
)