Problem: 2061D - Kevin and Numbers
Language: C++23 (GCC 14-64, msys2)

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

#include <vector>

#include <queue>

#include <set>

using namespace std;

#define ll long long



void solve() {

    ll i, n, m, suma = 0ll, sumb = 0ll;

    cin >> n >> m;

    vector<ll> a(n);

    vector<ll> b(m);

    

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

        cin >> a[i];

        suma += a[i];

    }

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

        cin >> b[i];

        sumb += b[i];

    }

    

    if(suma != sumb) {

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

        return;

    }

    

    multiset<ll> t(a.begin(), a.end()); // Target multiset

    priority_queue<ll> pq;             // Max-heap for sequence b

    

    for(auto x : b) 

        pq.push(x);

    

    while(!pq.empty()) {

        ll maxi = pq.top();

        pq.pop();

        

        if(t.find(maxi) != t.end()) {

            // Remove one occurrence from t.

            t.erase(t.find(maxi));

        } else {

            if(maxi == 1) {

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

                return;

            }

            // Split the number into two smaller parts.

            pq.push(maxi / 2);

            pq.push((maxi + 1) / 2);

        }

    }

    

    // If all elements in t are matched, output "Yes".

    cout << (t.empty() ? "Yes" : "No") << "\n";

}



int main() {

    int t;

    cin >> t;

    while(t--) {

        solve();

    }

    return 0;

}



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

#include <vector>

#include <map>

#include <algorithm>

using namespace std;

#define ll long long



void solve() {

    ll n, m;

    cin >> n >> m;

    

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

    ll sumA = 0, sumB = 0;

    

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

        cin >> a[i];

        sumA += a[i];

    }

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

        cin >> b[i];

        sumB += b[i];

    }

    

    if (sumA != sumB) {

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

        return;

    }

    

    map<ll, ll> need;

    for (auto x : a) {

        need[x]++;

    }

    

    map<ll, ll> freq;

    for (auto x : b) {

        freq[x]++;

    }

    

    while (!freq.empty()) {

        auto it = freq.rbegin();

        ll x = it->first;

        ll count = it->second;

        freq.erase(x); 

        

        ll required = (need.count(x) ? need[x] : 0);

        ll used = min(count, required);

        if (used > 0) {

            need[x] -= used;

        }

        ll remaining = count - used;

        if (remaining > 0) {

            if (x == 1) {

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

                return;

            }

            ll left = x / 2;

            ll right = (x + 1) / 2;

            freq[left] += remaining;

            freq[right] += remaining;

        }

    }

    

    for (auto p : need) {

        if (p.second != 0) {

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

            return;

        }

    }

    

    cout << "Yes" << "\n";

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    

    int t;

    cin >> t;

    while(t--) {

        solve();

    }

    return 0;

}