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

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

}



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

#include <vector>

#include <unordered_map>

using namespace std;



typedef long long ll;



// Global frequency map to store counts of numbers in array a.

unordered_map<ll, ll> freq;



// Recursive function to check if we can split x into numbers that exactly match entries in freq.

bool solve(ll x) {

    // If x is present in freq, use one occurrence.

    if(freq[x] > 0) {

        freq[x]--;

        return true;

    }

    // If x cannot be split further, return false.

    if(x == 1)

        return false;

    // Otherwise, try splitting x into two parts:

    // One part is floor(x/2) and the other is ceil(x/2).

    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<ll> a(n), b(m);

        ll 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 must hold: otherwise it's impossible.

        if(sumA != sumB){

            cout << "No\n";

            continue;

        }



        // Build the frequency map for array a.

        freq.clear();

        for(auto x : a)

            freq[x]++;



        // Try to decompose the total sum (which equals sumB) into elements from a.

        if(solve(sumB))

            cout << "Yes\n";

        else

            cout << "No\n";

    }

    return 0;

}