local read = io.read

local n = read("n")

local sum = 0
local max = -1
for i = 1, n do
	local read_num = read("n")
	if read_num > max then
		max = read_num
	end
	sum = sum - read_num + max
end

print(sum)
