Cod sursa(job #38446)
#include<fstream.h>
int main(){
ifstream fin("buline.in");
ofstream fout("buline.out");
unsigned long N,p,l;
long int a[200000];
long int S=0,max=-200000;
long x,y;
fin>>N;
for (unsigned long i=0;i<N;i++){
fin>>x>>y;
if (y==0)
a[i]=(-1)*x;
else
a[i]=x;}
for (unsigned long j=0;j<N;j++){
S=0;
for (unsigned long y=j;y<N;y++){
S+=a[y];
if (S>max){
max=S;
p=j+1;
l=y-j;}}
for (unsigned long t=0;t<j;t++){
S+=a[t];
if (S>max){
max=S;
p=j+1;
l=N-j+1+t;} }}
fout<<max<<" "<<p<<" "<<l;
fin.close();
fout.close();
return 0;
}