Pagini recente » Cod sursa (job #2356079) | Cod sursa (job #179674) | Cod sursa (job #1870989) | Cod sursa (job #217281) | Cod sursa (job #26459)
Cod sursa(job #26459)
var v,x,s,t,t1:array[0..400000]of longint;
i,smax,sc,p,j,l,n,b,c,pmax,lmax,max,poz,lung:longint;
begin
assign(input,'buline.in');reset(input);
assign(output,'buline.out');rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(b,c);
if c=1 then
v[i]:=b
else
v[i]:=-b;
v[n+i]:=v[i];
end;
sc:=v[1];
smax:=v[1];
p:=1;l:=1;
for i:=2 to 2*n do
begin
if l<n then
if sc+v[i]>v[i]
then begin
sc:=sc+v[i];
inc(l);
end
else
begin
sc:=v[i];
p:=i;
l:=1;
end;
if sc>smax then
begin
smax:=sc;
pmax:=p;
lmax:=l;
end;
end;
s[1]:=v[1];
s[n+1]:=s[1];
for i:=2 to 2*n do
s[i]:=s[i-1]+v[i];
t[1]:=s[1];
t1[1]:=1;
for i:=2 to n do
begin
t[i]:=t[i-1];
t1[i]:=t1[i-1];
if s[i]>t[i] then
begin
t[i]:=s[i];
t1[i]:=i;
end;
end;
max:=0;poz:=0;lung:=0;
for i:=1 to n do
begin
if t[i-1]+s[n]-s[i-1]>max then
begin
max:=t[i-1]+s[n]-s[i-1];
j:=i;
lung:=n-i+1+t1[i-1];
{ while s[j]-s[i-1]<>max do
begin
inc(j);
inc(lung);
end;}
poz:=i;
end;
end;
if max>smax then
writeln(max,' ',poz,' ',lung)
else
writeln(smax,' ',pmax,' ',lmax);
close(input);close(output);
end.