GAME_11 (question asked in Starters 132)

this is my code for the question , i want to know why i am getting tle.`#include
include
include

using namespace std;

int main() {
int t;
cin>>t;
while(t–){
int m,n;
cin>>m;
cin>>n;
if(m<4 || n<4 || m+n<11){
cout<<-1<<endl;
continue;
}
vector bat,bow;
for(int i=0;i<m;i++){
int x;
cin>>x;
bat.push_back(x);
}
for(int i=0;i<n;i++){
int x;
cin>>x;
bow.push_back(x);
}

    sort(bow.begin(),bow.end());
    sort(bat.begin(),bat.end());
    
    long long ans=0;

    int cnt=0;

    for(int i=1 ; i<=4 ; i++)
    {
        ans+=bat[m-i];
        ans+=bow[n-i];
    }
    
    cout<<"first four"<<ans<<endl;

    int i=m-5 , j=n-5 , c=0;

    while(i>=0 && j>=0 && c<3)
    {
        if(bat[i]>=bow[j])
        {
            ans+=bat[i];
            i--;
        }

        else
        {
            ans+=bow[j];
            j--;
        }

        c++;
    }
    
    // cout<<"c is "<<c<<endl;

    if(c<3)
    {
        if(i<0)
        {
            while(j>=0 && c<3)
            {
                ans+=bow[j];
                j--;
                c++;
            }
        }

        else if(j<0)
        {
            while(i>=0 && c<3)
            {
                ans+=bat[i];
                i--;
                c++;
            }
        }
        
        else
        {
            break;
        }
    }

    if(c<3)
    {
        cout<<-1<<endl;
        continue;
    }

    cout<<ans<<endl;
    
}
return 0;

}`

This is a corrected and commented version of your code.
You had two issues:

  1. You were continuing before time. You must complete the input before taking a decision, even if you already know the answer.
  2. You were printing something weird:

cout<<“first four”<<ans<<endl;

This is the code. It was overally good.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        int m,n;
        cin>>m;
        cin>>n;
        
        vector<int> bat,bow;
        for(int i=0;i<m;i++){
            int x;
            cin>>x;
            bat.push_back(x);
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            bow.push_back(x);
        }

        if(m<4 || n<4 || m+n<11){
            cout<<-1<<endl;
            continue;
            /*  This was wrong.
                You were continuing before time.
                You must complete the input before
                taking a decision.
            */
        }

        sort(bow.begin(),bow.end());
        sort(bat.begin(),bat.end());
    
        long long ans=0;
        int cnt=0;

        for(int i=1 ; i<=4 ; i++)
        {
            ans+=bat[m-i];
            ans+=bow[n-i];
        }
    
        //cout<<"first four"<<ans<<endl;
        /*
            Why were you printing this?
        */
        
        int i=m-5 , j=n-5 , c=0;
        while(i>=0 && j>=0 && c<3)
        {
            if(bat[i]>=bow[j])
            {
                ans+=bat[i];
                i--;
            }
            else
            {
                ans+=bow[j];
                j--;
            }
    
            c++;
        }
    
        // cout<<"c is "<<c<<endl;

        if(c<3)
        {
            if(i<0)
            {
                while(j>=0 && c<3)
                {
                    ans+=bow[j];
                    j--;
                    c++;
                }
            }
    
            else if(j<0)
            {
                while(i>=0 && c<3)
                {
                    ans+=bat[i];
                    i--;
                    c++;
                }
            }
            
            else
            {
                break;
            }
        }
    
        if(c<3)
        {
            cout<<-1<<endl;
            continue;
        }
    
        cout<<ans<<endl;
    
    }
    return 0;
}