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

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

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 i = 0; i < m; i++){

            cin >> b[i];

            sumB += b[i];

        }

 

        // The sum must be equal.

        if(sumA != sumB){

            cout << "No\n";

            continue;

        }

 

        // Build a frequency map for numbers in a.

        unordered_map<long long, long long> freq;

        freq.reserve(n * 2);

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

            freq[a[i]]++;

        }

 

        // Use a max-heap (priority queue) to simulate splitting.

        priority_queue<long long> pq;

        for(auto x : b)

            pq.push(x);

 

        bool possible = true;

        long long countUsed = 0; // count how many numbers from a we have matched.

        while(!pq.empty() && possible){

            long long x = pq.top();

            pq.pop();

            if(freq[x] > 0){

                freq[x]--;

                countUsed++;

            } else {

                if(x == 1){

                    possible = false;

                    break;

                }

                // Split x into two parts.

                long long left = x / 2;

                long long right = x - left;

                pq.push(left);

                pq.push(right);

            }

        }

 

        // All a's must be matched.

        if(countUsed != n)

            possible = false;

 

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

    }

    return 0;

}

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;

}