比赛 20101119 评测结果 WWWWWWWWWW
题目名称 象棋比赛 最终得分 0
用户昵称 reamb 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 10:07:07
显示代码纯文本
program chess;
var
  a,b:array[1..100000]of longint;
  n,k,i,answer:longint;
procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         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);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
procedure sort2(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=b[(l+r) div 2];
         repeat
           while b[i]<x do
            inc(i);
           while x<b[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=b[i];
                b[i]:=b[j];
                b[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

begin
  assign (input,'chess.in');
  reset (input);
  assign (output,'chess.out');
  rewrite (output);
    readln (n,k);
    for i:=1 to n do
      readln (a[i]);
    sort(1,n);
    for i:=1 to n-1 do
      b[i]:=a[i+1]-a[i];
    sort2(1,n-1);
    for i:=1 to k do
      answer:=answer+b[i];
    writeln (answer);
  close (input);
  close (output);
end.