比赛 20101117 评测结果 C
题目名称 物品 最终得分 0
用户昵称 苏轼 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-17 10:02:24
显示代码纯文本
program migica(input,output);

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

procedure qsort(l,h:integer);
  var
    i,j,m:integer;
    t:array[1..2]of integer;
  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.