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;
#define int long long
#define YES cout<<"YES\n";
#define NO cout<<"NO\n";
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int N=1e9+7;
const int N1=1e6+3;
signed main(){IOS;
    int t;cin>>t;
    while(t--){
        int n,m;cin>>n>>m;int a[n+1],b[m+1];
        map<int,int>mp;int sum1=0,sum2=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];mp[a[i]]++;sum1+=a[i];
        }
        for(int i=1;i<=m;i++)cin>>b[i],sum2+=b[i];
        if(sum1!=sum2){
            cout<<"No"<<endl;continue;
        }
        sort(b+1,b+m+1);
//         auto dfs=[&](auto &dfs,int x,int y)->void{
//             //cout<<x<<" "<<y<<endl;
//             if(x==y){
//                 if(mp1[x+y])mp1[x]*=2;
//                 else mp1[x]=2;
//                 if(!mp[x]&&x>=2)dfs(dfs,x/2,(x+1)/2);
//             }else{
//                 mp1[x]++;mp1[y]++;
//                 if(!mp[x]&&x>=2)dfs(dfs,x/2,(x+1)/2);
//                 if(!mp[y]&&y>=2)dfs(dfs,y/2,(y+1)/2);
//             }
//             if(mp[x]>=mp1[x]&&mp[y]>=mp1[y]&&x!=y){
//                 mp[x]-=mp1[x];mp[y]-=mp1[y];mp[x+y]++;
//                 mp1[x]=0;mp1[y]=0;
//             }
//             if(mp[x]>=mp1[x]&&x==y){
//                 mp[x]-=mp1[x];mp[x+y]++;
//             }
//         };
        int flag=1;
        for(int i=m;i>=1;i--){
            if(mp[b[i]])mp[b[i]]--;
            else{
                map<int,int>mp1;
                int k1=b[i]/2,k2=(b[i]+1)/2;
                mp1[k1]=1;mp1[k2]=1;
                while(true){
                    if(mp[k1]>=mp1[k1]){
                        mp[k1]-=mp1[k1];k1=0;
                    }
                    if(mp[k2]>=mp1[k2]){
                        mp[k2]-=mp1[k2];k2=0;
                    }
                    mp1[k1]-=mp[k1];mp1[k2]-=mp[k2];
                    if(!k1&&!k2)break;
                    if(!k1||(k1==1&&!k2)){
                        flag=0;break;
                    }
                    if(k1&1){
                        mp1[k1/2]+=mp1[k1];
                        mp1[k1/2+1]+=mp1[k1];
                    }else {
                        mp1[k1/2]=mp1[k1]*2;
                    }
                    if(k2&1){
                        mp1[k2/2]+=mp1[k2];
                        mp1[k2/2+1]+=mp1[k2];
                    }else{
                        mp1[k2/2]+=mp1[k1]*2;
                    }
                    //cout<<k1<<" "<<k2<<endl;
                    k1/=2;k2=(k2+1)/2;
                }
            }
        }
        if(flag)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
}
/*******************************************
............................................
............................................
............................................
............................................
.............................1ffLf1.........
............................GGGGGGGC........
....................8CCCftCCCCC1;LLG,.......
..................:CC8CCC.CCCCCCii..........
.................CC88CCCC.CCCCCCC...........
................CC88CCCCC .8.:8CCC..........
................888 CCCCC.88888G;;,.........
,................888CC88.888888888,.........
:::...............CCC888.888888888..........
:::::............Gi1tC8f888888C88,..........
:::::::,........111tttfffffLG8888...........
:::::::::,....ii111ttttttfffttG.............
::::::::::::i111tttffffffffffttt............
::::::::::11tttttfff....fffffftt ...........
..:::::::1Lf1tff,..........fffff1...........
....,:::iiftG::::::.........,LLGtL..........
::::::;:i11t:;;;;;;::,,,,,,,,, f1ii,::,,,,,,
::::::::i11t:::::::::::::::::::f111:::::::::
::::::::111:::::::::::::::::::::t111::::::::
:::::::i1if::::::,,,,,,,,,,,,,,:i111::::::,:
,,,,,::,8G,,,,,,,,,,,,,,,,,,,,,,,tL1:,,,,,,,
,,,,,,:LGL;,,,,,,,,,,,,,,,,,,,,,,,CLf,,,,,,,
,,,,,;LGGG,,,,:,,,,,,,,,,,,,,,,,,,LGCG,,,,,,
*******************************************/