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

Previous 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;

}



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

using namespace std;

#define int long long



int32_t main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    

    int t;

    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];

        }

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

            cin >> b[i];

            sumb += b[i];

        }

        if(suma != sumb){

            cout << "No" << "\n";

            continue;

        }

        map<int, int, greater<int>> curr;

        for(auto x : b) curr[x]++;

        bool possible = true;

        while(!curr.empty()){

            auto it = curr.begin();

            int x = it->first, cnt = it->second;

            curr.erase(it);

            if(mp.count(x)){

                int use = min(mp[x], cnt);

                mp[x] -= use;

                cnt -= use;

                if(mp[x] == 0)

                    mp.erase(x);

            }

            if(cnt == 0)

                continue;

            if(x == 1){

                possible = false;

                break;

            }

            int left = x / 2, right = (x + 1) / 2;

            curr[left] += cnt;

            curr[right] += cnt;

        }

        if(!mp.empty())

            possible = false;

        cout << (possible ? "Yes" : "No") << "\n";

    }

    return 0;

}