Two different codes based on same logic 1 giving ac the other WA

there are two different codes based on same logic written on the same problem .The first one gives me wa while 2nd one gives ac but The 1st one is more optimised that the second then why is the 1st program giving me wrong answer.here is 1st code for this question .

#include <bits/stdc++.h>
using namespace std;
const int N=(int)100005;
const long int maxa=(long int)10000007;
long int arr[N],b[N],p[maxa];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    memset(arr,0,sizeof arr);
    memset(b,0,sizeof b);
    memset(p,0,sizeof p);
    long int t,n,k,m,wh;
    cin>>t;
    while(t--){
        cin>>n>>k>>m;
        for(int i=0;i<n;i++)
        cin>>arr[i];
        for(int j=0;j<n;j++){
            cin>>b[j];
        }
        for(int i=0;i<k;i++){
        cin>>wh;
        p[wh]++;
        }
        for(int j=0;j<m;j++){
        cin>>wh;
        p[wh]++;
       }
       for(int i=0;i<n;i++){
           int why=arr[i]-b[i];
           if(p[why]>0)
           {
               if(p[why]>0 && b[i] +why <=arr[i])
               {
                   p[why]--; arr[i]=arr[i]-why;
               }
              
           }
           else if(p[why]==0)
       {
           while(p[why]==0)
           {  why--;
            if(why<0)
            break;
           }
       
       if(why>=1)
{
           if(p[why]>0 && why + b[i] <=arr[i])
           {
               p[why]--;
               arr[i]=arr[i]-why;
               
           }
}
       
    }}
    
       int ans=0;
       for(int i=0;i<n;i++){
           
           ans=ans+arr[i]-b[i];
       }
       cout<<ans<<endl;
    }
	// your code goes here
	return 0;
}

and 2nd code is this
pls help.

Put the following lines:

memset(arr,0,sizeof arr);

memset(b,0,sizeof b);

memset(p,0,sizeof p);

inside the
while(t–){…}
loop.

This was the difference.

1 Like

What are differences in your first and second code? If you mention that, it will be easier to help you.