记录编号 22458 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 Gravatarmaxiem 是否通过 通过
代码语言 Pascal 运行时间 0.172 s
提交时间 2010-11-19 13:10:38 内存使用 0.49 MiB
显示代码纯文本
program chess;
var
  num:array [1..100001] of longint;
  tmp,k,n,i:longint;
  ans:int64;
procedure down(i,n:longint);
begin
  while i*2<=n do begin
    i:=i*2;
	if (i+1<=n) and (num[i+1]>num[i]) then inc(i);
	if num[i]>num[i div 2] then begin
	  tmp:=num[i];
	  num[i]:=num[i div 2];
	  num[i div 2]:=tmp;
	end
	else break;
  end;
end;
procedure heapsort;
begin
  for i:=n div 2 downto 1 do down(i,n);
  for i:=n downto 2 do begin
    tmp:=num[i];
	num[i]:=num[1];
	num[1]:=tmp;
	down(1,i-1);
  end;
end;
begin
  fillchar (num,sizeof(num),0);
  assign (input,'chess.in');
  reset (input);
  readln (n,k);
  for i:=1 to n do readln (num[i]);
  close (input);
  assign (output,'chess.out');
  rewrite (output);
  heapsort;
  for i:=1 to n-1 do num[i]:=num[i+1]-num[i];
  dec(n);
  heapsort;
  for i:=1 to k do inc(ans,num[i]);
  writeln (ans);
  close (output);
end.