记录编号 22437 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 Gravatarybh 是否通过 通过
代码语言 Pascal 运行时间 0.164 s
提交时间 2010-11-19 11:40:55 内存使用 0.49 MiB
显示代码纯文本
{Chess NOIP模拟2010-11-19
 Author: yangbohua
 Time: 2010-11-19}

program chess;
var
  a:array[0..100001] of longint;
  n,i,k,temp:longint;
  ans:int64;

procedure down(i,m:longint);
begin
  while i*2<=m do
  begin
    i:=i*2;
    if (i+1<=m) and (a[i+1]>a[i])
      then i:=i+1;
    if a[i]>a[i div 2] then
    begin
      temp:=a[i];
      a[i]:=a[i div 2];
      a[i div 2]:=temp;
    end
    else break
  end;
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]);
  for i:=n div 2 downto 1 do
    down(i,n);
  for i:=n downto 2 do
  begin
    temp:=a[1];
    a[1]:=a[i];
    a[i]:=temp;
    down(1,i-1);
  end;
  for i:=1 to n-1 do
    a[i]:=a[i+1]-a[i];
  n:=n-1;
  for i:=n div 2 downto 1 do
    down(i,n);
  for i:=n downto 2 do
  begin
    temp:=a[i];
    a[i]:=a[1];
    a[1]:=temp;
    down(1,i-1);
  end;
  ans:=0;
  for i:=1 to k do
    ans:=ans+a[i];
  writeln(ans);
  close(input);
  close(output)
end.