Problem: 2061D - Kevin and Numbers
Language: C++23 (GCC 14-64, msys2)

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define double ld
#define int ll
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define vl vector<ll>
#define vvl vector<vl>
#define vb vector<bool>
#define vvb vector<vb>
#define vc vector<char>
#define vvc vector<vc>
#define vs vector<string>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define vvpll vector<vector<pair<ll, ll>>>
#define umll unordered_map<ll, ll>
#define umcl unordered_map<char, ll>
#define umlc unordered_map<ll, char>
#define fr(i, a, n) for (ll i = (a); i < (n); i++)
#define rfr(i, n, a) for (ll i = n; i >= a; i--)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define srt(v) sort(all(v))         // sort
#define mxe(v) *max_element(all(v)) // find max element in vector
#define mne(v) *min_element(all(v)) // find min element in vector
#define rev(v) reverse(all(v))
// #define sum_arr(v) accumulate(all(v),0)
#define pq_max priority_queue<int>
#define pq_min priority_queue<int, vector<int>, greater<int>>
#define yes cout << "YES" << "\n"
#define no cout << "NO" << "\n"

// function

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll ncr(ll n, ll r)
{
    ll res = 1;
    fr(i, 0, r)
    {
        res = res * (n - i);
        res = res / (i + 1);
    }
    return res;
}

int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}

vector<int> simpleSieve(int limit)
{
    vector<bool> is_prime(limit + 1, true);
    vector<int> primes;
    for (int p = 2; p <= limit; ++p)
    {
        if (is_prime[p])
        {
            primes.push_back(p);
            for (int multiple = p * p; multiple <= limit; multiple += p)
            {
                is_prime[multiple] = false;
            }
        }
    }
    return primes;
}

int primeinrange(int L, int R)
{
    int limit = sqrt(R);
    vector<int> primes = simpleSieve(limit);
    vector<bool> is_prime(R - L + 1, true);
    for (int prime : primes)
    {
        int start = max(prime * prime, (L + prime - 1) / prime * prime);
        for (int j = start; j <= R; j += prime)
        {
            is_prime[j - L] = false;
        }
    }
    if (L == 1)
        is_prime[0] = false;
    int c = 0;
    for (int i = 0; i <= R - L; ++i)
    {
        if (is_prime[i])
        {
            c++;
        }
    }
    return c;
}

int mod_exp(int base, int exp, int mod)
{
    int result = 1;
    while (exp > 0)
    {
        if (exp % 2 == 1)
        {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        exp /= 2;
    }
    return result;
}

// Code by harshsingh9222

int f(unordered_map<int,int>&mpp,int a,int b,int &c,int &sp,int n,int m)
{
    sp++;
    if(sp>n-m) return 0;
    if(a==0 || b==0) return 0;
    int op=0;
    if(mpp[a]>0)
    {
        op++;
        mpp[a]--;
    }
    else{
        if(f(mpp,a/2,(a+1)/2,c,sp,n,m))
        op++;
        else{
            return 0;
        }
    }
    if(mpp[b]>0)
    {
        op++;
        mpp[b]--;
    }
    else{
        if(f(mpp,b/2,(b+1)/2,c,sp,n,m))
        op++;
        else{
            return 0;
        }
    }
    if(op==2)
    {
        c++;
        return 1;
    }
    return 0;
}

void solve()
{
    int n,m;
    cin>>n>>m;
    unordered_map<int,int>mpp;
    vl a(n),b(m);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        mpp[a[i]]++;
    }
    for(int i=0;i<m;i++)
    {
        cin>>b[i];
    }
    int cnt=0,sp=0;
    for(int i=0;i<m;i++)
    {
        if(mpp[b[i]]>0)
        {
            mpp[b[i]]--;
        }
        else if(n==m)
        {
            no;
            return;
        }
        else
        {
            int c=0;
            int k=f(mpp,b[i]/2,(b[i]+1)/2,c,sp,n,m);
            if(c==0 || !k)
            {
                no;
                return;
            }
            cnt+=c;
        }
    }
    if(cnt==n-m)
    {
        yes;
    }
    else{
        no;
    }
}

