Pagini recente » Cod sursa (job #2379855) | Cod sursa (job #2194128) | Cod sursa (job #2247717) | Cod sursa (job #2876608) | Cod sursa (job #2119463)
#include <fstream>
using namespace std;
ifstream f("buline.in");
ofstream g("buline.out");
int n,l1,l2,lmin,lmax,stmin,stmax,st1,st2,S1,S2,Smax,Smin,Stot,i;
int a,p,v[400001];
int main()
{ f>>n;l1=0;l2=0;
for(i=1;i<=n;i++){
f>>a>>p;
if(p==0){
v[i]=-a;
v[i+n]=v[i];
}
else{
v[i]=a;
v[i+n]=v[i];
}
}
for(i=1;i<=2*n;i++){
if(S1+v[i]>=v[i]){
S1=S1+v[i];
l1++;
if(l1>n){
S1=v[i];
st1=i;
l1=1;
}
}
else{
S1=v[i];
st1=i;
l1=1;
}
if(S2+v[i]<=v[i]){
S2=S2+v[i];
l2++;
if(l2>n){
S2=v[i];
l2=1;
st2=i;
}
}
else{
S2=v[i];
st2=i;
l2=1;
}
if(S2<Smin){
Smin=S2;
stmin=st2;
lmin=l2;
}
if(S1>Smax){
Smax=S1;
stmax=st1;
lmax=l1;
}
if(i<=n)
Stot=Stot+v[i];
}
if(Smax>Stot-Smin){
if(stmax>n)
stmax=stmax-n;
g<<Smax<<' '<<stmax<<' '<<lmax;
}
else{
if(Stot-Smin>Smax){
if(stmin+lmin>n)
stmin=stmin+lmin-n;
else
stmin=stmin+lmin;
g<<Stot-Smin<<' '<<stmin<<' '<<n-lmin;
}
if(Stot-Smin==Smax){
if(stmin+lmin>n)
stmin=stmin+lmin-n;
else
stmin=stmin+lmin;
if(stmax>n)
stmax=stmax-n;
if(stmin<stmax)
g<<Stot-Smin<<' '<<stmin<<' '<<n-lmin;
else
if(stmin>stmax)
g<<Smax<<' '<<stmax<<' '<<lmax;
else
if(stmin==stmax){
if(lmax<lmin)
g<<Smax<<' '<<stmax<<' '<<lmax;
else
g<<Stot-Smin<<' '<<stmin<<' '<<n-lmin;
}
}
}
return 0;
}