Cod sursa(job #332920)

Utilizator ionutz32Ilie Ionut ionutz32 Data 20 iulie 2009 23:39:33
Problema Carnati Scor 70
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.85 kb
var t,p:array[1..2000] of longint;
n,c,i,m,b2,pmax,pmax2,x,aux,piv:longint;
f,g:text;
k:boolean;
function sort2(min,max:longint):longint;
         begin
         piv:=t[min+(max-min) div 2];
         i:=min-1;
         m:=max+1;
         k:=true;
         repeat
               repeat
                     i:=i+1;
               until t[i]>=piv;
               repeat
                     m:=m-1;
               until t[m]<=piv;
               if i<m then
                  begin
                  aux:=t[i];
                  t[i]:=t[m];
                  t[m]:=aux;
                  aux:=p[i];
                  p[i]:=p[m];
                  p[m]:=aux;
                  end
               else
                   begin
                   k:=false;
                   sort2:=m;
                   break;
                   end;
         until k=false;
         end;
procedure sort(min,max:longint);
          var p1:longint;
          begin
          if min<max then
             begin
             p1:=sort2(min,max);
             sort(min,p1);
             sort(p1+1,max);
             end;
          end;
function val(pr:longint):word;
         begin
         if pr>=x then
            val:=1
         else
             val:=0;
         end;
begin
assign(f,'carnati.in');
assign(g,'carnati.out');
reset(f);
readln(f,n,c);
for i:=1 to n do
    readln(f,t[i],p[i]);
sort(1,n);
for m:=1 to n do
    begin
    x:=p[m];
    pmax:=val(p[1])*x-c;
    b2:=pmax;
    for i:=2 to n do
        begin
        aux:=b2+c*(t[i-1]-t[i]+1);
        b2:=val(p[i])*x-c;
        if aux>=0 then
           inc(b2,aux);
        if b2>pmax then
           pmax:=b2;
        end;
    if pmax>pmax2 then
       begin
       pmax2:=pmax;
       rewrite(g);
       write(g,pmax2);
       close(g);
       end;
    end;
close(f);
end.