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

res = []
for i in range(len(A) - 1, 0, -1):
    val = A[i]
    max_val = -1
    for j in A[:i]:
        if max_val < j:
            max_val = j

    if max_val > val:
        res.append(max_val - val)
    else:
        res.append(0)

print(sum(res))
