program main
   implicit none
   integer n
   integer,allocatable,dimension(:):: h
   integer:: i,j,counter=0
   read(*,*)n
   allocate(h(n))
   read(*,*)h
   do i=1,n
10       continue
      do j=n,i,-1

         if(all(h(i:j)>0))then
            h(i:j)=h(i:j)-1
            counter=counter+1
            goto 10
         endif
      enddo
   enddo
   write(*,*)counter
end program main
