比赛 20101119 评测结果 TTTTTTTTTE
题目名称 象棋比赛 最终得分 0
用户昵称 Achilles 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 08:31:19
显示代码纯文本
program chess;
var
  a,b:array[1..100000]of longint;
  c:array[1..100000]of boolean;
  i,j,n,k,s:longint;
procedure qsort1(l,r:integer);
var i,j,m,k:integer;
begin
   i:=l; j:=r;
   m:=a[(l+r)div 2];
   repeat
     while a[i]<m do
       i:=i-1;
     while a[j]>m do
       j:=j-1;
     if i<=j then begin
       k:=a[i];
       a[i]:=a[j];
       a[j]:=k;
       i:=i+1;
       j:=j-1;
     end;
   until i>j;
   if j>l then qsort1(l,j);
   if i<r then qsort1(i,r);
end;
procedure qsort2(l,r:integer);
var i,j,m,k:integer;
begin
   i:=l; j:=r;
   m:=b[(l+r)div 2];
   repeat
     while b[i]<m do
       i:=i+1;
     while b[j]>m do
       j:=j-1;
     if i<=j then begin
       k:=b[i];
       b[i]:=b[j];
       b[j]:=k;
       i:=i+1;
       j:=j-1;
     end;
   until i>j;
   if j>l then qsort2(l,j);
   if i<r then qsort2(i,r);
end;
begin
  assign(input,'chess.in');
  assign(output,'chess.out');
  reset(input);
  rewrite(output);
  readln(n,k);
  for i:=1 to n do
    readln(a[i]);
  qsort1(1,n);
  for i:=1 to n-1 do
    b[i]:=a[i+1]-a[i];
  qsort2(1,n-1);
  for i:=1 to k do
    s:=s+b[i];
  writeln(s);
  close(input);
  close(output);
end.