N = int(input())
A = [int(a) for a in input().split()]

ans = 0
for i in range(1, len(A))[::-1]:
    # print(A)
    if A[i-1] > A[i]:
        diff = A[i-1] - A[i]
        for j in range(i, len(A)):
            if A[i-1] > A[j]:
                A[j] += diff
                ans += diff

# print(A)
print(ans)