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
    if As[a] < As[a-1]:
      if(max_a not in As[0:a-1]):
        max_a = As[a-1]
      r += max_a - As[a]
  print(r)

main()
