import copy

n = int(input())
a = input().split()
list = copy.copy(a)
ans = 0

for i in range(1,n):
    if a[i] < a[i-1]:
        a[i] = a[i-1]
        
for j in range(n):
    if a[j] != list[j]:
        ans += int(a[j]) - int(list[j])
print(ans)