      PROGRAM Step
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: n,i,ans
      integer(p),allocatable :: a(:)
      
      read*,n
      allocate( a(n) )
      read*,a
      
      ans = 0
      do i = 2,n
        if( a(i)<a(i-1) )then
          ans = ans + (a(i-1)-a(i))
          a(i) = a(i-1)
        end if
      end do
      
      print*,ans
      
      
      stop
      !debugg
      
      
      
      END PROGRAM