program name
    implicit none
    integer(4):: n,i,maxh, li, ans
    integer(4),allocatable:: h(:)

    read*, n
    allocate(h(n))
    read*, h(:)

    maxh = maxval(h)
    ans = 0
    do while(maxh > 0)

        do i=1,n
            if (h(i) == maxh) then
                li = i
                exit
            end if
        end do

        do i=1,n
            if (h(i) == maxh) then
                if (li+1 /= i) ans=ans+1
                li = i
                h(i) = h(i) - 1
            end if
        end do
        maxh = maxval(h)
    end do

    print*, ans
end program name