Cod sursa(job #25047)

Utilizator TudorutzuMusoiu Tudor Tudorutzu Data 4 martie 2007 10:10:20
Problema Buline Scor 0
Compilator fpc Status done
Runda preONI 2007, Runda 3, Clasa a 9-a si gimnaziu Marime 1.34 kb
var f,g:text;
    n,i,s_cr,s,l,x,l_cr,pz_cr,p:integer;
    a:array[1..30000] of integer;
begin
     assign(f,'buline.in'); reset(f);
     assign(g,'buline.out'); rewrite(g);
     readln(f,n);
     for i:=1 to n do
     begin
          readln(f,a[i],x);
          if x=0 then a[i]:=a[i]*(-1);
     end;
     for i:=1 to n-1 do a[i+n]:=a[i];
     pz_cr:=1; s_cr:=0;  l:=0; l_cr:=1;
     for i:=1 to 2*n-1 do
     begin
          if l_cr<=n then
          begin
               s_cr:=s_cr+a[i];
               if s_cr>s then
               begin
                    s:=s_cr;
                    p:=pz_cr;
                    L:=l_cr;
               end;
               if s_cr=s then
               begin
                    if p>pz_cr then
                    begin
                         p:=pz_cr;
                         L:=l_cr;
                    end;
                    if p=pz_cr then
                         if L>l_cr then L:=l_cr;
               end;
               inc(l_cr);
          end;
          if s_cr<0 then
          begin
               s_cr:=0;
               pz_cr:=i+1;
               l_cr:=1;
          end;
          if l_cr>n then
          begin
               s_cr:=0;
               pz_cr:=i;
               l_cr:=1;
          end;
     end;
     write(g,s,' ',p,' ',L);
     close(g);
end.