program fruit;
var
tot:array[0..20000] of longint;
quea,queb:array[0..10000] of longint;
i,j,n,la,lb,ra,rb,x,max,k,a1,a2,b1,b2,sum:longint;
procedure minx;
var i,j:longint;
begin
if (a1
inc(la,2);
inc(rb,1);
queb[rb]:=a1+a2;
dec(k);
sum:=sum+a1+a2;
exit;
end;
if (b1
inc(lb,2);
inc(rb,1);
queb[rb]:=b1+b2;
dec(k);
sum:=sum+b1+b2;
exit;
end;
inc(la,1);
inc(lb,1);
inc(rb,1);
queb[rb]:=a1+b1;
dec(k);
sum:=sum+a1+b1;
end;
begin
readln(n);
for i:=1 to n do
begin
read(x);
inc(tot[x]);
if x>max then max:=x;
end;
la:=1;lb:=1;ra:=0;rb:=0;
for i:=1 to max do
for j:=1 to tot[i] do
begin
inc(ra);
quea[ra]:=i;
end;
k:=n;
while k<>1 do
begin
a1:=0;a2:=0;b1:=0;b2:=0;
if la<=ra then a1:=quea[la] else a1:=maxlongint;
if la+1<=ra then a2:=quea[la+1] else a2:=maxlongint;
if lb<=rb then b1:=queb[lb] else b1:=maxlongint;
if lb+1<=rb then b2:=queb[lb+1] else b2:=maxlongint;
minx;
end;
writeln(sum);
end.