local mmi, mma = math.min, math.max
local n = io.read("*n")
local r = 0
local max = 0
for i = 1, n do
  local a = io.read("*n")
  if a < max then r = r + max - a
  elseif max < a then max = a
  end
end
print(r)