记录编号 3088 评测结果 AAAAAAAAAA
题目名称 [NOIP 2004]合并果子 最终得分 100
用户昵称 Gravatar王瑞祥K 是否通过 通过
代码语言 Pascal 运行时间 1.430 s
提交时间 2008-10-03 15:50:18 内存使用 0.15 MiB
显示代码纯文本
program fruit(input,output);
var
 a:array[1..10000]of longint;
 s:longint;
 p,q,n:longint;
procedure qsort(l,r:integer);
var x,i,j,t:integer;
begin
 i:=l;j:=r;x:=a[i];
 repeat
  while(i<j)and(a[j]>=x)do dec(j);
  t:=a[i];a[i]:=a[j];a[j]:=t;
  while(i<j)and(a[i]<=x)do inc(i);
  t:=a[j];a[j]:=a[i];a[i]:=t;
 until i=j;
 i:=i+1;j:=j-1;
 if i<r then qsort(i,r);
 if j>l then qsort(l,j);
end;
procedure ins(num:longint);
var i,j,m:longint;
begin
 if num<=a[p+2]then a[p+1]:=num;
 if num>=a[n]then begin
  for i:=p+1 to n-1 do a[i]:=a[i+1];
  a[n]:=num;
 end;
 if(num>a[p+2])and(num<a[n])then begin
  i:=p+2; j:=n;
  while j<>i+1 do begin
   m:=(i+j)div 2;
   if a[m]<=num then i:=m;
   if a[m]>num then j:=m;
  end;
  for j:=p+1 to i-1 do a[j]:=a[j+1];
  a[i]:=num;
 end;
end;
begin
 assign(input,'fruit.in');assign(output,'fruit.out');
 reset(input);rewrite(output);
 readln(n);
 s:=0;
 for p:=1 to n do read(a[p]);
 if n=1 then s:=a[1];
 if n>1 then begin
  qsort(1,n);
  for p:=1 to n-2 do begin
   q:=a[p]+a[p+1];
   s:=s+q;
   ins(q);
  end;
  q:=a[p+1]+a[p+2];
  s:=s+q;
 end;
 write(s);
 close(input);close(output);
end.