Cod sursa(job #28743)

Utilizator andrewgPestele cel Mare andrewg Data 8 martie 2007 11:08:45
Problema Buline Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.27 kb
const maxn = 400000;

var f:text;
    n,i,p,l,ml,mp:longint;
    s,max:longint;
    a:array[1..maxn]of longint;

procedure readdata;
var x,y:longint;
begin
   assign(f,'buline.in');
   reset(f);
   readln(f,n);
   for i:=1 to n do
   begin
      readln(f,x,y);
      if y=0 then y:=-1;
      a[i]:=x*y;
      a[i+n]:=x*y;
   end;
   close(f);
end;

procedure solve;
begin
   i:=2;
   s:=a[1];
   p:=1;
   l:=1;
   while i<=2*n do
   begin
      if (s>=0) then
      begin
         if l<n then
         begin
            s:=s+a[i];
            inc(l);
         end
            else
         begin
            s:=s-a[i-l];
            if s>0 then
            begin
               s:=s+a[i];
               inc(p);
               if p>n then p:=p-n;
            end;
         end;
      end
         else
      begin
         s:=a[i];
         p:=i;
         if p>n then p:=p-n;
         l:=1;
      end;
      if s>max then
      begin
         max:=s;
         mp:=p;
         if mp>n then mp:=mp-n;
         ml:=l;
      end;
      inc(i);
   end;
end;

procedure writedata;
begin
   assign(f,'buline.out');
   rewrite(f);
   writeln(f,max,' ',mp,' ',ml);
   close(f);
end;

begin
   readdata;
   solve;
   writedata;
end.