N = int(input())
array = list(map(int, input().split()))
ans = 0

for x in reversed(range(1,len(array))):
    m = max(array)

    if m > array[x]:
        ans += m - array[x]
    array.pop(-1)

print(ans)
