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

high = 0
cnt = 0
for i in range(1,N):
    if high <= A[i]:
        high = A[i]
    if A[i] >= A[i-1]:
        cnt += high - A[i-1]
        
print(cnt)