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

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

int main(){
    int tc;
    cin>>tc;
    outer:
    while(tc--){
        int n,m;
        cin>>n>>m;
        multiset<int> a,b;
        for(int i = 0;i<n;i++){
            int temp;
            cin>>temp;
            a.insert(temp);
        }
        a.insert(1e9+1);
        for(int i = 0;i<m;i++){
            int temp;
            cin>>temp;
            b.insert(temp);
        }
        for(auto it = a.begin();it!=a.end();++it){
            if(*it  == 1e9+1) break;
            auto next_it = next(it);
            auto ite = b.find(*next_it + *it);
            if(b.find(*it)!=b.end()){
                b.erase(*it);
            }else if(abs(*next_it - *it)<=1 && ite!= b.end()){
                b.erase(ite);
                ++it;
            }else if(abs(*next_it - *it)<=1){
                a.insert(*next_it + *it);
                ++it;
            }else{
                cout<<"NO\n";
                goto outer;
            }
        }
        if(b.size() == 0) cout<<"YES\n";
        else cout<<"NO\n";

    }
}

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

int main() {
    int t; cin >> t;
    while (t--) {
        int n, m; cin >> n >> m;
        map <int, int> seq1;
        map <int, int> seq2;
        int temp;
        int ans = 1;
        for (int i = 0; i < n; i++) {
            cin >> temp;
            if (seq1.find(temp) != seq1.end()) seq1[temp]++;
            else seq1[temp] = 1;
        }
        for (int i = 0; i < m; i++) {
            cin >> temp;
            if (seq2.find(temp) != seq2.end()) seq2[temp]++;
            else seq2[temp] = 1;
        }
        bool loop = true;
        while (!seq2.empty() && loop) {
            for (auto i = seq2.cbegin(); i != seq2.cend();) {
                if (seq1.find(i->first) != seq1.end()) {
                    if (i->second > seq1[i->first]) {
                        seq2[i->first] -= seq1[i->first];
                        seq1.erase(i->first);
                        i++;
                    }else if (i->second < seq1[i->first]) {
                        seq1[i->first] -= i->second;
                        i = seq2.erase(i);
                    }else {
                        seq1.erase(i->first);
                        i = seq2.erase(i);
                    }
                }else {
                    if (i->first % 2 == 0) {
                       if (seq2.find(i->first / 2) != seq2.end()) {
                            seq2[i->first / 2] += i->second*2; 
                        }else {
                            seq2[i->first / 2] = i->second*2;
                        }
                        i = seq2.erase(i);
                    }else if (i->first % 2 == 1 && i->first > 1){
                        int a = 0, b = 0;
                        if (seq2.find(i->first / 2) != seq2.end()) {
                            b = seq2[i->first / 2];
                        }
                        if (seq2.find(i->first / 2 + 1) != seq2.end()) {
                            a = seq2[i->first / 2 + 1];
                        }
                        seq2[i->first / 2 + 1] = a + i->second;
                        seq2[i->first / 2 ] = b + i->second;
                        i = seq2.erase(i);
                    }else {
                        loop = false;
                        ans = 0;
                        break;
                    }
                }
        }
            }
            
        if (ans == 0 || (seq2.empty() && !seq1.empty())) cout << "no" << endl;
        else cout << "yes" << endl;
    }
    return 0;
}