type mas = array[1..200000] of longint;
var m:mas;

function rec(a,b:longint):int64;
var i,min:longint;
begin
if(a=b)
  then
  rec:=m[a]
  else
  begin
    min:=a;
    for i:=(a+1) to b do
    begin
      if(m[i]<m[min])
      then
        min:=i;
    end;
    if(min=a)
    then
      rec:=rec(a+1,b)
    else
      begin
      if(min=b)
        then
        rec:=rec(a,b-1)
        else
        rec:=rec(a,min-1)+rec(min+1,b)-m[min];
      end;
  end;
end;

var n,i:longint;
ans:int64;
begin
read(n);
for i:=1 to n do
  read(m[i]);
ans:=rec(1,n);
writeln(ans);
end.
