n = int(input())
a = list(map(int, input().split()))

result = 0
for i in range(1, n):
    hoge = a[i]
    a[i] = a[i]+(a[i]-a[i-1]) if a[i]-a[i-1] > 0 else a[i]
    result += a[i] - hoge
    
print(result)