n=int(input())
arr=list(map(int,raw_input().split()))

def solve(arr,n):
    if arr==sorted(arr):
        return 0
    ans=0
    m=arr[0]
    for i in range(1,n):
        if arr[i]<m:
            ans+=m-arr[i]
        else:
            m=arr[i]
    return ans
print(solve(arr,n))