Cod sursa(job #309378)

Utilizator 7RaduRadu Antohi 7Radu Data 30 aprilie 2009 10:15:57
Problema Carnati Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.02 kb
program Carnati;
var
   n,m,c,i,j,k,s,mn,i1,max1,j1,tt,ss,sst,mm,inc1,inc2:longint;
   t,p:array[0..2000] of longint;
   fl:text;
begin
  assign(fl,'carnati.in');
  reset(fl);
  readln(fl,n,c);
  for i := 1 to n do
     readln(fl,t[i],p[i]);
  close(fl);

  for i := 1 to n do
    for j := i+1 to n do
      if t[i]>=t[j] then
        begin
           k:=t[i];
           t[i]:=t[j];
           t[j]:=k;
           k:=p[i];
           p[i]:=p[j];
           p[j]:=k;
        end;

  s:=0;
  tt:=0;
  mm:=0;
  for i := 1 to n do
     begin
        mm:=0;
        for j := 1 to n do
           begin
              if p[j]>=p[i] then
                 i1:=p[i]
              else
                 i1:=0;

              ss:=i1+mm-(t[j]-t[j-1])*c;

              if ss<i1-c then
                 ss:=i1-c;

              if ss>max1 then
                 max1:=ss;
              mm:=ss;
           end;
     end;
   assign(fl,'carnati.out');
   rewrite(fl);
   writeln(fl,max1);
   close(fl);
end.