Pagini recente » Cod sursa (job #2944031) | Cod sursa (job #171107) | Cod sursa (job #2153442) | Cod sursa (job #1135235) | Cod sursa (job #332913)
Cod sursa(job #332913)
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);rewrite(g);
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
if b2+c*(t[i-1]-t[i]+1)>=0 then
b2:=b2+val(p[i])*x+t[i-1]*c-t[i]*c
else
b2:=val(p[i])*x-c;
if b2>pmax then
pmax:=b2;
end;
if pmax>pmax2 then
pmax2:=pmax;
end;
write(g,pmax2);
close(f);close(g);
end.