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

Previous 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;
}


Submission (verdict: Accepted):
#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;
        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;
}