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;



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;

}



Submission (verdict: Accepted):
#include <iostream>

#include <vector>

#include <map>

using namespace std;



typedef long long ll;



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

        }



        // Invariant check: the sums must be equal.

        if(sumA != sumB){

            cout << "No\n";

            continue;

        }



        // Build frequency map for a.

        map<ll, ll> freq;

        for(auto x : a)

            freq[x]++;



        // Build the need map from b.

        // need[x] means: we need x to appear this many times.

        map<ll, ll> need;

        for(auto x : b)

            need[x]++;



        bool possible = true;



        // Process the need map in descending order.

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

            // Get the largest key.

            auto it = need.end();

            it--;

            ll x = it->first;

            ll cnt = it->second;

            need.erase(it);



            // Use available copies from freq.

            ll use = 0;

            if(freq.count(x))

                use = min(freq[x], cnt);

            cnt -= use;

            if(freq.count(x))

                freq[x] -= use;



            // If we still need more copies of x but cannot split further, fail.

            if(cnt > 0){

                if(x == 1){

                    possible = false;

                    break;

                }

                // Split each copy of x.

                if(x % 2 == 0){

                    // x even => must come from two copies of x/2.

                    need[x/2] += cnt * 2;

                } else {

                    // x odd => must come from one copy of floor(x/2) and one copy of floor(x/2)+1.

                    need[x/2]     += cnt;

                    need[x/2 + 1] += cnt;

                }

            }

        }



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

    }

    return 0;

}