Program Zeroing;

Var  N,i,k,a,b,c:int64;

begin
 Read(N);
 Read(a);
 k:=a;
 i:=2;
 c:=a;
 While i<=N do
 begin
  Read(b);
  if b>c then
   if (abs(b-c)<abs(b-a)) then
    k:=k+abs(b-c)
   else
    k:=k+abs(b-a);
  if b<a then
   a:=b;
  c:=b;
  i:=i+1;
 end;
 Write(k);
end.