Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 27):
#include<bits/stdc++.h>
#include<climits>

using namespace std;

void solve();
#define ll long long
#define pii pair<int,int>
#define endl '\n'
#define ip(a,n) for(int i=0; i<n; ++i) cin>>a[i];
#define op(a,n) for(int i=0; i<n; ++i) cout<<a[i]<<" "; cout<<'\n';
#define p(x) cout<<(x)<<'\n';

const int mod = 1e9+7;


int main(){



    int t = 1;
    cin>>t;

    while(t--){
        solve();
    }

    return 0;
}




/*



*/
bool fn(unordered_map<int, int>& m, int a, int b) {
    stack<pair<int, int>> st;  // Stack to simulate recursion
    st.push({a, b});
    
    while (!st.empty()) {
   pair<int, int> top = st.top();  // Explicitly access the top pair
        st.pop();

        int x = top.first;  // First element of the pair
        int y = top.second;

        if (x == 0 || y == 0)
            return false;

        // Check for x in the map
        if (m[x] > 0) {
            --m[x];
        } else {
            int temp1 = x / 2;
            int temp2 = x - temp1;
            st.push({temp1, temp2});
        }

        // Check for y in the map
        if (m[y] > 0) {
            --m[y];
        } else {
            int temp1 = y / 2;
            int temp2 = y - temp1;
            st.push({temp1, temp2});
        }
    }
    return true;
}







void solve(){
    int n,k;
    cin>>n>>k;
    vector<int>a(n,0),b(k);
    ip(a,n);
    ip(b,k);
    unordered_map<int,int>m;
    for(auto i:a)
    m[i]++;
    sort(b.rbegin(),b.rend());
    for(int i=0;i<k;i++){
        if(m.find(b[i])!=m.end()&&m[b[i]]>0){
            --m[b[i]];
            
        }
        else {
        int temp1=b[i]/2;
        int temp2=b[i]-temp1;
      if(! fn(m,temp1,temp2)){
        cout<<"No"<<endl;
        return;
      }
        }


    }
    for(auto i:m){
        if(i.second>0){
          cout<<"No"<<endl;
        return;  
        }
    }
    cout<<"Yes"<<endl;





}


























































    

























Submission (verdict: Accepted):
#include<bits/stdc++.h>
#include<climits>

using namespace std;

void solve();
#define ll long long
#define pii pair<int,int>
#define endl '\n'
#define ip(a,n) for(int i=0; i<n; ++i) cin>>a[i];
#define op(a,n) for(int i=0; i<n; ++i) cout<<a[i]<<" "; cout<<'\n';
#define p(x) cout<<(x)<<'\n';

const int mod = 1e9+7;


int main(){



    int t = 1;
    cin>>t;

    while(t--){
        solve();
    }

    return 0;
}




/*



*/
bool fn(map<int,int>&m,int a,int b){
    if(a==0||b==0)
    return false;
    if(m.find(a)==m.end()||m[a]==0){
        int temp=a/2;
       if(! fn(m,temp,a-temp)) return false;
       
    }
    else --m[a];
   
     if(m.find(b)==m.end()||m[b]==0){
        int temp=b/2;
       if(! fn(m,temp,b-temp)) return false;
    }
    else  --m[b];
   
    return true;
    
}



void solve(){
    int n,k;
    cin>>n>>k;
    vector<int>a(n,0),b(k);
    ip(a,n);
    ip(b,k);
   map<int,int>m;
    for(auto i:a)
    m[i]++;
  

    for(int i=0;i<k;i++){
        if(m.find(b[i])!=m.end()&&m[b[i]]>0){
            --m[b[i]];   
        }
        else {
            int temp1=b[i]/2;
            int temp2=b[i]-temp1;
            if(! fn(m,temp1,temp2)){
                cout<<"No"<<endl;
                 return;
            }
        }
    }
    for(auto i:m){
        if(i.second>0){
          cout<<"No"<<endl;
            return;  
        }
    }
    cout<<"Yes"<<endl;

}