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

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define aa(z) int z; cin >> z
#define vi vector<int>
#define f(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define in(arr, n) for (int i = 0; i < n; i++) cin >> arr[i]
#define out(arr, n) for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl
#define srt(v) sort(v.begin(), v.end())
#define srtr(v) sort(v.begin(), v.end(), greater<int>())
#define err cout << fixed << setprecision(10)
#define lb(arr, d) lower_bound(arr, arr + n, d) - arr
const int mod = 998244353;
/// nirashaaaaaaaaaa
void mohammaad_rafi() {
    aa(n);
    aa(m);
    vi a(n), b(m);
    in(a, n);
    in(b, m);

    map<int, int> mp;
srt(a);srt(b);
    // Populate the map with elements of `a`
    for (int x : a) {
        mp[x]++;
    }
    // for(auto it=mp.begin();it!=mp.end();it++){
    //     cout<<it->first<<" "<<it->second<<endl;
    // }
int i=0;
    while (i<m) {
        auto it = mp.begin();

// cout<<"new map "<<endl;
//        for(auto kt=mp.begin();kt!=mp.end();kt++){
//         cout<<kt->first<<" "<<kt->second<<endl;
//     }

        // Remove all zero-valued entries from the map
        while (it != mp.end() && it->second == 0) {
            it = mp.erase(it); // Erase and move iterator to the next element
        }


        // If the smallest key in the map is greater than `x`, it's impossible to satisfy the condition
        if (mp.empty() || it->first > b[i]) {
            cout << "No" << endl;
            return;
        }

        




        // Handle cases where the smallest key is less than `x`
        while (it->first < b[i]) {
            //auto it=mp.begin();
            int key = it->first;

            // Check if we can combine smaller elements to form `x`
            if (mp[key] >=2 ) {
                mp[key]--;
                mp[key ]--;
                mp[key + key]++;
            }
           else if (mp[key] > 0 && mp.find(key + 1) != mp.end() && mp[key + 1] > 0) {
                mp[key]--;
                mp[key + 1]--;
                mp[key + key + 1]++;
            } else {
                cout << "No" << endl;
                return;
            }

            // Remove keys with zero values from the map
            if (mp[key] == 0) {
                mp.erase(key);
            }
            if (mp[key + 1] == 0) {
                mp.erase(key + 1);
            }


            it = mp.begin(); // Reset iterator after changes
        }

        // If we reach here, the smallest key is equal to `x`
// cout<<"old map "<<endl;
//        for(auto kt=mp.begin();kt!=mp.end();kt++){
//         cout<<kt->first<<" "<<kt->second<<endl;
//     }

        if (it->first == b[i]) {
            i++;
            mp[it->first]--;
            if (mp[it->first] == 0) {
                mp.erase(it);
                
            }
        }

        


    }

if(mp.size()!=0){
    //auto it=mp.begin();
    //if(it->first>0){
cout<<"No"<<endl;return;
//cout<<"uojoeif"<<endl;
//}
}

    cout << "Yes" << endl;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    aa(t); // Number of test cases
    while (t--) {
        mohammaad_rafi();
    }
    return 0;
}


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

#define int long long
#define aa(z) int z; cin >> z
#define vi vector<int>
#define f(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define in(arr, n) for (int i = 0; i < n; i++) cin >> arr[i]
#define out(arr, n) for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl
#define srt(v) sort(v.begin(), v.end())
#define srtr(v) sort(v.begin(), v.end(), greater<int>())
#define err cout << fixed << setprecision(10)
#define lb(arr, d) lower_bound(arr, arr + n, d) - arr
const int mod = 998244353;

void mohammaad_rafi() {
    aa(n);
    aa(m);
    vi a(n), b(m);
    in(a, n);
    in(b, m);

    map<int, int> mp;
    srt(a);
    srt(b);

    // Populate the map with elements of `a`
    for (int x : a) {
        mp[x]++;
    }
int i=0;
    while (i<m) {
if(mp[b[i]]>=1){
    mp[b[i]]--;
    if(mp[b[i]]==0){
        mp.erase(b[i]);
    }
    i++;
}


else if(b[i]%2==0&&mp[b[i]/2]>=2){
    mp[b[i]/2]-=2;
    if(mp[b[i]/2]==0){
        mp.erase(b[i]/2);
    }
    i++;
}
else if(b[i]%2!=0&&mp[b[i]/2]>=1&&mp[b[i]/2+1]>=1){
    mp[b[i]/2]-=1;
    mp[b[i]/2+1]-=1;
     if(mp[b[i]/2]==0){
        mp.erase(b[i]/2);
    }
    
     if(mp[b[i]/2+1]==0){
        mp.erase(b[i]/2+1);
    }
    i++;
}
else{

auto it=mp.begin();
while(it->first<b[i]){
int key=it->first;
if(mp[key+1]>1){
    mp[key]--;
    mp[key+1]--;
    mp[key+key+1]++;
}

if(mp[key]==0){
    mp.erase(key);
}
if(mp[key+1]==0){
    mp.erase(key);
}


it=mp.begin();
}

 it=mp.begin();
 if(mp.size()==0||it->first!=b[i]){
    cout<<"No"<<endl; return;
 }
}



    
    }
    if(mp.size()!=0){
        cout<<"No"<<endl;
        return;
    }

    cout << "Yes" << endl;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    aa(t); // Number of test cases
    while (t--) {
        mohammaad_rafi();
    }
    return 0;
}