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

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

#define int long long

#define pp pair<int,int>

#define all(a) a.begin(),a.end()

#define rall(a) a.rbegin(),a.rend()

using namespace std;

void remove(int val,map <int,int> &mp) {

    mp[val]--;

    if(mp[val]==0)

        mp.erase(val);

}

void form(int val,map <int,int> &mp) {

    if(mp.size()==0)return;

    if(val<(mp.begin()->first))return;

    

    if(val&1) {

        int x=(val-1)/2,y=x+1;

        if(mp.find(x)!=mp.end() && mp.find(y)!=mp.end()) {

            remove(x,mp);

            remove(y,mp);

            mp[val]++;

        }

        else if(mp.find(x)!=mp.end()) {

            form(y,mp);

            if(mp.find(y)!=mp.end()) {

                remove(y,mp);

                remove(x,mp);

                mp[val]++;

            }

        }

        else if(mp.find(y)!=mp.end()) {

            form(x,mp);

            if(mp.find(x)!=mp.end()) {

                remove(x,mp);

                remove(y,mp);

                mp[val]++;

            }

        }

        else {

            bool flag=false;

            form(x,mp);

            if(mp.find(x)!=mp.end()) {

                remove(x,mp);

                flag=true;

            }

            else

                return;

            form(y,mp);

            if(flag && mp.find(y)!=mp.end()) {

                remove(y,mp);

                mp[val]++;

            }

            else

                return;

        }

    }

    else {

        int x=val/2;

        if(mp.find(x)!=mp.end()) {

            int f=mp[x];

            if(f>=2) {

                remove(x,mp);

                remove(x,mp);

                mp[val]++;

            }

            else if(f==1) {

                form(x,mp);

                if(mp[x]>=2) {

                    remove(x,mp);

                    remove(x,mp);

                    mp[val]++;

                }

            }

        }

        else {

            bool flag=false;

            form(x,mp);

            if(mp.find(x)!=mp.end()) {

                remove(x,mp);

                flag=true;

            }

            else

                return;

            form(x,mp);

            if(flag && mp.find(x)!=mp.end()) {

                remove(x,mp);

                mp[val]++;

            }

            else

                return;

        }

    }   



}

void solve()

{

    int n,m;cin>>n>>m;

    vector <int> a(n),b(m);

    for(int i=0;i<n;i++)cin>>a[i];

    for(int i=0;i<m;i++)cin>>b[i];

    

    map <int,int> mp;

    for(int i:a)

        mp[i]++;



    for(int i=0;i<m;i++) {

        if(mp.find(b[i])!=mp.end()) {

            remove(b[i],mp);

        }

        else {

            form(b[i],mp);

            if(mp.find(b[i])!=mp.end()) {

                remove(b[i],mp);

            }

            else {

                cout<<"No"<<endl;

                return;

            }

        }

    }

    if(mp.size()==0)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    

}

signed main()

{

    cin.tie()->sync_with_stdio(false);

    int t;cin>>t;

    while(t--)solve();

}

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

#define int long long

#define pp pair<int,int>

#define all(a) a.begin(),a.end()

#define rall(a) a.rbegin(),a.rend()

using namespace std;

void remove(int val,map <int,int> &mp) {

    mp[val]--;

    if(mp[val]==0)

        mp.erase(val);

}

void form(int val,map <int,int> &mp) {

    if(mp.size()==0)return;

    if(val<(mp.begin()->first))return;

    if(mp.find(val)!=mp.end())return;

    if(val&1) {

        int x=(val-1)/2,y=x+1;

        form(x,mp);

        if(mp.find(x)!=mp.end()) {

            remove(x,mp);

            form(y,mp);

            if(mp.find(y)!=mp.end()) {

                remove(y,mp);

                mp[val]++;

            }

            else

                mp[x]++;

        }

    }

    else {

        int x=val/2;

        form(x,mp);

        if(mp.find(x)!=mp.end()) {

            remove(x,mp);

            form(x,mp);

            if(mp.find(x)!=mp.end()) {

                remove(x,mp);

                mp[val]++;

            }

            else

                mp[x]++;

        }

    }



}

void solve()

{

    int n,m;cin>>n>>m;

    vector <int> a(n),b(m);

    for(int i=0;i<n;i++)cin>>a[i];

    for(int i=0;i<m;i++)cin>>b[i];

    

    map <int,int> mp;

    for(int i:a)

        mp[i]++;



    for(int i=0;i<m;i++) {

        form(b[i],mp);

        if(mp.find(b[i])!=mp.end()) {

            remove(b[i],mp);

        }

        else {

            cout<<"No"<<endl;

            return;

        }

    }

    if(mp.size()==0)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    

}

signed main()

{

    cin.tie()->sync_with_stdio(false);

    int t;cin>>t;

    while(t--)solve();

}