Program Zeroing;
Var
        A:array[1..100] of integer;
        n,max,i,k,j:integer;
Begin
Read(n);
for i:=1 to n do
begin
        Read(A[i]);
end;
k:=0;
max:=0;
j:=1;
while A[j]>0 do
begin
for i:=1 to n do
begin
        if A[i]>max
        then
        begin
                max:=A[i];
                j:=i;
        end;
end;
if (A[j]=A[j+1]) and (A[j]=A[j-1])
        then
                begin
                        A[j]:=A[j]-1;
                        A[j-1]:=A[j-1]-1;
                        A[j+1]:=A[j+1]-1;
                        k:=k+1;
                end
        else if A[j]=A[j+1]
                then
                        begin
                                A[j]:=A[j]-1;
                                A[j+1]:=A[j+1]-1;
                                k:=k+1;
                        end
                else if A[j]=A[j-1]
                        then
                                begin
                                        A[j]:=A[j]-1;
                                        A[j-1]:=A[j-1]-1;
                                        k:=k+1;
                                end
                        else if (A[j]>A[j-1]) and (A[j]>A[j+1])
                        then
                        begin
                                A[j]:=A[j]-1;
                                k:=k+1;
                        end;
end;
for i:=1 to n do
begin
        if A[i]<>0
        then
        begin
                while A[i]<>0 do
                begin
                        A[i]:=A[i]-1;
                        k:=k+1;
                end;
        end;
end;
if n>3
then k:=k-1;
write(k);
End.