signed main()
{
    ios_base ::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;

    int t;
    cin >> t;
    // t=1;

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

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define double ld
#define int ll
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define vl vector<ll>
#define vvl vector<vl>
#define vb vector<bool>
#define vvb vector<vb>
#define vc vector<char>
#define vvc vector<vc>
#define vs vector<string>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define vvpll vector<vector<pair<ll, ll>>>
#define umll unordered_map<ll, ll>
#define umcl unordered_map<char, ll>
#define umlc unordered_map<ll, char>
#define fr(i, a, n) for (ll i = (a); i < (n); i++)
#define rfr(i, n, a) for (ll i = n; i >= a; i--)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define srt(v) sort(all(v))         // sort
#define mxe(v) *max_element(all(v)) // find max element in vector
#define mne(v) *min_element(all(v)) // find min element in vector
#define rev(v) reverse(all(v))
// #define sum_arr(v) accumulate(all(v),0)
#define pq_max priority_queue<int>
#define pq_min priority_queue<int, vector<int>, greater<int>>
#define yes cout << "YES" << "\n"
#define no cout << "NO" << "\n"

// function

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll ncr(ll n, ll r)
{
    ll res = 1;
    fr(i, 0, r)
    {
        res = res * (n - i);
        res = res / (i + 1);
    }
    return res;
}

int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}

vector<int> simpleSieve(int limit)
{
    vector<bool> is_prime(limit + 1, true);
    vector<int> primes;
    for (int p = 2; p <= limit; ++p)
    {
        if (is_prime[p])
        {
            primes.push_back(p);
            for (int multiple = p * p; multiple <= limit; multiple += p)
            {
                is_prime[multiple] = false;
            }
        }
    }
    return primes;
}

int primeinrange(int L, int R)
{
    int limit = sqrt(R);
    vector<int> primes = simpleSieve(limit);
    vector<bool> is_prime(R - L + 1, true);
    for (int prime : primes)
    {
        int start = max(prime * prime, (L + prime - 1) / prime * prime);
        for (int j = start; j <= R; j += prime)
        {
            is_prime[j - L] = false;
        }
    }
    if (L == 1)
        is_prime[0] = false;
    int c = 0;
    for (int i = 0; i <= R - L; ++i)
    {
        if (is_prime[i])
        {
            c++;
        }
    }
    return c;
}

int mod_exp(int base, int exp, int mod)
{
    int result = 1;
    while (exp > 0)
    {
        if (exp % 2 == 1)
        {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        exp /= 2;
    }
    return result;
}

// Code by harshsingh9222

int f(unordered_map<int,int>&mpp,int a,int b,int &c,int &sp,int n,int m)
{
    sp++;
    if(sp>n-m) return 0;
    if(a==0 || b==0) return 0;
    int op=0;
    if(mpp[a]>0)
    {
        op++;
        mpp[a]--;
    }
    else{
        if(f(mpp,a/2,(a+1)/2,c,sp,n,m))
        op++;
        else{
            return 0;
        }
    }
    if(mpp[b]>0)
    {
        op++;
        mpp[b]--;
    }
    else{
        if(f(mpp,b/2,(b+1)/2,c,sp,n,m))
        op++;
        else{
            return 0;
        }
    }
    if(op==2)
    {
        c++;
        return 1;
    }
    return 0;
}

void solve()
{
    int n,m;
    cin>>n>>m;
    vl a(n),b(m);
    priority_queue<int>pq_a;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        pq_a.push(a[i]);
    }
    priority_queue<int>pq_b;
    for(int i=0;i<m;i++)
    {
        cin>>b[i];
        pq_b.push(b[i]);
    }
    int cnt=0;
    while(!pq_a.empty() && !pq_b.empty() && cnt<=n-m)
    {
        int b_top=pq_b.top();
        int a_top=pq_a.top();
        if(a_top==b_top)
        {
            pq_a.pop();
            pq_b.pop();
        }
        else if(a_top<b_top)
        {
            pq_b.pop();
            if(b_top%2)
            {
                pq_b.push(b_top/2);
                pq_b.push((b_top+1)/2);
                cnt++;
            }
            else{
                pq_b.push(b_top/2);
                pq_b.push(b_top/2);
                cnt++;
            }
        }
        else{
            no;
            return;
        }
    }
    if(pq_a.empty() && pq_b.empty())
    {
        yes;
    }
    else{
        no;
    }
}

signed main()
{
    ios_base ::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;

    int t;
    cin >> t;
    // t=1;

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