local N=io.read("n")
h={}
for i=1,N do 
  h[i]=io.read("n")
  end
a=1
s=0
while a<=N-s do
  if h[a]==h[a+1] then
    for i=a,N-1-s do 
      h[i]=h[i+1]
      end
    s=s+1
    else
    a=a+1
    end
  end
if N-s==1 then 
  print(h[1])
  end
if N-s==2 then 
  if h[1]>h[2] then
    print(h[1])
    else print(h[2])
    end
end
D=0
d=0
if h[1]>h[2] then
  D=D+h[1]
  end
if h[N-s]>h[N-s-1] then
  D=D+h[N-s]
  end
for i=2,N-s-1 do 
  if h[i]>h[i-1] and h[i]>h[i+1] then
  D=D+h[i]
  elseif h[i]<h[i-1] and h[i]<h[i+1] then
  d=d+h[i]
  end
end
print(D-d)
