PROGRAM C
  IMPLICIT NONE
  INTEGER :: n,i,tmp=0,sum=0
  INTEGER,allocatable :: a(:)
  READ *, n
  allocate(a(n))
  READ *, a

  Do i=1,n-1
    tmp=a(i)-a(i+1)
    IF(tmp<0)tmp=0
    sum=sum+tmp
    a(i+1)=a(i+1)+tmp
  End do

  PRINT *, sum
END PROGRAM C
