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

Previous submission (verdict: Memory 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;

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]);
}

bool check(int n, unordered_map<int, int> &mp,int mode) {
    if (mp.find(n) != mp.end() && mp[n] > 0) {
        mp[n]--;
        if(mode==1){
          mp[n]--;  
        }
        if (mp[n] == 0) {
            mp.erase(n);  
        }
        return true;
    }
    if (n == 1 and mp.find(1) == mp.end()) {
        return false;
    }
    if (n & 1) { // If n is odd
        bool t1 = check((n - 1) / 2, mp,0);
        bool t2 = check((n + 1) / 2, mp,0);
        return t1 && t2;
    }
    return check(n / 2, mp,1); 
}

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];
    }
    sort(b.begin(),b.end());
    for (int i = 0; i < m; i++) {
        if (!check(b[i], mp,0)) {
            cout << "No" << endl;
            return;
        }
    }
    if(mp.size()){
        cout<<"No"<<endl;
        return;
    }

    cout << "Yes" << endl;

}


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;
}

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) {
    stack<pair<int, int>> st;
    st.push({n, mode});

    while (!st.empty()) {
        int curr=st.top().first;
        int mode=st.top().second;
       
        st.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) { 
            st.push({(curr + 1) / 2, 0});
            st.push({(curr - 1) / 2, 0});
        } else {
            st.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];
    }

    sort(b.begin(), b.end());

    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;
}