记录编号 1035 评测结果 AAAAAAAAAA
题目名称 [NOIP 2004]合并果子 最终得分 100
用户昵称 Gravatar辨机ZN 是否通过 通过
代码语言 Pascal 运行时间 0.078 s
提交时间 2008-07-23 19:26:02 内存使用 0.19 MiB
显示代码纯文本
program ex(f1,f2);
 type
  zn=array [1..20000] of longint;
 var
  a:zn; f1,f2:text;
  total,now:longint; i,j,k,n,m:longint;
  answer:longint;
 procedure swap(var x,y:longint);
  var
   temp:longint;
  begin
   temp:=x;
   x:=y;
   y:=temp;
  end;
 procedure insert(num:longint);
  var s:longint; 
  begin
   inc(total);
   a[total]:=num;
   s:=total;
   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 delete;
  var s:longint; y:longint;
  begin
   a[1]:=a[total];
   dec(total);
   s:=1;
   while (s*2<=total) do
    begin
     if (s*2=total) or (a[s*2]<a[s*2+1])
       then y:=s*2
       else y:=s*2+1;
     if a[s]>a[y]
     then
       begin
       swap(a[s],a[y]);
       s:=y
       end 
     else
      exit;
    end;
  end;


begin
  assign(f1,'fruit.in'); reset(f1);
  assign(f2,'fruit.out'); rewrite(f2);
  readln(f1,n);
  total:=0;
  for i:=1 to n do
   begin
   read(f1,m);
   insert(m);
   end;
  answer:=0;
  for i:=1 to n-1 do
   begin
    now:=a[1];
    delete;
    now:=now+a[1];
    answer:=answer+now;
    delete;
    insert(now);
   end;
  writeln(f2,answer);
  close(f1);
  close(f2);
 end.