n = int(input())
row = list(map(float, input().split()))
ans = 0
for index in range(1,n):
  if row[index] < row[index - 1]:
    ans += row[index - 1] - row[index]
    row[index] = row[index - 1]
print(ans)