Cod sursa(job #25575)

Utilizator bacerandreiBacer Andrei bacerandrei Data 4 martie 2007 12:59:30
Problema Buline Scor 50
Compilator cpp Status done
Runda preONI 2007, Runda 3, Clasa a 9-a si gimnaziu Marime 0.69 kb
#include<fstream.h>
long a[200000],b[200000],n,i,j,s,ic,lg,pc,max,gasit;
int main()
{
  ifstream f("buline.in");
  ofstream g("buline.out");
   f>>n;
  for(i=1;i<=n;i++)
   f>>a[i]>>b[i];
  f.close();
   i=1;
    while(i<=n)
     {
      if(b[i]==0)
       i++;
      j=i;
       gasit=0;
      s=0; ic=0;
       while(!gasit)
	{
	 if(s>=0&&b[j]==1)
	  {
	   s=s+a[j];
	   ic++;
	  }
	 if(s>=0&&b[j]==0)
	  {
	   s=s-a[j];
	   ic++;
	  }
	   if(s<0)
	    gasit=1;
	 if(s>max)
	  {
	   max=s;
	   pc=i;
	   lg=ic;
	  }
	  j++;
	   if(j==n+1)
	    j=1;
	   if(j==i)
	    gasit=1;
	 }
	i++;
       }
     g<<max<<" "<<pc<<" "<<lg;
    g.close();
  return 0;
}