Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include <iostream>

#include <vector>

#include <unordered_map>

using namespace std;



unordered_map<long long, long long> freq;



bool solve(long long x) {

    // If x is present in a, use one copy.

    if(freq[x] > 0) {

        freq[x]--;

        return true;

    }

    // If x is 1 and not available, we cannot split further.

    if(x == 1) return false;

    // Recursively split x into two parts.

    return solve(x / 2) && solve((x + 1) / 2);

}



int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    

    int t;

    cin >> t;

    while(t--){

        int n, m;

        cin >> n >> m;

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

        long long sumA = 0, sumB = 0;

        

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

            cin >> a[i];

            sumA += a[i];

        }

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

            cin >> b[i];

            sumB += b[i];

        }

        

        // The sum invariant: if sums differ, answer is "No".

        if(sumA != sumB){

            cout << "No\n";

            continue;

        }

        

        freq.clear();

        for (auto x : a)

            freq[x]++;

        

        // Start the recursion from the total sum.

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

    }

    return 0;

}

Submission (verdict: Memory limit exceeded on test 6):
#include <iostream>

#include <vector>

#include <queue>

#include <unordered_map>

using namespace std;



int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);



    int t;

    cin >> t;

    while(t--){

        int n, m;

        cin >> n >> m;

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

        long long sumA = 0, sumB = 0;

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

            cin >> a[i];

            sumA += a[i];

        }

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

            cin >> b[j];

            sumB += b[j];

        }



        // The sum invariant must hold.

        if(sumA != sumB){

            cout << "No\n";

            continue;

        }



        // Build a frequency map for the numbers in a.

        unordered_map<long long, int> freq;

        for(auto x : a)

            freq[x]++;



        // Use a max-heap (priority_queue) for the b sequence.

        priority_queue<long long> pq;

        for(auto x : b)

            pq.push(x);



        bool possible = true;



        // Process the numbers by greedily matching or splitting.

        while(!pq.empty()){

            long long x = pq.top();

            pq.pop();



            if(freq[x] > 0){

                // We found a needed number; use it.

                freq[x]--;

            } else {

                // If x is 1 and not needed, we cannot split further.

                if(x == 1){

                    possible = false;

                    break;

                }

                // Split x into two parts.

                long long part1 = x / 2;

                long long part2 = x - part1;

                pq.push(part1);

                pq.push(part2);

            }

        }



        // Check that all numbers from a have been matched.

        for(auto &p : freq){

            if(p.second != 0){

                possible = false;

                break;

            }

        }



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

    }

    return 0;

}