Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;

// Function to determine if transformation is possible
bool can_transform(int n, int m, vector<int> &a, vector<int> &b) {
    long long sum_a = 0, sum_b = 0;
    for(auto &x : a) sum_a += x;
    for(auto &x : b) sum_b += x;
    
    // Check if the sums are equal
    if(sum_a != sum_b) return false;
    
    // Sort 'a' and 'b' in descending order
    sort(a.begin(), a.end(), greater<int>());
    sort(b.begin(), b.end(), greater<int>());
    
    // Initialize a max heap (priority queue) with elements of 'b'
    priority_queue<int> pq;
    for(auto &x : b) pq.push(x);
    
    // Pointer for elements in 'a'
    int a_ptr = 0;
    
    // Process the priority queue
    while(!pq.empty()) {
        int y = pq.top(); 
        pq.pop();
        
        // If the current element in 'a' matches 'y', assign it
        if(a_ptr < n && a[a_ptr] == y) {
            a_ptr++;
            continue;
        }
        
        // If 'y' is less than 2, it cannot be split further
        if(y < 2) {
            return false;
        }
        
        // Split 'y' into two parts x and y_x such that |x - y_x| <= 1
        int x = y / 2;
        int y_x = y - x;
        
        // Validate the split condition
        if(abs(x - y_x) > 1) {
            return false;
        }
        
        // Push the split parts back into the priority queue
        pq.push(x);
        pq.push(y_x);
    }
    
    // After processing, all elements in 'a' should have been assigned
    return a_ptr == n;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int t;
    cin >> t;
    
    while(t--){
        int n, m;
        cin >> n >> m;
        
        vector<int> a(n);
        for(auto &x : a) cin >> x;
        
        vector<int> b(m);
        for(auto &x : b) cin >> x;
        
        bool res = can_transform(n, m, a, b);
        cout << (res ? "Yes" : "No") << "\n";
    }
}


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

// Function to count frequencies in a sorted vector
vector<pair<ll, ll>> get_frequencies(const vector<ll>& vec) {
    vector<pair<ll, ll>> freqs;
    int n = vec.size();
    if(n ==0) return freqs;
    ll current = vec[0];
    ll count =1;
    for(int i=1;i<n;i++) {
        if(vec[i] == current) {
            count++;
        }
        else {
            freqs.emplace_back(make_pair(current, count));
            current = vec[i];
            count =1;
        }
    }
    freqs.emplace_back(make_pair(current, count));
    return freqs;
}

bool can_transform(int n, int m, vector<ll> &a, vector<ll> &b) {
    ll sum_a = 0, sum_b = 0;
    for(auto &x : a) sum_a += x;
    for(auto &x : b) sum_b += x;
    if(sum_a != sum_b) return false;
    
    // Sort a and b in descending order
    sort(a.begin(), a.end(), greater<ll>());
    sort(b.begin(), b.end(), greater<ll>());
    
    // Get frequencies of a and b
    vector<pair<ll, ll>> freq_a = get_frequencies(a);
    vector<pair<ll, ll>> freq_b = get_frequencies(b);
    
    // Initialize a pointer for freq_a
    int ptr_a =0;
    ll size_a = freq_a.size();
    
    // Initialize a stack with freq_b
    // We'll use a vector as a stack, where the back is the top
    vector<pair<ll, ll>> stack_b;
    for(auto &p : freq_b) {
        stack_b.emplace_back(p);
    }
    
    while(!stack_b.empty()) {
        pair<ll, ll> current = stack_b.back();
        stack_b.pop_back();
        ll y = current.first;
        ll cnt = current.second;
        
        // Assign as much as possible from a
        while(cnt >0 && ptr_a < size_a && freq_a[ptr_a].first == y) {
            if(freq_a[ptr_a].second >= cnt) {
                freq_a[ptr_a].second -= cnt;
                cnt =0;
            }
            else {
                cnt -= freq_a[ptr_a].second;
                freq_a[ptr_a].second =0;
                ptr_a++;
            }
        }
        
        if(cnt ==0) continue;
        
        // Now, need to split the remaining cnt of y
        if(y ==1) return false; // Cannot split further
        
        // Split y into x and y_x
        ll x = y /2;
        ll y_x = y -x;
        
        if(abs(x - y_x) >1) return false;
        
        // Push the split parts back into the stack with count multiplied by cnt
        // Since we need to split 'cnt' number of y's
        stack_b.emplace_back(make_pair(x, cnt));
        stack_b.emplace_back(make_pair(y_x, cnt));
    }
    
    // After processing, ensure all elements in a are used
    // All elements in a should have been matched
    // It's allowed for a to have extra elements if m < n, but problem states m <=n
    // However, since sum_a == sum_b and we've matched all b's elements, it should be fine
    // Just need to check that any remaining elements in a have zero counts
    for(int i=ptr_a;i<freq_a.size();i++) {
        if(freq_a[i].second !=0) return false;
    }
    
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        int n, m;
        cin >> n >> m;
        vector<ll> a(n);
        for(auto &x : a) cin >> x;
        vector<ll> b(m);
        for(auto &x : b) cin >> x;
        bool res = can_transform(n, m, a, b);
        if(res) cout << "Yes\n";
        else cout << "No\n";
    }
}