比赛 20101119 评测结果 AAWAWWWAWW
题目名称 象棋比赛 最终得分 40
用户昵称 itachi 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 09:01:43
显示代码纯文本
program t2(input,output);
type
atype=array[1..100000]of integer;


var
data,f:atype;
ans:int64; n,k,i,j:longint;
procedure sort(l,r:longint;var a:atype);
var
  i, j:longint; x, y: integer;
begin
  i := l; j := r; x := a[(l+r) DIV 2];
  repeat
    while a[i] < x do i := i + 1;
    while x < a[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j,a);
  if i < r then Sort(i, r,a);
end;

begin
assign(input,'chess.in');
reset(input);
assign(output,'chess.out');
rewrite(output);
readln(n,k);
for i:= 1 to n do
read(data[i]);
sort(1,n,data);
if not odd(n)  then data[n+1]:=maxint;
for i:= 1 to n div 2 do
begin
 f[i*2-1]:=data[i*2]-data[i*2-1];
 f[i*2]:=data[i*2+1]-data[i*2];
end;
sort(1,n-1,f);
ans:=0;
for i:= 1 to k  do
ans:=ans+f[i];
writeln(ans);
close(input);
close(output);

end.