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

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


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

// Function to determine if transformation is possible
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;
    
    // Check if the sums are equal
    if(sum_a != sum_b) return false;
    
    // Sort 'a' in descending order
    sort(a.begin(), a.end(), greater<ll>());
    
    // Sort 'b' in descending order
    sort(b.begin(), b.end(), greater<ll>());
    
    // Initialize a max heap (priority queue) with elements of 'b'
    priority_queue<ll> pq;
    for(auto &x : b) pq.push(x);
    
    // Iterate through each element in 'a'
    for(int i = 0; i < n; ++i){
        if(pq.empty()) return false; // No more elements to match
        
        ll current = pq.top();
        pq.pop();
        
        if(current < a[i]) {
            // Current element in heap is smaller than required
            return false;
        }
        
        if(current == a[i]){
            // Exact match found, proceed to next element
            continue;
        }
        
        // Split the current element
        // Split into two parts x and y such that |x - y| <= 1 and x + y = current
        // To satisfy |x - y| <=1, we can choose x = current / 2 and y = current - x
        if(current < 2){
            // Cannot split further
            return false;
        }
        
        ll x = current / 2;
        ll y = current - x;
        
        // Ensure the split condition is met
        if(abs(x - y) > 1){
            return false;
        }
        
        // Push the split parts back into the heap
        pq.push(x);
        pq.push(y);
        
        // After splitting, push the current a[i] back to the heap for the next iteration
        // This ensures that we try to match a[i] in the next steps
        // However, this can lead to infinite loops if not handled properly
        // To prevent this, we continue the loop without pushing back
        // Since we've already split, we allow the next iteration to handle it
        i--; // Stay on the same a[i] for the next iteration
    }
    
    // After matching all elements in 'a', ensure no extra elements remain in 'b'
    // However, due to the sum check and matching process, this is implicitly handled
    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";
    }
}