比赛 20101117 评测结果 WWWWWWWWWW
题目名称 物品 最终得分 0
用户昵称 1102 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-17 10:55:37
显示代码纯文本
program xxxx;
var a:array[1..5000,0..2] of longint;
    s,i,j,n,p,x,head,tail:longint;
begin
  assign(input,'magica.in');
  reset(input);
  assign(output,'magica.out');
  rewrite(output);
  s:=0;
  readln(n,p);
  for i:=1 to n do
    begin
      repeat
        begin
          inc(a[i,0]);
          read(a[i,a[i,0]]);
        end
      until
        eoln;
      if a[i,0]=1 then
        s:=s+a[i,1]
      else
        if a[i,2]-a[i,1]<=p then
          begin
            a[i,0]:=1;
            s:=s+a[i,1];
          end;
    end;
  for i:=1 to n do
    if a[i,0]=1 then
      begin
        for j:=i+1 to n do
          begin
            a[j-1,0]:=a[j,0];
            a[j-1,1]:=a[j,1];
            a[j-1,2]:=a[j,2];
          end;
        dec(n);
      end;
  for i:=1 to n-1 do
    for j:=i+1 to n do
      if a[i,2]-a[i,1]>a[j,2]-a[j,1] then
        begin
          x:=a[i,2];
          a[i,2]:=a[j,2];
          a[j,2]:=x;
          x:=a[i,1];
          a[i,1]:=a[j,1];
          a[j,1]:=x;
        end;
  tail:=n;
  head:=1;
  repeat
    begin
      if s>=p then
        begin
          s:=s-p;
          s:=s+a[tail,2];
          dec(tail);
        end
      else
        begin
          s:=s+a[head,1];
          inc(head);
        end;
    end
  until
    tail<head;
  write(s);
  close(input);
  close(output);
end.