program prob1
    implicit none
    integer(8)::N, i, j, maxf, count, ans, fc, ec
    integer, allocatable::h(:), m(:)
    read(*,*) N
    allocate(h(N))
    allocate(m(N))
    read(*,*) h
    maxf = maxval(h)
    ans = 0

    do while(maxf /= 0)
        count = 0
        do i = 1, N-1
            if(h(i) == 0 .and. h(i+1) > 0) then
                if(count == 0)then
                    fc = i
                end if
                ec = i
                count = count + 1
            end if
        end do
        count = count + 1

        if(sum(h(1:fc)) == 0) then
            count = count -1
        end if
        ans = ans + count

        do i = 1, N
            if(h(i) > 0) then
                h(i) = h(i) - 1
            end if
        end do

        maxf  = maxval(h)
    end do

    write(*,*) ans

    deallocate(h)
    deallocate(m)
    stop
end program prob1