Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)

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

int main() {
    int t; cin >> t;
    while (t--) {
        int n, m, add; cin >> n >> m;
        multiset<int> a, b;

        for (int i = 0; i < n; i++) {
            cin >> add;
            a.insert(add);
        }
        for (int i = 0; i < m; i++) {
            cin >> add;
            b.insert(add);
        }

        while (!b.empty() && !a.empty()) {
            if (*a.begin() > *b.begin()) break;

            for (auto it = a.begin(); it != a.end();) {
                auto found = b.find(*it);
                if (found != b.end()) {
                    b.erase(found);
                    it = a.erase(it);
                } else {
                    ++it;
                }
            }

            if (b.empty()) break;

            auto it = b.begin();
            while (it != b.end() && *it == 1) {
                ++it;
            }
            if (it == b.end()) break; 

            int primul = *it;
            b.erase(it);
            b.insert(primul / 2);
            b.insert(primul / 2 + (primul % 2));
        }
        
        if(a.empty() && b.empty()){
            cout << "YES\n";
        }
        else{
            cout << "NO\n";
        }

    }
}


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
using namespace std;

int main() {
    int t; 
    cin >> t;
    while (t--) {
        int n, m, add, numar; 
        cin >> n >> m;
        multiset<int> a, b;

        for (int i = 0; i < n; i++) {
            cin >> add;
            a.insert(add);
        }

        bool is_valid = true;
        for (int i = 0; i < m; i++) {
            cin >> numar;
            if(n == 1 && m == 1 && add != numar){
                cout << "No\n";
                is_valid = false;
                continue;
            }

            if (a.empty() || numar < *a.begin()) {
                cout << "No\n";
                is_valid = false;
                break;
            }

            if (a.count(numar) > 0) {
                a.erase(a.find(numar));
            } 
            else {
                b.insert(numar);
                while (!b.empty() && *b.rbegin() > 1) {
                    auto it = b.begin();
                    while (it != b.end() && *it == 1) {
                        ++it;
                    }

                    if (it == b.end()) {
                        cout << "No\n";
                        is_valid = false;
                        break; 
                    }

                    int primul = *it;
                    b.erase(it);
                    b.insert(primul / 2);
                    b.insert(primul / 2 + (primul % 2));

                    for (auto it_b = b.begin(); it_b != b.end();) {
                        if (a.count(*it_b) > 0) {
                            a.erase(a.find(*it_b));
                            it_b = b.erase(it_b);
                        } else {
                            ++it_b;
                        }
                    }
                }

                if (!is_valid) break;
            }

            if (!is_valid) break;
        }

        if (is_valid && a.empty() && b.empty()) {
            cout << "Yes\n";
        } else if (is_valid) {
            cout << "No\n";
        }
    }
}