program c176
 
implicit none
integer :: n, i, m, x
integer, allocatable :: a(:)

read *, n

if(n == 1) then
 print *, 0
 stop
end if

allocate(a(1:n))

x = 0

read *, a(:)

do i = 2, n-1
 m = a(i+1) - a(i) 
 if(m <= 0) then
  x = x
 else
  x = x + m
  a(i+1) = a(i+1) + m
 end if
end do

print *, x

end program c176