N = int(input())
A = list(map(int,input().split()))
ans = 0

for i in range(1,N):
    diff = A[i] - A[i - 1]
    if diff <= 0:
        pass
    else:
        ans += diff
        A[i] += diff
print(ans)