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

Previous submission (verdict: Time limit exceeded on test 28):
#include <bits/stdc++.h>

using namespace std;

#define int long long



bool fun(unordered_map<int, int>& mp, int x) {

    if (x < 1) {

        return false;

    }

    

    if (mp.find(x) != mp.end() && mp[x] > 0) {

        mp[x]--;

        return true;

    }

    if(x==1){

        return false;

    }



    

    int left = 0, right = 0;

    if (x % 2 == 0) {

        left = x / 2;

        right = x / 2;

    } else {

        left = x / 2;

        right = (x / 2) + 1;

    }

    

    if(fun(mp, left)&&fun(mp,right)){

        return true;

    }    

    return false;

}



int32_t main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);



    int t = 1;

    cin >> t;

    while(t--){

        int n, m;

        cin >> n >> m;

        vector<int> a(n), b(m);

        unordered_map<int, int> mp;

        

        for (int i = 0; i < n; i++){

            cin >> a[i];

            mp[a[i]]++;

        }

        

        for (int i = 0; i < m; i++){

            cin >> b[i];

        }

        

        bool flag = true;

        for (int i = 0; i < m; i++){

            bool f = fun(mp, b[i]);

            if (!f) {

                flag = false;

                break;

            }

        }

        bool allUsed = true;

        for (auto &p : mp) {

            if (p.second != 0) {

                allUsed = false;

                break;

            }

        }

        

        if (!allUsed) {

            flag = false;

        }

        

        

        if (flag) {

            cout << "Yes" << endl;

        } else {

            cout << "No" << endl;

        }

    }

    

    return 0;

}



Submission (verdict: Memory limit exceeded on test 6):
#include <bits/stdc++.h>

using namespace std;

#define int long long



int32_t main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);



    int t = 1;

    cin >> t;

    while(t--){

        int n, m;

        cin >> n >> m;

        vector<int> a(n), b(m);

        unordered_map<int, int> mp;



        int suma = 0, sumb = 0;

        for(int i = 0; i < n; i++){

            cin >> a[i];

            mp[a[i]]++;

            suma += a[i];

        }



        priority_queue<int> pq;

        for(int i = 0; i < m; i++){

            cin >> b[i];

            pq.push(b[i]);

            sumb += b[i];

        }



        if(suma < sumb){

            cout << "No"<<endl;

            continue;

        }



        bool possible = true;



        while(!pq.empty()){

            int x = pq.top();

            pq.pop();



            if(mp[x] > 0){

                mp[x]--;

                continue;

            }



            if(x == 1){

                possible = false;

                break;

            }



            int left = x / 2;

            int right = (x + 1) / 2;

            pq.push(left);

            pq.push(right);

        }

        bool allUsed = true;

for (auto &p : mp) {

    if (p.second != 0) {

        allUsed = false;

        break;

    }

}



if (!allUsed) {

    possible = false;

}





        if(possible){

            cout << "Yes"<<endl;

        } else {

            cout << "No"<<endl;

        }

    }



    return 0;

}