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

Previous submission (verdict: Time limit exceeded on test 3):
#include <bits/stdc++.h>
using namespace std;

#define fastIO                   \
    ios::sync_with_stdio(false); \
    cin.tie(NULL);               \
    cout.tie(NULL);              \
    cout.precision(numeric_limits<double>::max_digits10);
#define int long long

const int M = 1e9 + 7;

bool check(int n, unordered_map<int, int> &mp, int mode)
{
    queue<pair<int, int>> q;
    q.push({n, mode});

    while (!q.empty())
    {
        auto [curr, mode] = q.front();
        q.pop();

        if (mp.find(curr) != mp.end() && mp[curr] > 0)
        {
            mp[curr]--;
            if (mode == 1)
            {
                mp[curr]--;
            }
            if (mp[curr] == 0)
            {
                mp.erase(curr);
            }
            continue;
        }

        if (curr == 1 && mp.find(1) == mp.end())
        {
            return false;
        }

        if (curr & 1)
        { 
            q.push({(curr - 1) / 2, 0});
            q.push({(curr + 1) / 2, 0});
        }
        else
        {
            q.push({curr / 2, 1});
        }
    }
    return true;
}


void ved()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    unordered_map<int, int> mp;

    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
    }

    for (int i = 0; i < m; i++)
    {
        cin >> b[i];
    }

    for (int i = 0; i < m; i++)
    {
        if (!check(b[i], mp, 0))
        {
            cout << "No" << endl;
            return;
        }
    }

    if (!mp.empty())
    {
        cout << "No" << endl;
        return;
    }

    cout << "Yes" << endl;
}

int32_t main()
{
    fastIO;

    int t;
    cin >> t;
    while (t--)
    {
        ved();
    }
    return 0;
}


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

using namespace std;

#define fastIO                   \
    ios::sync_with_stdio(false); \
    cin.tie(NULL);               \
    cout.tie(NULL);              \
    cout.precision(numeric_limits<double>::max_digits10);
#define int long long

const int M = 1e9 + 7;

int powm(int x, int n)
{
    x %= M;
    if (n == 0)
        return 1;
    else if (n == 1)
        return x;
    int p = powm(x * x, n / 2);
    if (n % 2)
        return p * x % M;
    else
        return p;
}

int mod_div(int p, int q) { return p % M * powm(q, M - 2) % M; }

vector<int> fact;
int N = 1e6;

int ncr(int n, int r)
{
    if (r > n)
        return 0;
    return mod_div(fact[n], fact[n - r] * fact[r]);
}
void ved()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<m;i++){
        cin>>b[i];
    }
    map<int, int> fa, fb;
    set<int, greater<int>> pa, pb;
    
    for (auto &&i : a)
    {
        fa[i]++;
        pa.insert(i);
    }
    
    for (auto &&i : b)
    {
        fb[i]++;
        pb.insert(i);
    }
    
    while (true)
    {
        if (pa.empty() && pb.empty())
        {
            cout << "Yes" << endl;
            return;
        }
        if (pa.empty() || pb.empty())
        {
            cout << "No" << endl;
            return;
        }
        
        int mxb = *pb.begin();
        int mxa = *pa.begin();
        
        if (mxa > mxb)
        {
            cout << "No" << endl;
            return;
        }
        
        if (fa[mxb])
        {
            int mn = min(fa[mxb], fb[mxb]);
            fa[mxb] -= mn;
            fb[mxb] -= mn;
            
            if (fa[mxb] == 0)
                pa.erase(mxb);
            if (fb[mxb] == 0)
                pb.erase(mxb);
        }
        else
        {
            int sb = mxb / 2;
            int lb = (mxb + 1) / 2;
            fb[sb] += fb[mxb];
            fb[lb] += fb[mxb];
            fb[mxb] = 0;
            pb.insert(sb);
            pb.insert(lb);
            
            if (fb[mxb] == 0)
                pb.erase(mxb);
        }
    }
}



int32_t main()
{
    fastIO;

    fact.assign(N, 1);
    for (int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i % M;
    }
    
    int t = 1;
    cin >> t;
    while (t--)
    {
        ved();
    }
    return 0;
}