n = int(input())
a = list(map(int, input().split()))
ans = 0
heights_stack = [0]
height_max = max(a)
a.append(height_max)
for i in range(n + 1):
hole_height = height_max - a[i]
while hole_height < heights_stack[-1]:
ans += heights_stack.pop() - heights_stack[-1]
if hole_height > heights_stack[-1]:
heights_stack.append(hole_height)
print(ans)