def getFumidai(arr, idx):
  if idx == 0:
    return 0

  my_height = arr[idx]
  target = arr[0:idx]
  max_height = max(target)

  if (my_height >= max_height):
    return 0

  return max_height - my_height

n = int(input())
a = [int(i) for i in input().split(' ')]

result = 0

for i in range(n):
  result += getFumidai(a, i)

print(result)