local read = io.read

local n = read("n")
local h_t = {}
for i = 1, n do
	h_t[i] = read("n")
end

local count = 0
for i = 1, n do
	local l, r = i, i
	local h_l = h_t[l]
	while h_t[r + 1] and h_t[r + 1] >= h_t[l] do
		r = r + 1
	end
	local r_plus_1 = h_t[r + 1] or 0

	for j = l, r do
		h_t[j] = h_t[j] - (h_l - r_plus_1)
	end
	count = count + h_l - r_plus_1
end

print(count)
