Cod sursa(job #123677)

Utilizator TudorutzuMusoiu Tudor Tudorutzu Data 16 ianuarie 2008 23:53:33
Problema Buline Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.98 kb
var t,s:array[1..400000] of longint;
    a:array[1..200000] of longint;
    pc,l,i,j,x,n,max:integer;
    f,g:text;
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];
     end;
     for i:=1 to n-1 do a[i+n]:=a[i];
     s[1]:=a[1]; t[1]:=s[1];
     for i:=2 to n+n-1 do
     begin
          s[i]:=s[i-1]+a[i];
          if s[i]>t[i-1] then t[i]:=s[i]
                         else t[i]:=t[i-1];
     end;
     for i:=2 to n do
     begin
          if max=t[i-1]+s[n]-s[i-1] then
             if i<pc then pc:=i;
          if max<t[i-1]+s[n]-s[i-1] then
          begin
               max:=t[i-1]+s[n]-s[i-1];
               pc:=i;
               j:=i;
               repeat inc(j)
               until s[j]-s[i-1]=max;
               l:=j-i+1;
          end;
     end;
     writeln(g,max,' ',pc,' ',l);
     close(g);
end.