program bing;
var
f1,f2:text;
n,tot,i,m,s0,ss:longint;
a:array[0..10000] of longint;
procedure ex(c,b:longint);
var temp:longint;
begin
temp:=a[b];a[b]:=a[c];a[c]:=temp;
end;
procedure into(x:longint);
var s:longint;
begin
inc(tot);
a[tot]:=x;
s:=tot;
while (s<>1) and(a[s]<a[s div 2]) do
begin ex(s,s div 2);
s:=s div 2;
end;
end;
procedure out;
var s,j:longint;
begin
a[1]:=a[tot];
dec(tot);
s:=1;
while s*2<=tot do begin
if (s*2=tot)or (a[s*2]<a[s*2+1])then j:=s*2
else j:=s*2+1;
if a[s]>a[j] then begin
ex(s,j);
s:=j;
end
else exit;
end;
end;
begin
assign(f1,'fruit.in');reset(f1);
assign(f2,'fruit.out');rewrite(f2);
readln(f1,n);
read(f1,a[1]);
tot:=1;
for i:=2 to n do begin
read(f1,m);
into(m);end;
ss:=0;
for i:=1 to n-1 do begin
s0:=a[1];
out;
s0:=s0+a[1];
ss:=ss+s0;
out;
into(s0);
end;
write(f2,ss);
close(F1);close(f2);
end.