记录编号 22118 评测结果 WAWWWAWWWA
题目名称 物品 最终得分 30
用户昵称 Gravatar苏轼 是否通过 未通过
代码语言 Pascal 运行时间 0.008 s
提交时间 2010-11-17 11:39:28 内存使用 0.15 MiB
显示代码纯文本
program migica(input,output);

type
  arr=array[1..2]of dword;

var
  i,j,n,pi,sum,sum1,sum2,a,b:dword;
  ch:char;
  p:array[1..5000]of arr;

procedure qsort(l,h:integer);
  var
    i,j,m:integer;
    t:arr;
  begin
    i:=l;
    j:=h;
    m:=p[(i+j) div 2,2];

   repeat
     while p[i,2]<m do
       inc(i);

     while m<p[j,2] do
       dec(j);

     if i<=j then
     begin
       t:=p[i];
       p[i]:=p[j];
       p[j]:=t;
       inc(i);
       dec(j);
     end;
   until i>j;

   if i<h then
     qsort(i,h);

   if j>l then
     qsort(l,j);
  end;

begin
  assign(input,'magica.in');
  reset(input);
  assign(output,'magica.out');
  rewrite(output);

  readln(n,pi);
  for i:=1 to n do
  begin
    read(a);
    read(ch);
    if ch=' ' then
    begin
      readln(b);

      if b-a<=pi then
        sum:=sum+a
      else
      begin
        inc(j);
        p[j,1]:=a;
        p[j,2]:=b;
      end;
    end
    else
      sum:=sum+a;
  end;

  qsort(1,j);

  sum1:=sum;
  for i:=1 to j do
    if sum1>=pi then
      sum1:=sum1+p[i,2]-pi
    else
      sum1:=sum1+p[i,1];

  sum2:=sum;
  for i:=j downto 1 do
    if sum2>=pi then
      sum2:=sum2+p[i,2]-pi
    else
      sum2:=sum2+p[i,1];

  if sum1>sum2 then
    writeln(sum1)
  else
    writeln(sum2);

  close(input);
  close(output);
end.