Problem: 2061D - Kevin and Numbers
Language: C++23 (GCC 14-64, msys2)

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

#ifdef LOCAL 
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << "\033[0m" << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr<<"\033[1;31m"<< "LINE "<<__LINE__<<": -> [" << #__VA_ARGS__ << "]:", dbg_out(__VA_ARGS__) 
#else
#define dbg(...) 19
#endif

#define int int64_t
#define sz(x) int(x.size())

bool check(multiset<int>& A, multiset<int>& B){
    if(sz(A) and sz(B)){
        int mxA = *A.rbegin();
        int mxB = *B.rbegin();
        if(mxB < mxA){
            return false;
        }
        int mnA = *A.begin();
        int mnB = *B.begin();
        if(mnB < mnA){
            return false;
        }
        return true;
    } else{
        return false;
    }
}

void solve(int tt){
    int N, M; cin >> N >> M;
    multiset<int> A, B; 
    for(int i = 0; i < N; ++i){
        int x; cin >> x;
        A.insert(x);
    }
    for(int i = 0; i < M; ++i){
        int x; cin >> x;
        B.insert(x);
    }

    vector<int> toRemove;
    map<int, int> freqB;
    for(int x : B){
        freqB[x]++;
    }
    map<int, int> freqA;
    for(int x : A){
        freqA[x]++;
    }

    map<int, int> freq;
    for(auto [k, v] : freqB){
        if(freqA.count(k)){
            freq[k] = min(v, freqA[k]);
        }
    }
    
    for(auto [k, v] : freq){
        for(int i = 0; i < v; ++i){
            A.extract(k);
            B.extract(k);
        }
    } 

    // overlap B to A
    while(true){
        dbg(A, B);
        if(sz(A) == sz(B) and sz(A) == 0){
            break;
        }
        dbg(check(A, B));
        if(!check(A, B)){
            cout << "No\n";
            return;
        } else{
            int mn = *B.begin();
            int a = mn/2;
            int b = mn - a;
            B.extract(mn); 
            B.insert(a); 
            B.insert(b);
            dbg(A, B); 
            vector<int> toRemove;
            if(B.count(a) and A.count(a)){
                toRemove.push_back(a);
            } 
            if(sz(toRemove)){
                B.extract(toRemove.back());
                A.extract(toRemove.back());
                toRemove.clear();
            }
            if(B.count(b) and A.count(b)){
                toRemove.push_back(b);
            }
            if(sz(toRemove)){
                B.extract(toRemove.back());
                A.extract(toRemove.back());
                toRemove.clear();
            }
        }
    }
    cout << "Yes\n";
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T=1; cin >> T;
    for(int tt = 1; tt <= T; ++tt){
        solve(tt);    
    }
}

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

#ifdef LOCAL 
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << "\033[0m" << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr<<"\033[1;31m"<< "LINE "<<__LINE__<<": -> [" << #__VA_ARGS__ << "]:", dbg_out(__VA_ARGS__) 
#else
#define dbg(...) 19
#endif

#define int int64_t
#define sz(x) int(x.size())

bool check(multiset<int>& A, multiset<int>& B){
    if(sz(A) and sz(B)){
        int mxA = *A.rbegin();
        int mxB = *B.rbegin();
        if(mxB < mxA){
            return false;
        }
        int mnA = *A.begin();
        int mnB = *B.begin();
        if(mnB < mnA){
            return false;
        }
        return true;
    } else{
        return false;
    }
}

void solve(int tt){
    int N, M; cin >> N >> M;
    multiset<int> A, B; 
    for(int i = 0; i < N; ++i){
        int x; cin >> x;
        A.insert(x);
    }
    for(int i = 0; i < M; ++i){
        int x; cin >> x;
        B.insert(x);
    }

    vector<int> toRemove;
    map<int, int> freqB;
    for(int x : B){
        freqB[x]++;
    }
    map<int, int> freqA;
    for(int x : A){
        freqA[x]++;
    }

    map<int, int> freq;
    for(auto [k, v] : freqB){
        if(freqA.count(k)){
            freq[k] = min(v, freqA[k]);
        }
    }
    
    for(auto [k, v] : freq){
        for(int i = 0; i < v; ++i){
            A.extract(k);
            B.extract(k);
        }
    } 

    // overlap B to A
    while(true){
        dbg(A, B);
        if(sz(A) == sz(B) and sz(A) == 0){
            break;
        }
        dbg(check(A, B));
        if(!check(A, B)){
            cout << "No\n";
            return;
        } else{
            int mn = *B.begin();
            int a = mn/2;
            int b = mn - a;
            B.extract(mn); 
            B.insert(a); 
            B.insert(b);
            dbg(A, B); 
            vector<int> toRemove;
            if(B.find(a) != B.end() and A.find(a) != A.end()){
                toRemove.push_back(a);
            } 
            if(sz(toRemove)){
                B.extract(toRemove.back());
                A.extract(toRemove.back());
                toRemove.clear();
            }
            if(B.find(b) != B.end() and A.find(b) != A.end()){
                toRemove.push_back(b);
            }
            if(sz(toRemove)){
                B.extract(toRemove.back());
                A.extract(toRemove.back());
                toRemove.clear();
            }
        }
    }
    cout << "Yes\n";
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T=1; cin >> T;
    for(int tt = 1; tt <= T; ++tt){
        solve(tt);    
    }
}