Pagini recente » Cod sursa (job #2325559) | Cod sursa (job #2539634) | Cod sursa (job #2554474) | Cod sursa (job #73126)
Cod sursa(job #73126)
#include<fstream.h>
long a[400000],x,y,N,S,in,lg,max=-10211431,b[400000];
void citire(){
ifstream fin("buline.in");
fin>>N;
for (long i=0;i<N;i++){
fin>>x>>y;
if (y==0)
a[i]=(-1)*x;
else
a[i]=x; }
fin.close();
}
void maxim(){
long S1=0,max1=-1254320;
for (long c=0;c<N;c++)
{ S1+=a[c];
b[c]=S1;
}
int lg1=0;
for (long i=0;i<N;i++){
S+=a[i];
if (S>max){
max=S;
lg=lg1;
in=i-lg+1;}
if (S<0){
S=0;
lg1=0;}
lg1++;}
long sf;
if (in+lg==N){
for (long h=0;h<in;h++)
if (b[h]>max1){
max1=b[h];
sf=h;}
if (max1>0){
max+=max1;
lg+=sf; }}
}
int main(){
ofstream fout("buline.out");
citire();
maxim();
fout<<max<<" "<<in+1<<" "<<lg+1<<"\n";
fout.close();
return 0;
}