比赛 20101119 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 magic 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 09:46:27
显示代码纯文本
program gls;
var

n,a,b,k,i,j,ans:longint;
ss:array[1..100000,1..2]of longint;
st:array[1..100000]of longint;
f,g:text;

   procedure Sort(l, r: longint);
   var
   i, j, x, y: longint;
   begin
   i := l; j := r; x := ss[(l+r) DIV 2,1];
   repeat
   while ss[i,1] < x do i := i + 1;
   while x < ss[j,1] do j := j - 1;
   if i <= j then
   begin
   y := ss[i,1]; ss[i,1] := ss[j,1]; ss[j,1] := y;
   y := ss[i,2]; ss[j,2] := ss[j,2]; ss[j,2] := y;
   i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;
    procedure Sorta(l, r: longint);
   var
   i, j, x, y: longint;
   begin
   i := l; j := r; x := st[(l+r) DIV 2];
   repeat
   while st[i] < x do i := i + 1;
   while x < st[j] do j := j - 1;
   if i <= j then
   begin
   y := st[i]; st[i] := st[j]; st[j] := y;

   i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sorta(l, j);
  if i < r then Sorta(i, r);
end;




begin
  assign(f,'chess.in');
  reset(f);
  assign(g,'chess.out');
  rewrite(g);
  readln(f,n,k);
  for i:=1 to n do
 begin
    readln(f,ss[i,1]);
    ss[i,2]:=i;
 end;
 sort(1,n);
{ for k:=1 to n do write(g,ss[k,1],' '); }
  for i:=1 to n-1 do
  st[i]:=ss[i+1,1]-ss[i,1];
  sorta(1,n-1);
 for i:=1 to k do
  ans:=st[i]+ans;





writeln(g,ans);
close(f);close(g);
end.