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

Previous submission (verdict: Time limit exceeded on test 6):
//                                                 ඞඞඞ

#include<bits/stdc++.h>
using namespace std;

#define ll long long 
#define vl vector<long long>
#define rep(j,b) for((j)=0;(j)<(b);(j)++)
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define all(v) (v).begin(), (v).end() 
#define Sort(v) sort(all(v))
#define nl cout<<endl;

pair<bool,map<ll,ll>> f(ll k, map<ll,ll> &mp)
{
    if(mp[k]>0)
    {
        mp[k]--;
        map<ll,ll> t; t[k]++;
        return {true, t};
    }
    ll x=k/2, y=(k+1)/2;
    if(x<1)
    {
        map<ll,ll> d; return {false,d};
    }
    auto t1=f(x,mp), t2=f(y,mp);
    if(t1.first==true)
    {
        if(t2.first==true)
        {
            for(auto it: t2.second)
            {
                t1.second[it.first]+= it.second;
            }
            return t1;
        }
        else
        {
            for(auto it:t1.second)
            {
                mp[it.first]+=it.second;
            }
            t1.second.clear();
            return {false,t1.second};
        }
    }
    else
    {
        if(t2.first==true)
        {
            for(auto it:t2.second)
            {
                mp[it.first]+=it.second;
            }
            t2.second.clear();
            return {false,t2.second};
        }
        else
        {
            map<ll,ll> mm;
            return {false, mm};
        }
    }
}

vl input_vec(ll n) { ll i,a; vl vec; rep(i,n) { cin>>a; vec.push_back(a); } return vec; }

void solve()
{
    ll n,m; cin>>n>>m;
    vl vec=input_vec(n);
    vl v=input_vec(m);
    map<ll,ll> mp; for(ll i=0;i<n;i++) mp[vec[i]]++;
    ll x=accumulate(all(v),0ll);
    ll y=accumulate(all(vec),0ll);
    if(x!=y)
    {
        N return;
    }
    for(ll i=0;i<m;i++) 
    {
        ll k=v[i];
        if(f(k,mp).first==true) continue;
        N return;
    }
    for(auto it:mp)
    {
        if(it.second!=0){ N return;}
    }
    Y 
    

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1; cin>>t;
    while(t--)
        solve();
    return 0;
}

Submission (verdict: Time limit exceeded on test 6):
//                                                 ඞඞඞ

#include<bits/stdc++.h>
using namespace std;

#define ll long long 
#define vl vector<long long>
#define rep(j,b) for((j)=0;(j)<(b);(j)++)
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define all(v) (v).begin(), (v).end() 
#define Sort(v) sort(all(v))
#define nl cout<<endl;

pair<bool,map<ll,ll>> f(ll k, map<ll,ll> &mp)
{
    if(mp[k]>0)
    {
        mp[k]--;
        map<ll,ll> t; t[k]++;
        return {true, t};
    }
    ll x=k/2, y=(k+1)/2;
    if(x<1)
    {
        map<ll,ll> d; return {false,d};
    }
    auto t1=f(x,mp), t2=f(y,mp);
    if(t1.first==true)
    {
        if(t2.first==true)
        {
            for(auto it: t2.second)
            {
                t1.second[it.first]+= it.second;
            }
            return t1;
        }
        else
        {
            for(auto it:t1.second)
            {
                mp[it.first]+=it.second;
            }
            t1.second.clear();
            return {false,t1.second};
        }
    }
    else
    {
        if(t2.first==true)
        {
            for(auto it:t2.second)
            {
                mp[it.first]+=it.second;
            }
            t2.second.clear();
            return {false,t2.second};
        }
        else
        {
            map<ll,ll> mm;
            return {false, mm};
        }
    }
}

vl input_vec(ll n) { ll i,a; vl vec; rep(i,n) { cin>>a; vec.push_back(a); } return vec; }

void solve()
{
    ll n,m; cin>>n>>m;
    vl vec=input_vec(n);
    vl v=input_vec(m);
    map<ll,ll> mp; for(ll i=0;i<n;i++) mp[vec[i]]++;
    ll x=accumulate(all(v),0ll);
    ll y=accumulate(all(vec),0ll);
    Sort(v);
    if(x!=y)
    {
        N return;
    }
    for(ll i=0;i<m;i++) 
    {
        ll k=v[i];
        if(f(k,mp).first==true) continue;
        N return;
    }
    for(auto it:mp)
    {
        if(it.second!=0){ N return;}
    }
    Y 
    

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1; cin>>t;
    while(t--)
        solve();
    return 0;
}