记录编号 |
22521 |
评测结果 |
AAAAAAAAAA |
题目名称 |
象棋比赛 |
最终得分 |
100 |
用户昵称 |
reamb |
是否通过 |
通过 |
代码语言 |
Pascal |
运行时间 |
0.102 s |
提交时间 |
2010-11-19 15:16:32 |
内存使用 |
0.87 MiB |
显示代码纯文本
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
sort2(l,j);
if i<r then
sort2(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.