Cod sursa(job #36995)

Utilizator cheery_g1rlHaller Emanuela cheery_g1rl Data 24 martie 2007 14:13:42
Problema Buline Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.92 kb
var f,g:text;
   q,max,n,i,m,j,p,l,a,b:longint;
   poz:array[1..400000] of 1..200000;
   v:array[1..200000] of -10000..10000;
begin
assign(f,'buline.in');
reset(f);
readln(f,n);
for i:=1 to n do
   begin
     readln(f,a,b);
     if b=0 then v[i]:=0-a
       else v[i]:=a;
     poz[i]:=i;
  end;
close(f);
m:=2*n;
for i:=n+1 to m do poz[i]:=i-n;
max:=0;
l:=200000;
p:=200000;
i:=n;
while i<=m do
  begin
    q:=i-n+1;
    a:=0;
    for j:=i downto q do
       begin
         b:=v[poz[j]]+a;
         if b>max then
            begin
               max:=b;
               p:=poz[j];
               l:=i-j+1;
            end
              else if (b=max)and(poz[j]<p) then  begin p:=poz[j]; l:=i-j+1;end
                 else if (b=max)and(poz[j]=p)and(l>i-j+1) then l:=i-j+1;
           a:=b;
       end;
       inc(i);
  end;
assign(g,'buline.out');
rewrite(g);
writeln(g,max,' ',p,' ',l);
close(g);
end.