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

ans = 0
pre_a = 0
i = 1
for a in A:
    if pre_a > a:
        ans += pre_a - a
    else:
        ans += 0
    pre_a = max(A[: i])
    i += 1

print(ans)
