Var a:Array [1..100001] of longint;
n,i,min,mini,c,c1,q,q1:longint;
procedure fmin(l,r,min1:longint; var s:longint);
var j,mi,m:longint;
begin
if l=r then s:=s+a[l]-min1
else begin m:=2000000000;
for j:=l to r do begin
if a[j]<m then begin m:=a[j]; mi:=j; end;
end;
s:=s+m-min1;
if l<>mi then fmin(l,mi-1,m,s);
if r<>mi then fmin(mi+1,r,m,s);
end;
end;
Begin

read(n);
min:=2000000000;
for i:=1 to n do a[i]:=0;
for i:=1 to n do begin read(a[i]);
if a[i]<min then begin min:=a[i]; mini:=i;
end;
end;
c:=a[mini];
if mini<>1 then fmin(1,mini-1,min,c);
if mini<>n then fmin(mini+1,n,min,c);
write(c);
end.
