记录编号 22554 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 Gravatarbelong.zmx 是否通过 通过
代码语言 Pascal 运行时间 0.103 s
提交时间 2010-11-19 21:15:58 内存使用 0.87 MiB
显示代码纯文本
program chess(input,output);
var
 n,k:longint;
 a:array[1..100000]of longint;
 f:array[1..100000]of longint;
 i,j,ans:longint;

procedure sort1(x,y:longint);
var
 i,j,k,p:longint;
begin
 i:=x;
 j:=y;
 k:=a[(x+y) div 2];
 repeat
  while a[i]>k do inc(i);
  while a[j]<k do dec(j);
  if not(i>j) then
  begin
   p:=a[i];
   a[i]:=a[j];
   a[j]:=p;
   inc(i);
   dec(j);
  end;
 until i>j;
 if i<y then sort1(i,y);
 if j>x then sort1(x,j);
end;

procedure sort2(x,y:longint);
var
 i,j,k,p:longint;
begin
 i:=x;
 j:=y;
 k:=f[(x+y) div 2];
 repeat
  while f[i]<k do inc(i);
  while f[j]>k do dec(j);
  if not(i>j) then
  begin
   p:=f[i];
   f[i]:=f[j];
   f[j]:=p;
   inc(i);
   dec(j);
  end;
 until i>j;
 if i<y then sort2(i,y);
 if j>x then sort2(x,j);
end;

begin
 assign(input,'chess.in');
 reset(input);
 readln(n,k);
 for i:=1 to n do readln(a[i]);
 close(input);

 sort1(1,n);

 for i:=1 to n-1 do
  f[i]:=a[i]-a[i+1];

 sort2(1,n-1);

 for i:=1 to k do
  ans:=ans+f[i];

 assign(output,'chess.out');
 rewrite(output);
 writeln(ans);
 close(output);
end.