Program Perebor;
  Type mas=array[1..10000,1..2] of longint;
  Var n,i,r:longint;
        a,q:mas;
 Procedure Main(rr,k:longint;var c,b:mas);
   Var t,y,m,o,l:longint;
 Begin
   t:=1;
   while t<=n do
     begin
       y:=t+1;
       while y<=n do
         begin
           if (c[t,2]=1)and(c[y,2]=1) then
             begin
               if c[t,1]>c[y,1] then
                 begin
                   l:=y;
                   m:=c[y,1];
                   for o:=1 to n do
                     begin
                       if (b[o,1]<m)and(b[o,2]=1) then
                         begin
                           l:=o;
                           m:=b[o,1];
                         end;
                     end;
                   c[t,2]:=0;
                   b[t,2]:=1;
                   if l<>y then
                     begin
                       c[y,2]:=0;
                       b[y,2]:=1;
                       c[l,2]:=1;
                     end;
                   if k=n-2 then
                     begin
                       if rr+c[t,1]<r then r:=rr+c[t,1];
                     end
                    else main(rr+m+c[t,1],k+1,c,b);
                   c[t,2]:=1;
                   b[t,2]:=0;
                   if l<>y then
                     begin
                       c[y,2]:=1;
                       b[y,2]:=0;
                       c[l,2]:=0;
                     end;
                 end
                else
                 begin
                   l:=t;
                   m:=c[t,1];
                   for o:=1 to n do
                     begin
                       if (b[o,1]<m)and(b[o,2]=1) then
                         begin
                           l:=o;
                           m:=b[o,1];
                         end;
                     end;
                   c[y,2]:=0;
                   b[y,2]:=1;
                   if l<>t then
                     begin
                       c[t,2]:=0;
                       b[t,2]:=1;
                       c[l,2]:=1;
                     end;
                   if k=n-2 then
                     begin
                       if rr+c[y,1]<r then r:=rr+c[y,1];
                     end
                    else main(rr+m+c[y,1],k+1,c,b);
                   c[y,2]:=1;
                   b[y,2]:=0;
                   if l<>t then
                     begin
                       c[t,2]:=1;
                       b[t,2]:=0;
                       c[l,2]:=0;
                     end;
                 end;
             end;
           y:=y+1;
         end;
       t:=t+1;
     end;
 End;
Begin
  read(n);
  for i:=1 to n do read(a[i,1]);
  for i:=1 to n do
    begin
      q[i,1]:=a[i,1];
      q[i,2]:=0;
      a[i,2]:=1;
    end;
  r:=10000000;
  main(0,0,a,q);
  writeln(r);
End.
