Cod sursa(job #29611)

Utilizator ProtomanAndrei Purice Protoman Data 9 martie 2007 17:39:48
Problema Buline Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.46 kb
var a:array[0..200000]of integer; x,i,n:longint; sf,ic,v,b,pz,sc,sm,s,p:longint; f1,f2:text;
begin
        assign(f1,'buline.in');
        reset(f1);
        assign(f2,'buline.out');
        rewrite(f2);
        read(f1,n);
        for i:=1 to n do begin
                read(f1,a[i]);
                read(f1,x);
                if x=0 then a[i]:=a[i]*(-1);
                s:=s+a[i];
        end;
        a[0]:=a[n];
        sm:=a[1];
        sc:=sm;
        pz:=1;
        ic:=1;
        sf:=1;
        for i:=2 to 2*n do begin
                if sc>0 then begin sc:=a[i mod n]; pz:=i; end
                        else sc:=sc+a[i mod n];
                if sc<sm then begin
                        sm:=sc;
                        ic:=pz;
                        sf:=i;
                        end;
                if i mod n=ic-1 then sc:=0;
        end;
        v:=sf; b:=ic;
        sc:=a[1];
        p:=sm;
        for i:=2 to 2*n do begin
                if sc<0 then begin sc:=a[i mod n]; pz:=i; end
                        else sc:=sc+a[i mod n];
                if sc>sm then begin
                        sm:=sc;
                        ic:=pz;
                        sf:=i;
                        end;
                if i mod n=ic-1 then sc:=0;
        end;
        if s-p>sm then begin sm:=s-p;
        write(f2,sm,' ',sf+1,' ',n-v+b-1); end
        else write(f2,sm,' ',ic,' ',sf-ic+1);
        close(f1);
        close(f2);
end.