def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  max_a = max(As)
  for a in range(n-1, -1, -1):
    if(a == 0):
      break
    splitAs = As[0:a-1]
    if As[a] < As[a-1]:
      if(max_a not in splitAs):
        max_a = As[a-1]
      tmp = As[a-1] - As[a]
      r += tmp
      if tmp + As[a] < max_a:
        r += max_a - (tmp + As[a])
  print(r)


main()
