N = int(input())
A = list(map(int, input().split()))
max_num = 0
for num in A:
  if num > max_num:
    max_num = num

border = A.index(max_num)
height = []
before_h = A[0]
for h in A[:border]:
  if before_h <= h:
    height.append(0)
    
  elif before_h > h:
    height.append(before_h-h)
  before_h = h

for h in A[border:]:
  height.append(max_num-h)

print(sum(height))

