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

def rec(h)
	#p h
	if h.size == 1 then
		return h.first
	else
		# 最小値を持つ花で分割
		min = h.min

		i = 0
		new_hs = []
		ps = []
		while i < h.size do
			if h[i] != min then
				ps << h[i] - min
			elsif ps.size > 0 then
				new_hs << Marshal.load(Marshal.dump(ps))
				ps = []
			end
			i += 1
		end
		if ps.size > 0 then
			new_hs << Marshal.load(Marshal.dump(ps))
		end

		# 分割した全ての部分について水やり回数を合計
		#p "new_hs: #{new_hs}"
		result = new_hs.map{|n_h| rec(n_h)}
		#p "result: #{result}"
		return min + result.inject(0, :+)
	end
end

p rec(h)
