N = int(input())
A = [int(x) for x in input().split()]
buf = 0

for i in range(N-1) :
    if A[i] - A[i+1] >0 :
        buf += A[i] - A[i+1]
if A[N-2] - A[N-1] >0 :
    buf += A[N-2] - A[N-1]
print(buf)