program prob5
    implicit none
    integer :: n, ans
    integer, allocatable :: h(:)
    read(*,*) n
    allocate(h(n))
    read(*,*) h

    ans = 0
    call solve(1, n)
    write(*,*) ans

    deallocate(h)

    stop
contains
recursive subroutine solve(left, right)
    implicit none
    integer :: i, mi
    integer :: left, right
    if(left == right) then
        ans = ans + h(left)
        return
    end if

    mi = minval(h(left:right))
    ans = ans + mi
    do i = left, right
        h(i) = h(i) - mi
    end do


    do i = left, right
        if(h(i) == 0) then
            if(i /= left) then
                call solve(left, i-1)
            end if
            if(i /= right) then
                call solve(i+1, right)
            end if
            return
        end if
    end do

    
    return
end subroutine solve

end program prob5