[<AutoOpen>]
module Program

open System

let read f = stdin.ReadLine() |> f
let reads f = stdin.ReadLine().Split() |> Array.toList |> List.map f

let f xs =
    let rec aux c xs =
        match List.skipWhile ((=) 0) xs with
        | [] -> c
        | ys -> aux (c + 1) (List.skipWhile (fun y -> y > 0) ys)
    aux 0 xs

[<EntryPoint>]
let main _ =
    let n = read int
    let hs = reads int
    seq {
        let mutable xs = hs
        while List.exists (fun x -> x > 0) xs do
            yield f xs
            xs <- List.map (fun x -> if x > 0 then x - 1 else x) xs
    }
    |> Seq.sum
    |> printfn "%i"
    0