Cod sursa(job #50340)

Utilizator marius21Marius Petcu marius21 Data 7 aprilie 2007 15:01:14
Problema Buline Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.99 kb
var a:array[0..50000] of longint;
col,pi,pf,pi1,pf1,l,l1,x,p,n,k,i,j:longint;
stot,s,smax,smin:int64;
f,g:text;
begin
assign(f,'buline.in');
assign(g,'buline.out');
reset(f);
rewrite(g);
read(f,n);
s:=-1;
pi:=1;
smax:=-maxlongint;
for i:=1 to n do begin
	read(f,a[i],col);
	if col=0 then a[i]:=-a[i];
	if a[i]>s+a[i] then begin
   	s:=0;
      pi:=i
   end;
	inc(s,a[i]);
   if s>smax then begin
   	smax:=s;
      pf:=i;
      end;
   inc(stot,a[i]);
   end;
s:=1;
smin:=maxlongint;
for i:=1 to n do begin
	if a[i]<s+a[i] then begin
   	s:=0;
      pf1:=i
   end;
	inc(s,a[i]);
   if s<smin then begin
   	smin:=s;
      pi1:=i;
      end;
   end;
l:=pi-pf+1;
dec(pf1);
if pf1=0 then pf1:=n;
inc(pi1);
if pi1=n+1 then pi1:=1;
l1:=pf1+n-pi1+1;
if (stot-smin>smax) or ((stot-smin=smax) and ((pi1<pi) or ((pi=pi1) and (pf1<pf)) ) ) then begin
	smax:=stot-smin;
   pi:=pi1;
   pf:=pf1;
   l:=l1;
   end;
writeln(g,smax,' ',pi,' ',l);
close(f);
close(g);
end.