def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  sortAs = sorted(As)
  max_a = sortAs[-1]
  for i in range(n-1, -1, -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()
