program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,ans,l,r
    integer(int32), allocatable:: h(:)

    read*, n
    allocate(h(n))
    read*, h(:)
    ans=0
    do while(sum(h) > 0)
        call search_lr(l,r)
        h(l:r)=h(l:r)-1
        ans=ans+1
    end do
    print'(i0)', ans

contains
    subroutine search_lr(l,r)
        integer(int32):: l,r,i
        i=1
        l=0
        r=0
        do i=1,n
            if (h(i) /= 0) then
                l=i
                exit
            end if
        end do

        do i=l,n
            if(h(i) == 0) exit
            r=i
        end do
    end subroutine
end program name