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

Previous submission (verdict: Time limit exceeded on test 2):
#include <iostream>
#include <vector>
#include <set>
#include <queue>

using namespace std;

void match_and_del(multiset<int>& a, multiset<int>& b) {
    queue<int> del_queue;
    for(auto& val : b) {
        auto ia = a.find(val);
        if(ia != a.end()) {
            del_queue.push(val);
        }
    }

    while(!del_queue.empty()) {
        int val = del_queue.front(); del_queue.pop();
        auto ia = a.find(val);
        auto ib = b.find(val);
        if(ia != a.end()) {
            a.erase(ia);
            b.erase(ib);
        }
    }
}


int main() {
    int kase;
    cin >> kase;

    while(kase--) {
        int n, m;
        cin >> n >> m;
        
        int x;
        multiset<int> a, b;
        for(int i = 0; i < n; i++) {
            cin >> x;
            a.insert(x);
        }
        for(int i = 0; i < m; i++) {
            cin >> x;
            b.insert(x);
        }
        match_and_del(a, b);

        while(!a.empty() && !b.empty()) {
            int x = *b.rbegin();
            if(x > 1) {
                b.erase(b.find(x));
                // cout << "split " << x << " into " << x/2 << " and " << x-x/2 << endl;
                b.insert(x/2); b.insert(x-x/2);    
            }
            
            match_and_del(a, b);

            if(b.size() > a.size()) break;
        }

        if(a.empty() && b.empty()) cout << "YES" << endl;
        else cout << "NO" << endl;

    }
    return 0;
}

Submission (verdict: Time limit exceeded on test 7):
#include <iostream>
#include <vector>
#include <set>
#include <queue>

using namespace std;

bool match_and_del(multiset<int>& a, multiset<int>& b) {
    bool del_st = false;
    queue<int> del_queue;
    for(auto& val : b) {
        auto ia = a.find(val);
        if(ia != a.end()) {
            del_queue.push(val);
        }
    }

    while(!del_queue.empty()) {
        int val = del_queue.front(); del_queue.pop();
        // cout << "del " << val << endl;
        auto ia = a.find(val);
        auto ib = b.find(val);
        if(ia != a.end()) {
            a.erase(ia);
            b.erase(ib);
            del_st = true;
        }
    }

    return del_st;
}


int main() {
    int kase;
    cin >> kase;

    while(kase--) {
        int n, m;
        cin >> n >> m;
        
        int x;
        multiset<int> a, b;
        for(int i = 0; i < n; i++) {
            cin >> x;
            a.insert(x);
        }
        for(int i = 0; i < m; i++) {
            cin >> x;
            b.insert(x);
        }
        
        match_and_del(a, b);
        while(!a.empty() && !b.empty()) {
            int x = *b.rbegin();
            bool new_split = false;
            if(x > 1) {
                b.erase(b.find(x));
                // cout << "split " << x << " into " << x/2 << " and " << x-x/2 << endl;
                b.insert(x/2); b.insert(x-x/2);    
                new_split = true;
            }
            
            bool del_st = match_and_del(a, b);
            if(!new_split && !del_st) break;
            if(b.size() > a.size()) break;
        }

        if(a.empty() && b.empty()) cout << "YES" << endl;
        else cout << "NO" << endl;

    }
    return 0;
}