n = gets.to_i
h = gets.split.map &:to_i

diff = h

count = 0

ps = nil
p_min = -1
is_remain = true

while is_remain
	is_remain = false
	(0..diff.size).each {|i|
		if diff[i] != nil && diff[i] > 0 then
			is_remain = true
			if ps == nil then
				ps = i
				p_min = diff[i]
			else
				p_min = [p_min, diff[i]].min
			end
		else
			if ps != nil then
				(ps...i).each{|j|
					diff[j] -= p_min
				}
				count += p_min
				p_min = -1
				ps = nil
			end
		end 
	}
end

puts count