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

for the redalert problem, if i write my code as follows:

#include

using namespace std;

int main() {

int t;

cin>>t;

```
while(t--){
int n,d,h,x,flag=0;
cin>>n>>d>>h;
int sum=0,a[n];
for(int i=0; i<n; i++){
cin>>a[i];
}
for(int i=0; i<n; i++){
if(a[i]>0){
sum+=a[i];
}else if(a[i]==0 && sum>d){
sum=sum-d;
}else if(a[i]==0 && sum<=d){
sum=0;
}
if(sum>h){
flag=1;
cout<<"YES"<<endl;
break;}
}
if(flag==0){
cout<<"NO"<<endl;
}
}
return 0;
```

}

SecondCode::

#include

using namespace std;

int main() {

int t;

cin>>t;

```
while(t--){
int n,d,h,x,flag=0;
cin>>n>>d>>h;
int sum=0,a[n];
for(int i=0; i<n; i++){
cin>>a[i];
if(a[i]>0){
sum+=a[i];
}else if(a[i]==0 && sum>d){
sum=sum-d;
}else if(a[i]==0 && sum<=d){
sum=0;
}
if(sum>h){
flag=1;
cout<<"YES"<<endl;
break; }
}
if(flag==0){
cout<<"NO"<<endl;
}
}
return 0;
```

}

briefly my first code is written by taking inputs in a seperate loop and for checking conditions I used another loop, it shows as correct answer after submission but if I use only a single loop then I am getting as wrong answer after submissionâ€¦ can anyone please just clarify me why is this happeningâ€¦ In what way and how they both differ !!