N = gets.to_i
H = gets.chomp.split(" ").map(&:to_i)

def block_subtraction(f, t)
  mi = H[f..t].min
  (f..t).each{|i| H[i] -= mi}
  ret = mi
  w_f = -1
  (f..t).each{|i|
    w_f = i if w_f == -1 && H[i] > 0
    (ret += block_subtraction(w_f, i-1); w_f = -1) if w_f > -1 && H[i] == 0
  }
  ret += block_subtraction(w_f, t) if w_f > -1
  ret
end

puts block_subtraction(0, N-1)
