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 T,n,m,a[200005],b[200005];
priority_queue<int> pq;
map<int,int> ma,mb;
int main(){
    cin>>T;
    for(int i=1;i<=T;i++){
        ma.clear(),mb.clear();
        while(!pq.empty())
        pq.pop();
        cin>>n>>m;
        int op=n-m;
        for(int j=1;j<=n;j++){
            cin>>a[j];
            ma[a[j]]++;
        }
        for(int j=1;j<=m;j++){
            cin>>b[j];
            mb[b[j]]++;
        }
        for (auto it=ma.begin();it!=ma.end();){
            int x = it->first;
            if (mb.count(x)){
                if(ma[x]<mb[x]){
                    mb[x]-=ma[x];
                    it=ma.erase(it);
                }
                else if(ma[x]==mb[x]){
                    mb.erase(x);
                    it=ma.erase(it);
                }
                else{
                    ma[x]-=mb[x];
                    mb.erase(x);
                    it++;
                }
            }
            else
            it++;
        }
        for(auto it=mb.begin();it!=mb.end();it++){
            int x=it->first;
            pq.push(x);
        }
        while((!pq.empty())&&op>0){
            int x=pq.top();
            pq.pop();
            if(x==1)
            break;
            if(x%2){
                if(ma.count(x/2+1)){
                    if(ma[x/2+1]<mb[x]){
                        ma.erase(x/2+1);
                        if(!mb.count(x/2+1))
                        pq.push(x/2+1);
                        mb[x/2+1]+=mb[x]-a[x/2+1];
                    }
                    else if(ma[x/2+1]==mb[x])
                    ma.erase(x/2+1);
                    else
                    ma[x/2+1]-=mb[x];
                }
                else{
                    if(!mb.count(x/2))
                    pq.push(x/2);
                    mb[x/2+1]+=mb[x];
                }
                if(ma.count(x/2)){
                    if(ma[x/2]<mb[x]){
                        ma.erase(x/2);
                        if(!mb.count(x/2))
                        pq.push(x/2);
                        mb[x/2]+=mb[x]-a[x/2];
                    }
                    else if(ma[x/2]==mb[x])
                    ma.erase(x/2);
                    else
                    ma[x/2]-=mb[x];
                }
                else{
                    if(!mb.count(x/2))
                    pq.push(x/2);
                    mb[x/2]+=mb[x];
                }
            }
            else{
                if(ma.count(x/2)){
                    if(ma[x/2]<mb[x]*2){
                        ma.erase(x/2);
                        if(!mb.count(x/2))
                        pq.push(x/2);
                        mb[x/2]+=mb[x]*2-a[x/2];
                    }
                    else if(ma[x/2]==mb[x]*2)
                    ma.erase(x/2);
                    else
                    ma[x/2]-=mb[x]*2;
                }
                else{
                    if(!mb.count(x/2))
                    pq.push(x/2);
                    mb[x/2]+=mb[x]*2;
                }
            }
            op-=mb[x];
            mb.erase(x);
        }
        if(ma.empty()&&mb.empty()&&!op)
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
}