var ai:array[0..30000]of longint;
i,j:longint;
n,t:int64;
begin
repeat
t:=0;
readln(n);
for i:=1 to n do read(ai[i]);
for i:=2 to n do
begin
j:=i; ai[0]:=ai[i];
while ai[j-1]>ai[0] do
begin
ai[j]:=ai[j-1];
j:=j-1;
end;
ai[j]:=ai[0];
end;
for i:=1 to n-1 do
begin
ai[i+1]:=ai[i]+ai[i+1];
t:=t+ai[i+1];
end;
writeln(t);
readln;
until eof;
end.
我错在哪里