from numpy import max
from array import array
def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  max_a = max(array('L', As))
  for i in range(n-1, 0, -1):
    if i == 0:
      break
    if As[i] < As[i-1]:
      if(max_a not in As[0:i-1]):
        max_a = As[i-1]
      r += max_a - As[i]
  print(r)

main()
