#!/usr/local/bin/python3
_ = int(input())
A = list(map(int, input().split()))[::-1]

ans = 0
while len(A) != 0:
    m = max(A)
    idx = A.index(m) + 1
    for i in A[:idx]:
        ans += m - i
    A = A[idx:]

print(ans)
