Cod sursa(job #53427)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 22 aprilie 2007 08:41:21
Problema Buline Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.9 kb
var a:array[1..400000] of longint;
    s,smax,n,i,st,sf,y,poz:longint;
    f,g:text;
    ok:boolean;
begin
 assign(f,'buline.in'); reset(f);
 assign(g,'buline.out'); rewrite(g);
 read(f,n);
 for i:=1 to n do begin
  read(f,a[i],y);
  if y=0 then
   a[i]:=-a[i];
 end;
 for i:=1 to n do
  a[i+n]:=a[i];
 poz:=1;
 ok:=false;
 for i:=1 to n do
  if 0<=a[i] then
   ok:=true;
 if ok then begin
  if a[1]>0 then
   s:=a[1]
  else begin
   s:=0;
   poz:=2;
  end;
  smax:=s;
  i:=1;
 end
 else begin
  smax:=-2000000001;
  for i:=1 to n do
   if smax<a[i] then begin
    smax:=a[i];
    st:=i;
    sf:=i;
   end;
 end;
 while (i<2*n) and (ok) do begin
  inc(i);
  s:=s+a[i];
  if (s>smax) and (i-poz+1<=n) then  begin
   smax:=s;
   st:=poz;
   sf:=i;
  end;
  if s<0 then begin
   poz:=i+1;
   s:=0;
  end;
 end;
 writeln(g,smax,' ',st,' ',sf-st+1);
 close(f); close(g);
end.