integer N
integer,allocatable,dimension(:)::A
integer ans,baseline,l,r
read*,N
allocate(A(N+1))
read*,A(1:N)
A(N+1)=0
ans=0
do while(maxval(A)/=0)
  do i=1,N
    if(A(i)/=0)then
      l=i
      exit
    endif
  end do
  do i=L+1,N+1
    if(A(I)==0)then
      r=i-1
      exit
    endif
  end do
  baseline=minval(A(l:r))
  ans=ans+baseline
  do i=l,r
    A(i)=A(i)-baseline
  end do
end do
print"(I0)",ans
end