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;
        vector<long long> a(n), b(m);
        
        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.
        if(sumA != sumB){
            cout << "No\n";
            continue;
        }
 
        // Build a frequency map for the original numbers (sequence a).
        unordered_map<long long,int> freq;
        freq.reserve(n * 2);
        for (auto x : a)
            freq[x]++;
 
        // Use a max-heap to simulate splitting the numbers from b.
        priority_queue<long long> pq;
        for (auto x : b)
            pq.push(x);
 
        int matched = 0; // number of elements from a that we have "matched"
        bool possible = true;
 
        while(!pq.empty() && matched < n){
            long long cur = pq.top();
            pq.pop();
            // If cur appears in a then we “take” it.
            if(freq.find(cur) != freq.end() && freq[cur] > 0){
                freq[cur]--;
                matched++;
                continue;
            }
            // If cur is 1 and it is not in a, then we cannot split it further.
            if(cur == 1){
                possible = false;
                break;
            }
            // Otherwise, split cur into the only two valid numbers.
            if(cur % 2 == 0){
                pq.push(cur / 2);
                pq.push(cur / 2);
            } else {
                pq.push(cur / 2);
                pq.push(cur / 2 + 1);
            }
        }
 
        cout << (matched == n && possible ? "Yes" : "No") << "\n";
    }
    return 0;
}


Submission (verdict: Time limit exceeded on test 27):
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
 
// Global frequency map for the numbers of array A.
unordered_map<long long, int> freq;
 
// Recursive function that tries to "explain" x by matching it directly or by splitting.
bool dfs(long long x) {
    // If x is present in A, consume one occurrence and return true.
    if (freq[x] > 0) {
        freq[x]--;
        return true;
    }
    // If x is 1 and not available, we cannot split further.
    if (x == 1) return false;
    // Otherwise, split x into two parts.
    // Note: if x is even, both parts are x/2.
    //       if x is odd, the parts are x/2 and (x+1)/2.
    return dfs(x / 2) && dfs((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];
        }
        
        // Check the invariant: the total sum must be the same.
        if(sumA != sumB){
            cout << "No\n";
            continue;
        }
        
        // Build frequency map for array A.
        freq.clear();
        for(auto x : A)
            freq[x]++;
        
        // Try to "explain" every number in B by splitting it as needed.
        bool possible = true;
        for(auto x : B){
            if(!dfs(x)){
                possible = false;
                break;
            }
        }
        
        cout << (possible ? "Yes" : "No") << "\n";
    }
    
    return 0;
}