记录编号 1029 评测结果 AAAAAAAAAA
题目名称 75.[NOIP 2004]合并果子 最终得分 100
用户昵称 苏轼 是否通过 通过
代码语言 Pascal 运行时间 0.062 s
提交时间 2008-07-23 18:56:16 内存使用 0.16 MiB
显示代码纯文本
program cch(input,output,f1,f2);
var
 f1,f2:text;
 i,n,s,x,tot:longint;
 a:array[1..10000] of longint;

procedure swap(var x,y:longint);
var
 temp:longint;
begin
 temp:=x;
 x:=y;
 y:=temp;
end;

procedure ins(x:longint);
var
 s:longint;
begin
 inc(tot);
 a[tot]:=x;
 s:=tot;
 while (s>1)and(a[s]<a[s div 2]) do
  begin
   swap(a[s],a[s div 2]);
   s:=s div 2;
  end;
end;

procedure dele;
var
 s,j:longint;
begin
 a[1]:=a[tot];
 dec(tot);
 s:=1;
 while (s*2<=tot) do begin
  if (s*2=tot)or (a[s*2]<a[s*2+1]) then j:=s*2
                                   else j:=s*2+1;
  if a[s]>a[j] then begin
    swap(a[s],a[j]);
    s:=j;
   end
  else exit;
 end;
end;


begin
 assign(f1,'fruit.in');
 assign(f2,'fruit.out');
 reset(f1);
 rewrite(f2);
 readln(f1,n);
 tot:=0;
 for i:=1 to n do begin
  read(f1,x);
  ins(x);
 end;
 s:=0;
 for i:=1 to n-1 do begin
  x:=a[1];
  dele;
  x:=x+a[1];
  s:=s+x;
  dele;
  ins(x);
 end;
 writeln(f2,s);
 close(f1);
 close(f2);
end.