Pagini recente » Cod sursa (job #2016633) | Profil M@2Te4i | Monitorul de evaluare | Cod sursa (job #857524) | Cod sursa (job #2073674)
#include <iostream>
#include <fstream>
#include <climits>
#include <cstring>
using namespace std;
ifstream f("buline.in");
ofstream g("buline.out");
int main()
{
long int N, i, b, x, Smax, p, l=1, vf=0, mx, B, lmax=1;
f>>N;
long int V[N], S[N], MAX[N], P[N];
S[0]=0;
for(i=1; i<=N; i++)
{
f>>x>>b;
if(b){S[i]=S[i-1]+x;
V[i]=x;}
else {S[i]=S[i-1]-x;
V[i]=-x;}
}
MAX[1]=S[1];
for(i=2; i<=N; i++)
if(S[i]>MAX[i-1])MAX[i]=S[i];
else MAX[i]=MAX[i-1];
for(i=1; i<=N; i++) if (S[i]==MAX[i]){B=i;
break;}
for(i=1; i<=N; i++)if(V[i]>0)P[++vf]=i;
vf=0; Smax=0; mx=0; lmax=INT_MIN;
for(i=1; i<=N; i++)
{if(Smax+V[i]<0){i=P[++vf];
Smax=V[i];
l=1;
p=i;}
else {Smax+=V[i];
l++;}
if(Smax>mx){mx=Smax;
lmax=l;}
}
g<<mx<<" "<<lmax<<" "<<p;
return 0;
}