记录编号 22552 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 GravatarDeiTy 是否通过 通过
代码语言 Pascal 运行时间 0.121 s
提交时间 2010-11-19 17:24:48 内存使用 7.74 MiB
显示代码纯文本
program chess;
var
   n,k,i,j,t,sum:longint;
   a,cha:array[1..1000000] of longint;
procedure sort1(l,r:longint);
var
   i,j,mid:longint;
begin
  i:=l; j:=r; mid:=a[random(j-i)+i];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if not(i>j) then
     begin
       t:=a[i];
       a[i]:=a[j];
       a[j]:=t;
       inc(i); dec(j);
     end;
  until i>j;
  if l<j then sort1(l,j);
  if i<r then sort1(i,r);
end;
procedure sort2(l,r:longint);
var
   i,j,mid:longint;
begin
  i:=l; j:=r; mid:=cha[random(j-i)+i];
  repeat
    while cha[i]<mid do inc(i);
    while cha[j]>mid do dec(j);
    if not(i>j) then
     begin
       t:=cha[i];
       cha[i]:=cha[j];
       cha[j]:=t;
       inc(i); dec(j);
     end;
  until i>j;
  if l<j then sort2(l,j);
  if i<r then sort2(i,r);
end;
begin
  randomize;
  assign(input,'chess.in');
  assign(output,'chess.out');
  reset(input);
  rewrite(output);
  readln(n,k);
  for i:=1 to n do readln(a[i]);
  sort1(1,n);
  for i:=1 to n-1 do cha[i]:=a[i+1]-a[i];
  sort2(1,n-1);
  for i:=1 to k do sum:=sum+cha[i];
  writeln(sum);
  close(input);
  close(output);
end.