题目地址:http://codevs.cn/problem/1063/
分析:
堆
代码:
var t,n,i,d,e,ti:longint;
a,b:array[1..10000]of longint;
procedure put(a:longint);
var q,s:longint;
begin
inc(t);
b[t]:=a;
s:=t;
while (s<>1)and(b[s div 2]>b[s])do
begin
q:=b[s div 2];
b[s div 2]:=b[s];
b[s]:=q;
s:=s div 2;
end;
end;
function get:longint;
var q,s,p:longint;
stop:boolean;
begin
get:=b[1];
b[1]:=b[t];
t:=t-1;
p:=1;stop:=false;
while ((p*2<=t)or(p*2+1<=t))and(not stop) do
begin
if (p*2+1>t)or(b[p*2]<b[p*2+1])then
s:=p*2
else s:=p*2+1;
if b[p]>b[s] then
begin
q:=b[p];
b[p]:=b[s];
b[s]:=q;
p:=s;
end
else stop:=true;
end;
end;
begin
t:=0;ti:=0;
readln(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n do put(a[i]);
for i:=1 to n-1 do
begin
d:=get;
e:=get;
ti:=ti+d+e;
put(d+e);
end;
writeln(ti);
end.