Pagini recente » Cod sursa (job #987230) | Cod sursa (job #8207) | Cod sursa (job #724080) | Cod sursa (job #1001918) | Cod sursa (job #53427)
Cod sursa(job #53427)
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.