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

ans = 0
maxh = A[-1]

while 1:
    complete = True
    for i in range(1, len(A))[::-1]:
        if A[i-1] > A[i]:
            diff = A[i-1] - A[i]
            ans += diff
            A[i] += diff
            complete = False
    if complete:
        break

print(ans)