def minimumStoolHeight(A):
    stool_tot = 0
    height_min = 0
    
    if len(A) == 1:
        return 0
    
    height_min = A[0]
    for i in range(1, len(A)):
        if A[i] < A[i-1]:
            
            if A[i-1] > height_min:
                height_min = A[i-1]
            stool_tot += (height_min - A[i])
            
            print(i, height_min, stool_tot)
          
    return stool_tot        
        
N = int(input())
A = list(map(int, input().rstrip().split()))
print(minimumStoolHeight(A))