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

ans = 0

loop do
  from = 0

  while h[from] == 0 && from < N
    from += 1
  end

  break if from >= N

  min_value = h[from]
  to = from

  while h[to + 1] != 0 && to < N - 1
    min_value = [min_value, h[to]].min
    to += 1
  end

  ans += min_value

  (from..to).each do |i|
    h[i] -= min_value
  end
end

puts ans
