Cod sursa(job #213838)

Utilizator AndreyPAndrei Poenaru AndreyP Data 11 octombrie 2008 20:19:11
Problema Buline Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include<stdio.h>
int v[200005],smax,pmax,lmax,n;
void citire()
{
	scanf("%d",&n);
	int x,c;
	for(int i=1; i<=n; i++)
	{
		scanf("%d%d",&x,&c);
		if(c)
			v[i]=x;
		else
			v[i]=-x;
	}
}
void rezolva()
{
	int n1=n<<1;
	int s=0,p=1,l=0;
	for(int i=1,k=0; k<=n1 && l!=n; k++,i==n ? i=1 : i++)
	{
		s+=v[i];
		l++;
		if(s>smax)
		{
			smax=s;
			pmax=p;
			lmax=l;
		}
		else
		if(smax==s && p<pmax)
		{
			pmax=p;
			lmax=l;
		}
		if(s<0)
		{
			s=0;
			l=0;
			if(i==n)
				p=1;
			else
				p=i+1;
		}
	}
}
int main()
{
	freopen("buline.in","r",stdin);
	freopen("buline.out","w",stdout);
	pmax=1<<30;
	smax=-pmax;
	citire();
	rezolva();
	if(lmax==n)
		printf("%d 1 %d\n",smax,n);
	else
		printf("%d %d %d\n",smax,pmax,lmax);
	return 0;
}