program ex;
type
ss=array[1..10000]of integer;
var
a:ss;
max:longint;
f1,f2:text;
n:integer;
flag:boolean;
procedure init;
var
i:integer;
begin
assign(f1,'fruit.in');
assign(f2,'fruit.out');
reset(f1);
rewrite(f2);
readln(f1,n);
for i:=1 to n do read(f1,a[i]);
for i:=2 to n do
if a[i]<a[i-1] then flag:=true;
end;
procedure quick(l,r:integer);
var
i,j,x,y:integer;
begin
i:=l;
j:=r;
x:=a[(i+r)div 2];
repeat
while a[i]<x do inc(i);
while a[i]>x do dec(j);
if i<=j then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i);
dec(j);
end;
until i>j;
if l<j then quick(l,j);
if i<r then quick(i,r);
end;
procedure main;
var
m,i,j,t,x:integer;
begin
t:=n;
i:=0;
repeat
j:=0;
x:=0;
m:=0;
inc(i);
if a[i]<>0 then
begin
x:=a[i]+a[i+1];
a[i]:=0;
a[i+1]:=0;
dec(t);
max:=max+x;
end;
if t=1 then exit;
m:=i+1;
j:=i;
while x>a[j] do inc(j);
dec(j);
for i:=j downto m do
a[i-1]:=a[i];
a[j]:=x;
dec(i);
until t=1;
end;
begin
init;
if flag=true then quick(1,n);
max:=0;
main;
writeln(f2,max);
close(f1);
close(f2);
end.