n = gets.to_i
h = gets.chomp.split.map(&:to_i)
res = 0
while h.max > 0
  l = 0
  while h[l] == 0
    l += 1
  end

  r = l
  s = h[l]
  while r < n - 1 && h[r + 1] > 0
    r += 1
    s = h[r] if h[r] < s
  end
  res += s
  l.step(r) do |i|
    h[i] -= s
  end
end
puts res