Program Zeroing;
Var a: array [1..200000] of longint;
i,b,n: longint; smax, smin, d: int64;
Begin
Readln(n);
for i:=1 to n do
Read(a[i]);
smax:=0; smin:=0; b:=0;
  if n=1 
  then d:=a[1]
  else if n=2 
       then if a[1]>a[2] 
            then d:=a[1] 
            else d:=a[2]
       else Begin
if a[1]>a[2] then smax:=smax+a[1];
if a[n-1]<a[n] then smax:=smax+a[n];
for i:=2 to (n-1) do
Begin
if ((a[i-1]<a[i]) and (a[i]>a[i+1])) then smax:=smax+a[i];
if ((a[i-1]>a[i]) and (a[i]<a[i+1])) then smin:=smin+a[i];
if ((a[i-1]<a[i]) and (a[i]=a[i+1])) then
Begin
repeat
b:=b+1;
until a[i+b]<>a[i+1+b];
if a[i+b]>a[i+1+b] then smax:=smax+a[i];
b:=0;
end;
if ((a[i-1]>a[i]) and (a[i]=a[i+1])) then
Begin
repeat
b:=b+1;
until a[i+b]<>a[i+1+b];
if a[i+b]<a[i+1+b] then smin:=smin+a[i];
b:=0;
end;
end;
d:=smax-smin;
end;
Writeln(d);
end.