比赛 |
暑假培训六 |
评测结果 |
AWWWWTWTWT |
题目名称 |
合并果子 |
最终得分 |
10 |
用户昵称 |
辨机ZN |
运行时间 |
0.000 s |
代码语言 |
Pascal |
内存使用 |
0.00 MiB |
提交时间 |
2008-07-23 11:50:22 |
显示代码纯文本
program ex(f1,f2);
type
zn=array [0..100000] of integer;
var
a:zn; f1,f2:text; i,j,k,n:integer;
head,tail,p:longint; now,total:int64;
procedure paixu(left,right:longint);
var x,y,l,r:longint;
begin
l:=left; r:=right; x:=a[(l+r) div 2];
repeat
while a[l]<x do inc(l);
while a[r]>x do dec(r);
if l<=r then
begin
y:=a[l];
a[l]:=a[r];
a[r]:=y;
inc(l);
dec(r);
end;
until l>r;
if l<right then paixu(l,right);
if r>left then paixu(left,r);
end;
begin
assign(f1,'fruit.in'); reset(f1);
assign(f2,'fruit.out'); rewrite(f2);
readln(f1,n);
for i:=1 to n do read(f1,a[i]);
paixu(1,n);
head:=1; tail:=n; total:=0;
for i:=1 to n-1 do
begin
now:=a[head]+a[head+1];
j:=1;
if now>a[tail] then a[tail+1]:=now
else begin
while (a[j]<now) do inc(j);
for p:=j to tail do a[p+1]:=a[p];
end;
inc(tail);
a[j]:=now;
head:=head+2;
total:=total+now;
end;
writeln(f2,total);
close(f1);
close(f2);
end.