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


Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#ifndef ONLINE_JUDGE
#include "debugg.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
//change less<int> to less_equal<int> to make it a multiorderedset
//to erase from multiorderedset do s.erase(s.upperbound(a[i]))
//find_by_order(k)->iterator of the kth element
//order_of_key(k)->number of elements smaller than k
//ncr snip has modinv as well
#define float double
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl '\n'
#define p(x) cout<<x<<endl
#define c(x) cout<<x<<" "
#define int long long
#define pb push_back
#define scanarr int a[n];for(int i=0;i<n;i++)cin>>a[i]
#define scannarr(a) int a[(int)(a)];for(int i=0;i<(int)(a);i++)cin>>a[i]
#define fr(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define frr(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int,int>
#define vpi vector<pair<int,int>>
#define frr(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define mod 1000000007

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

void solve() {
    int n,m;
    cin>>n>>m;
    vi a(n),b(m);
    fr(i,0,n)cin>>a[i];
    fr(i,0,m)cin>>b[i];
    map<int,int>m1,m2;
    set<int>s,s1;
    fr(i,0,n){
        m1[a[i]]++;
        s1.insert(a[i]);
    }
    fr(i,0,m){
        m2[b[i]]++;
        s.insert(b[i]);
    }
    set<int>del;
    for(auto x:s){
        int k=max((int)0,m1[x]-m2[x]);
        int g=m1[x]-k;
        m2[x]-=g;
        if(m2[x]==0)del.insert(x);
        m1[x]=k;
        if(k==0)s1.erase(x);
    }
   
    for(auto x:del)s.erase(x);
   // function <void(int,int)> dfs = [&] (int r,int c) -> void
   int check=0;
//    p("hi");
    function<void(int,int)> solve = [&] (int x,int f) -> void{
        if(x==0)return;
        if(x==1){
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            if(m1[x]==0)s1.erase(x);
            if(f!=0){
                check=1;
            }
            return;
        }
        if(x%2==0){
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            // c(x);p(m1[x]);
            if(m1[x]==0)s1.erase(x);
            if(f==0)return;
           solve(x/2,f*2);
            
        }
        else{
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            //  c(x);p(m1[x]);
            if(m1[x]==0)s1.erase(x);
            if(f==0)return;
            solve(x/2,f);solve((x/2)+1,f);
        }
    };
   
    for(auto x:s){
        solve(x,m2[x]);
        // c("check ");c(x);p(check);
        if(check==1){p("No");return;}
    }
    // solve(3,m2[3]);
    // p((*s1.begin()));
    if(s1.size()==0)p("Yes");
    else p("No");
    


    
}
signed main() {
    fast;
    int t=1;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}