n = gets.to_i
a_list = gets.split.map(&:to_i)

before = a_list[0]
ans = 0

1.step(n - 1) do |i|
    current = a_list[i]
    inc = 0
    if before > current
        inc = before - current
        ans = ans + inc
    end
    before = current + inc
end

puts ans
