我的想法是:从小到大排序 加合前面2个,不断循环
但是和答案差一点 谁帮我看看..
type
int=longint;
var
n,s,i,k,last:int;
ai:array[1..20000]of int;
procedure qsort(l,r:int);
var
i,j,mid,k:int;
begin
i:=l; j:=r; mid:=ai[random(r-l)+l];
repeat
while ai[i]<mid do inc(i);
while ai[j]>mid do dec(j);
if i<=j then
begin
k:=ai[i];
ai[i]:=ai[j];
ai[j]:=k;
inc(i); dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if j>l then qsort(l,j);
end;
begin
assign(input,'p1066.in');reset(input);
assign(output,'p1066.out');rewrite(output);
readln(n);
for i:=1 to n do
read(ai[i]);
last:=n;
for i:=1 to n do
begin
qsort(1,last);
ai[2]:=ai[1]+ai[2];
ai[1]:=0;
k:=ai[last];
ai[last]:=ai[1];
ai[1]:=k;
dec(last);
s:=s+ai[2];
end;
writeln(s);
close(input);close(output);
end.