I am trying to solve this problem of codechef
below is my approach I am getting wrong and in sub task 1 & tle in sub task 2 can you please help me with this
#include <bits/stdc++.h>
using namespace std;
bool enough_people(int b,intc,int ppl,int bmax, int cmax)
{
int i=0;
int j=0;
int point=0;
while((i<bmax) && (j<cmax))
{
if(point==b[i])
{
if(ppl>=b[i+1])
{
ppl= ppl-b[i+1];
i=i+2;
}
else
{
return 0;
}
}
if(point==c[j])
{
if(ppl>=c[j+1])
{
ppl=ppl+c[j+2];
j=j+3;
}
}
point++;
}
if(j==cmax)
{
int buff=0;
while(i<bmax)
{
buff=buff+b[i+1];
i=i+2;
}
if(ppl>=buff)
return 1;
else
return 0;
}
return 1;
}
int minpeople(int *b, int *c, int max,int bmax,int cmax)
{
int ans=0;
int s=0;
int e=max;
int mid=0;
while(s<=e)
{
mid = (s+e)/2;
if(enough_people(b,c,mid,bmax,cmax))
{
ans = mid;
e=mid-1;
}
else
{
s=mid+1;
}
}
return ans;
}
int main(){
#ifndef ONLINE_JUDGE
freopen(“input.txt”,“r”,stdin);
freopen(“out.txt”,“w”,stdout);
#endif
int T;
cin>>T;
while(T--)
{
int x,bmax,cmax;
x=bmax=cmax=0;
int Max_people=0;
cin>>x;
cin>>bmax;
int b[2*bmax];
for(int i=0;i<2*bmax;i++)
{
cin>>b[i];
}
cin>>cmax;
int c[3*cmax];
for(int j=0;j<3*cmax;j++)
{
cin>>c[j];
}
for(int i=1;i<2*bmax;i+=2)
{
Max_people+=b[i];
}
if(cmax==0)
{
cout<<(Max_people+1)<<endl;
}
else
{
cout<<(minpeople(b,c,Max_people,2*bmax,3*cmax)+1)<<endl;
}
}
return 0;
}