What is wrong with this approach...Can anyone tell me?

https://www.codechef.com/problems/REDALERT

submission link

#include
using namespace std;
#define ll long long
int main() {
long WL=0;long i=0;
ll T,N,D,H;
cin>>T;
while(T–)
{
cin>>N>>D>>H;
long A[N];
for(long i=0;i<N;i++)
{
cin>>A[i];
}

    for(long j=0;j<N;j++)
    {
       
       if(A[j]>0)
            WL+=A[j];   
            
        else {
            if(A[j]==0)  
            {
                if(WL>D)
                WL-=D;
            
                else 
                WL=0;
            }
        }

   if(WL>H)
       {
           i=1;
           break;
       }

}

   if(i==0)
           cout<<"NO"<<endl;
   else
        cout<<"YES"<<endl;

}
return 0;

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

Deciphered it; consider the test input:

2
2 1 5
3 3  
1 1 2
1 
1 Like

Okay wait …i am sharing the link of my submission @ssjgz

https://www.codechef.com/submit/BGL1215 - #4 by ssjgz :slight_smile:

Anyway, see my Edit :slight_smile:

1 Like

Can you please point out bug in my code?

You’ve got a 100% reproducible testcase - please try and debug it yourself :slight_smile:

I’ll wait 30 mins and provide a solution if you haven’t figured it out by then.

Edit:

Or not. Sigh.

1 Like

Your logic is completely correct. However, you have defined WL = 0 and i =0 outside the ‘while’ loop and you are not resetting them back to zero after printing the answer.

1 Like

You have to check the level of water i.e WL in your solution, at each iteration . Not only at the end.

submission link

Please see this and let me know my mistake…I am still unable to figure it out.

@aryan_129 Beat me to it - see his comment, above :slight_smile:

1 Like

Thank you! finally got it.

Yes…got it.

1 Like