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


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

int main() {
    
    int tt;
    cin >> tt;
    while(tt--) {
        
        long long n, m;
        cin >> n >> m;
        
        vector<long long>v(n), t(m);
        for(auto &x : v) cin >> x;
        for(auto &x : t) cin >> x;
        
        multiset<long long>st;
        deque<long long>dq;
        
        //sort(t.begin(), t.end());
        for(auto i : t) {
            dq.push_back(i);
        }
        
        for(auto i : v) {
            st.insert(i);
        }
        
        bool exist = true;
        
        while(true) {
            
            long long el = dq.front();
            // cout << "el : " << el << endl;
            
            // cout << "Before :- " << endl;
            // cout << "st : ";
            // for(auto j : st) {
            //     cout << j << " ";
            // } cout << endl;
            
            // cout << "dq : ";
            // for(auto j : dq) {
            //     cout << j << " ";
            // } cout << endl;
            
            if(st.find(el) != st.end()) {
                // cout << "hitted here at el : " << el << endl;
                dq.pop_front();
                auto it = st.find(el);
                st.erase(it);
            }
            
            else if(el%2 == 0) {
                
                auto it = st.find(el/2);
                
                if(it != st.end()) {
                    dq.pop_front();
                    st.erase(it);
                    dq.push_back(el/2);
                }
                
                else {
                    exist = false;
                    break;
                }
            }
            else if(el == 1) {
                auto it = st.find(el);
                
                if(it == st.end()) {
                    exist = false;
                    break;
                }
                else {
                    st.erase(it);
                    dq.pop_front();
                }
            }
            else {
                long long left = el/2;
                long long right = el-left;
                
                // cout << "left : " << left << " right : " << right << endl;
                
                if(st.count(left) == 1) {
                    st.erase(st.find(left));
                    dq.pop_front();
                    dq.push_back(right);
                }
                else if(st.count(right) == 1) {
                    st.erase(st.find(right));
                    dq.pop_front();
                    dq.push_back(left);
                }
                else {
                    exist = false;
                    break;
                }
            }
            
            // cout << "st.size() : " << st.size() << " dq.size() : " << dq.size() << endl;
            
            if((int)st.size() == 0) {
                break;
            }
            if((int)dq.size() == 0) {
                break;
            }
            
            // cout << "After :- " << endl;
            // cout << "st : ";
            // for(auto j : st) {
            //     cout << j << " ";
            // } cout << endl;
            
            // cout << "dq : ";
            // for(auto j : dq) {
            //     cout << j << " ";
            // } cout << endl;
        }
        
        if(exist == true && (int)st.size() == 0 && (int)dq.size() == 0) {
            cout << "Yes" << endl;
        }
        else cout << "No" << endl;
    }
}