require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
n = gets.chomp.to_i
h = gets.chomp.split.map(&:to_i)

if n <= 2
  puts h.max
  exit
end

inc = true
hight = h.first
ans = h.first
(n-1).times do |i|
  if inc
    if h[i] <= h[i+1]
      # pass
    else
      ans += h[i] - hight
      hight = h[i+1]
      inc = false
    end
  else
    if h[i] >= h[i+1]
      hight = h[i+1]
    else
      inc = true
    end
  end
  # p [i,'',hight, inc, ans]
end

ans += h.last - hight
puts ans
