比赛 20101119 评测结果 ATTTTTTTTT
题目名称 象棋比赛 最终得分 10
用户昵称 王者自由 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 09:18:44
显示代码纯文本
program chess;
type arr=array of longint;
var n,k,i,s:longint;
    A,B:arr;
procedure Qsort(var A:arr;n:longint);
  procedure sort(l,r: longint);
  var i,j,x,y: longint;
  begin
    randomize;
    i:=l; j:=r;
    x:=A[trunc(random(r-l))+r];
    repeat
      while A[i]<x do inc(i);
      while x<A[j] do dec(j);
      if not(i>j) then
      begin
        y:=A[i]; A[i]:=A[j]; A[j]:=y;
        inc(i); dec(j);
      end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
  end;
begin
  sort(1,n);
end;
begin
  assign(input,'chess.in'); reset(input);
  assign(output,'chess.out'); rewrite(output);
  readln(n,k);
  SetLength(A,n+1); SetLength(B,n+1);
  for i:=1 to n do readln(A[i]);
  Qsort(A,n);
  for i:=1 to n-1 do B[i]:=A[i+1]-A[i];
  Qsort(B,n);
  s:=0;
  for i:=1 to k do s+=B[i+1];
  writeln(s);
  close(input); close(output);
end.