N = int(input())
A = list(map(int, input().split()))
now = A[0]
ans = 0
for i in range(1,N):
    if A[i]<now:
        step = now - A[i]
        ans += now
    else:
        step = 0
    now = A[i]+step
print(ans)
