| 记录编号 | 22536 | 评测结果 | AAAAAAAAAA | 
    
        | 题目名称 | 515.象棋比赛 | 最终得分 | 100 | 
    
        | 用户昵称 |  wo shi 刘畅 | 是否通过 | 通过 | 
    
        | 代码语言 | Pascal | 运行时间 | 0.102 s | 
    
        | 提交时间 | 2010-11-19 16:01:09 | 内存使用 | 0.87 MiB | 
    
    
    
    		显示代码纯文本
		
		var
  n,k,i,total:longint;
  a,b:array[0..100000]of 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 sort1(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
           sort1(l,j);
         if i<r then
           sort1(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];
  sort1(1,n-1);
  for i:=1 to k do inc(total,b[i]);
  writeln(total);
  close(input);
  close(output);
end.