program one;
type
  numbers=array of integer;
var
  mass:numbers;
  count,N:integer;
function min(mass:numbers;N:integer):integer;
  var
    m,i:integer;
  begin
    m:=0;
    for i:=0 to N-2 do
      if mass[m]>mass[i+1] then m:=i+1;
    min:=m;
  end;
function sum(mass:numbers;N:integer):integer;
 var mass1,mass2:numbers;
     m,i:integer;
 begin
   if n=1 then sum:=mass[0]
   else
   if n=0 then sum:=0
   else
     begin
       m:=min(mass,n);
       setlength(mass1,m);
       setlength(mass2,n-m-1);
       for i:=0 to m-1 do mass1[i]:=mass[i]-mass[m];
       for i:=0 to n-m-2 do mass2[i]:=mass[i+m+1]-mass[m];
       sum:=mass[m]+sum(mass1,m)+sum(mass2,n-m-1);
    end;
  end;
begin
readln(N);
setlength(mass,N);
for count:=0 to N-1 do readln(mass[count]);
write(sum(mass,n));
end.


