var
n,i,m,j,g,k:longint;
x:int64;
f,h:byte;
a: array[1..200050] of longint;
begin
readln(n);
for i:=1 to n do begin
read(a[i]);
                 end;
f:=10;
x:=0;
while f<>1 do begin
 m:=1;
 for i:=2 to n do begin
  if a[i]>a[m] then m:=i;
                  end;
 if m=n then begin
   x:=x+a[m]-a[m-1];
   a[m]:=a[m-1];
             end;
 if m=1 then begin
  if a[m]-a[m+1]<>0 then begin
   x:=x+a[m]-a[m+1];
   a[m]:=a[m+1];
                          end
   else begin
    j:=m;
    h:=1;
    while (j<=n) and (h=1)  do begin
    if a[j]<>a[m] then begin h:=0; g:=j; end;
    inc(j);
                               end;
    j:=m;
    k:=a[m];
    while j<=g-1 do begin
    a[j]:=a[g];
    inc(j);
                               end;
    x:=x+k-a[g];
        end;
             end;

 if (m<>1) and (m<>n) then begin
  if a[m-1]>a[m+1] then begin
  x:=x+a[m]-a[m-1];
  a[m]:=a[m-1];
                        end
  else begin
   if a[m]-a[m+1]<>0 then begin
   x:=x+a[m]-a[m+1];
   a[m]:=a[m+1];
                          end
   else begin
    j:=m;
    h:=1;
    while (j<=n) and (h=1)  do begin
    if a[j]<>a[m] then begin h:=0; g:=j; end;
    inc(j);
                               end;
    if a[m-1]>a[g] then g:=m-1;
    j:=m;
    k:=a[m];
    while j<=g-1 do begin
    a[j]:=a[g];
    inc(j);
                               end;
    x:=x+k-a[g];
        end;
       end;
                           end;
   h:=0;
  for i:=1 to n do begin
  if a[i]=1 then h:=1
  else begin
  h:=0;
  break;
       end;
                   end;
  if h=1 then f:=1;
              end;

x:=x+1;
write(x);
end.
