比赛 20101119 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 09:04:57
显示代码纯文本
{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.