program ex;
type
ss=array[1..10001]of longint;
var
a:ss;
max:longint;
f1,f2:text;
n:longint;
procedure init;
var
i:longint;
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]);
end;
procedure qsort(l,r:longint);
var
i,j,x,y:longint;
begin
i:=l; j:=r; x:=a[(l+r) div 2];
repeat
while a[i]<x do i:=i+1;
while x<a[j] do j:=j-1;
if i<=j then
begin
y:=a[i]; a[i]:=a[j]; a[j]:=y;
i:=i+1;j:=j-1;
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
procedure main;
var
m,i,j,k,x:longint;
begin
for i:=1 to n-1 do
begin
j:=0;
x:=0;
if a[i]<>0 then
begin
x:=a[i]+a[i+1];
a[i+1]:=0;
a[i]:=0;
max:=max+x;
end;
m:=0;
j:=1;
while (x>a[j])and(m<n)do
begin
inc(j);
inc(m);
end;
for j:=i to m do
a[j]:=a[j+1];
a[m]:=x;
end;
end;
begin
init;
qsort(1,n);
max:=0;
main;
writeln(f2,max);
close(f1);
close(f2);
end.