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 <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;
        long long sumA = 0, sumB = 0;
        // Frequency map for sequence a.
        unordered_map<long long, long long> freq;
        vector<long long> a(n), b(m);
 
        // Read sequence a and record frequencies.
        for (int i = 0; i < n; i++){
            cin >> a[i];
            freq[a[i]]++;
            sumA += a[i];
        }
 
        // Read sequence b.
        for (int j = 0; j < m; j++){
            cin >> b[j];
            sumB += b[j];
        }
 
        // The total sum is invariant under the merging operations.
        if(sumA != sumB){
            cout << "NO\n";
            continue;
        }
 
        // Use a max-heap (priority_queue) to simulate the reverse splitting.
        priority_queue<long long> pq;
        for(auto x : b)
            pq.push(x);
 
        bool possible = true;
        // Process until the priority queue is empty.
        while(!pq.empty()){
            long long cur = pq.top();
            pq.pop();
 
            // If this number is needed (appears in a), "use" one occurrence.
            if(freq[cur] > 0){
                freq[cur]--;
            } else {
                // If the number is not needed but can be split, split it.
                // Note: The only valid split is cur -> (floor(cur/2), ceil(cur/2))
                // If cur == 1, it cannot be split further so the transformation fails.
                if(cur == 1){
                    possible = false;
                    break;
                }
                long long left = cur / 2;
                long long right = cur - left; // ensures left + right == cur
                pq.push(left);
                pq.push(right);
            }
        }
 
        // After processing, if there remains any required number (i.e. frequency > 0)
        // then the transformation is impossible.
        for(auto &p : freq){
            if(p.second != 0) {
                possible = false;
                break;
            }
        }
 
        cout << (possible ? "YES" : "NO") << "\n";
    }
 
    return 0;
}


Submission (verdict: Time limit exceeded on test 27):
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <algorithm>
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;
        long long sumA = 0, sumB = 0;
        unordered_map<long long, long long> freqA; // frequency of numbers in sequence a
        vector<long long> a(n), b(m);
 
        // Read sequence a and build its frequency table.
        for (int i = 0; i < n; i++){
            cin >> a[i];
            freqA[a[i]]++;
            sumA += a[i];
        }
 
        // Read sequence b.
        for (int j = 0; j < m; j++){
            cin >> b[j];
            sumB += b[j];
        }
 
        // The total sum must be the same.
        if(sumA != sumB){
            cout << "NO\n";
            continue;
        }
 
        // We'll use a map to simulate a "priority queue" keyed by the number,
        // with its value being the count of that number.
        // We want to always extract the largest number.
        map<long long, long long> mp; // Sorted in ascending order.
        for(auto x : b) {
            mp[x]++; 
        }
 
        bool possible = true;
        // Process until our map is empty.
        while(!mp.empty() && possible){
            // Get the largest element.
            auto it = mp.rbegin(); 
            long long x = it->first;
            long long cnt = it->second;
            // Erase the current largest element.
            mp.erase(x);
 
            // Try to "match" some of these x's with a.
            if(freqA[x] > 0){
                long long use = min(freqA[x], cnt);
                freqA[x] -= use;
                cnt -= use;
            }
 
            // If there are still some copies of x left that are not needed,
            // they must be split.
            if(cnt > 0){
                if(x == 1){
                    // Cannot split 1 any further.
                    possible = false;
                    break;
                }
                long long left = x / 2;
                long long right = x - left; // This equals ceil(x/2)
                mp[left] += cnt;
                mp[right] += cnt;
            }
        }
 
        // Finally, if some frequencies in a remain unmatched, then it's impossible.
        for(auto &p : freqA){
            if(p.second != 0) {
                possible = false;
                break;
            }
        }
 
        cout << (possible ? "YES" : "NO") << "\n";
    }
 
    return 0;
}