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

Previous submission (verdict: Time limit exceeded on test 17):
#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

 //find_by order()->iterator of indexed element,  order_of_key()-> number of elements
//  #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")

// typedef tree < ll,  null_type ,  less_equal<ll> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
 
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
 
#define ll long long int

const ll N = 1000100;
ll inf = 1e9;
ll mod = 1e9 + 7;
ll M = mod;

#define endl '\n'
using pii=pair<ll,ll>;
#define MP make_pair
#define F first
#define ld long double
#define S second
#define all(o) o.begin(),o.end()
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
using vpii = vector<pii>;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define deb(x) cout << #x << "=" << x << "\n"
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << "\n"
#define pb push_back
#define mid (l+r)/2
#define lb lower_bound
#define ub upper_bound
ll lcm(ll a,ll b){return (a*b/__gcd(a,b));}
template <typename T> T bin_pow(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
ll fact[1000100],inv_fact[1000100];
ll nCr(ll n,ll r){if(r>n || n<0 ||r<0)return 0;ll d=(inv_fact[r]*inv_fact[n-r])%M;  ll f=(fact[n]*d)%M;return f;}
ll nPr(ll n,ll r){return (nCr(n,r)*fact[r])%M;}

#define cerr cout
#define debarr(a,n)cerr<<#a<<":";for(int i=0;i<n;i++)cerr<<a[i]<<" ";cerr<<endl;
#define debmat(mat,row,col)cerr<<#mat<<":\n";for(int i=0;i<row;i++){for(int j=0;j<col;j++)cerr<<mat[i][j]<<" ";cerr<<endl;}
#define pr(...)dbs(#__VA_ARGS__,__VA_ARGS__)
template<class S,class T>ostream &operator<<(ostream &os,const pair<S,T> &p){return os<<"("<<p.first<<","<<p.second<<")";}
template<class T>ostream &operator<<(ostream &os,const vector<T> &p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const set<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const multiset<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class S,class T>ostream &operator<<(ostream &os,const map<S,T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>void dbs(string str,T t){cerr<<str<<":"<<t<<"\n";}
template<class T,class...S>void dbs(string str,T t,S... s){int idx=str.find(',');cerr<<str.substr(0,idx)<<":"<<t<<",";dbs(str.substr(idx+1),s...);}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//please be crystal clear what problem asks,this will save you a lot of time 


vi arr,brr;
vvi g;
ll n,m,k,q;
 
void solve(){
    cin>>n>>m;
    multiset<ll>a, b;
    for(int i=0; i<n; i++){
        ll x; cin >> x; a.insert(x);
    }
    for(int i=0; i<m; i++){
        ll x; cin >> x;
        if(a.find(x) != a.end()) a.erase(a.find(x));
        else b.insert(x);
    }
    n = a.size(); m = b.size();
    if(b.empty()){
        if(a.empty()) yes;
        else no;
        return;
    }
    while(!a.empty() && !b.empty() && *b.rbegin() >= *a.rbegin() && a.size() >= b.size()){
        ll ae = *a.rbegin(), be = *b.rbegin();
        // pr(a, b);
        if(be == ae){
            a.erase(a.find(ae));
            b.erase(b.find(be));
        }else{
            ll x = be/2, y = be/2;
            if(y == 0){no; return;}
            if(be % 2){
                x++;
                // pr(x, y);
                if(a.find(x) != a.end() && a.find(y) != a.end()){
                    a.erase(a.find(x));
                    a.erase(a.find(y));
                }else{
                    b.insert(x); 
                    b.insert(y);
                }
                b.erase(b.find(be));
            }else{
                if(a.count(x) > 1){
                    a.erase(a.find(x));
                    a.erase(a.find(y));
                }else{
                    b.insert(x);
                    b.insert(y);
                }
                b.erase(b.find(be));
            }
        }
    }
    if(!a.empty() || !b.empty()) no;
    else yes;
}
 
 
signed main(){
    // freopen("IN", "r", stdin);
    // freopen("OUT", "w", stdout);
    IOS

    // fact[0] = 1;
    // for(ll i=1; i<3*100100; i++){
    //     fact[i] = (i * fact[i-1]) % mod;
    // }
    // for(ll i=0; i<3*100100; i++){
    //     inv_fact[i] = bin_pow<ll>(fact[i], mod-2, mod);
    // }
    // deb(nCr(5, 2));

    // div1.resize(m+1);
    // ll max_=0;
    // for(ll i=1; i<=m; i++){
    //     for(ll j=i; j<=m; j+=i){
    //         div1[j].pb(i);
    //         // max_ = max(max_, (ll)div1[j].size());
    //     }
    // }
    // cout<<max_<<endl;

    ll tt = 1;
    // ll cnt = 1;
    cin>>tt; // single or multiple run line

    while(tt--){
        // cout<<"Case #"<<cnt<<": ";
        solve();
        // cnt++;
    }
    // cout<<cnt<<endl;
    return 0;
}


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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

 //find_by order()->iterator of indexed element,  order_of_key()-> number of elements
//  #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")

// typedef tree < ll,  null_type ,  less_equal<ll> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
 
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
 
#define ll long long int

const ll N = 1000100;
ll inf = 1e9;
ll mod = 1e9 + 7;
ll M = mod;

#define endl '\n'
using pii=pair<ll,ll>;
#define MP make_pair
#define F first
#define ld long double
#define S second
#define all(o) o.begin(),o.end()
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
using vpii = vector<pii>;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define deb(x) cout << #x << "=" << x << "\n"
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << "\n"
#define pb push_back
#define mid (l+r)/2
#define lb lower_bound
#define ub upper_bound
ll lcm(ll a,ll b){return (a*b/__gcd(a,b));}
template <typename T> T bin_pow(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
ll fact[1000100],inv_fact[1000100];
ll nCr(ll n,ll r){if(r>n || n<0 ||r<0)return 0;ll d=(inv_fact[r]*inv_fact[n-r])%M;  ll f=(fact[n]*d)%M;return f;}
ll nPr(ll n,ll r){return (nCr(n,r)*fact[r])%M;}

#define cerr cout
#define debarr(a,n)cerr<<#a<<":";for(int i=0;i<n;i++)cerr<<a[i]<<" ";cerr<<endl;
#define debmat(mat,row,col)cerr<<#mat<<":\n";for(int i=0;i<row;i++){for(int j=0;j<col;j++)cerr<<mat[i][j]<<" ";cerr<<endl;}
#define pr(...)dbs(#__VA_ARGS__,__VA_ARGS__)
template<class S,class T>ostream &operator<<(ostream &os,const pair<S,T> &p){return os<<"("<<p.first<<","<<p.second<<")";}
template<class T>ostream &operator<<(ostream &os,const vector<T> &p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const set<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const multiset<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class S,class T>ostream &operator<<(ostream &os,const map<S,T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>void dbs(string str,T t){cerr<<str<<":"<<t<<"\n";}
template<class T,class...S>void dbs(string str,T t,S... s){int idx=str.find(',');cerr<<str.substr(0,idx)<<":"<<t<<",";dbs(str.substr(idx+1),s...);}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//please be crystal clear what problem asks,this will save you a lot of time 


vi arr,brr;
vvi g;
ll n,m,k,q;
 
void solve(){
    cin>>n>>m;
    multiset<ll>a, b;
    for(int i=0; i<n; i++){
        ll x; cin >> x; a.insert(x);
    }
    for(int i=0; i<m; i++){
        ll x; cin >> x;
        if(a.find(x) != a.end()) a.erase(a.find(x));
        else b.insert(x);
    }
    n = a.size(); m = b.size();
    if(b.empty()){
        if(a.empty()) yes;
        else no;
        return;
    }
    while(!a.empty() && !b.empty() && *b.rbegin() >= *a.rbegin() && a.size() >= b.size()){
        ll ae = *a.rbegin(), be = *b.rbegin();
        // pr(a, b);
        if(be == ae){
            a.erase(a.find(ae));
            b.erase(b.find(be));
        }else{
            ll x = be/2, y = be/2;
            if(y == 0){no; return;}
            if(be % 2){
                x++;
                // pr(x, y);
                if(a.find(x) != a.end() && a.find(y) != a.end()){
                    a.erase(a.find(x));
                    a.erase(a.find(y));
                }else{
                    b.insert(x); 
                    b.insert(y);
                }
                b.erase(b.find(be));
            }else{
                // count function of multiset is linear in matches
                bool flag = false;
                if(a.find(x) != a.end()){
                    a.erase(a.find(x));
                    if(a.find(y) != a.end()){
                        a.erase(a.find(y));
                    }else{
                        a.insert(x);
                        flag = true;
                    }
                }else flag = true;

                if(flag){
                    b.insert(x);
                    b.insert(y);
                }
                b.erase(b.find(be));
            }
        }
    }
    if(!a.empty() || !b.empty()) no;
    else yes;
}
 
 
signed main(){
    // freopen("IN", "r", stdin);
    // freopen("OUT", "w", stdout);
    IOS

    // fact[0] = 1;
    // for(ll i=1; i<3*100100; i++){
    //     fact[i] = (i * fact[i-1]) % mod;
    // }
    // for(ll i=0; i<3*100100; i++){
    //     inv_fact[i] = bin_pow<ll>(fact[i], mod-2, mod);
    // }
    // deb(nCr(5, 2));

    // div1.resize(m+1);
    // ll max_=0;
    // for(ll i=1; i<=m; i++){
    //     for(ll j=i; j<=m; j+=i){
    //         div1[j].pb(i);
    //         // max_ = max(max_, (ll)div1[j].size());
    //     }
    // }
    // cout<<max_<<endl;

    ll tt = 1;
    // ll cnt = 1;
    cin>>tt; // single or multiple run line

    while(tt--){
        // cout<<"Case #"<<cnt<<": ";
        solve();
        // cnt++;
    }
    // cout<<cnt<<endl;
    return 0;
}