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

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T>
using ordered_set= tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr); cout.tie(nullptr); 
#define endl "\n" 
#define int long long

signed main() {
    fastio
    int t;
    // t=1;
    cin>>t;
    while(t--){
        int n,m;cin>>n>>m;
        if(m > n){cout<<"no"<<endl; continue;}
        vector<int>v(n),w(m);
        map<int,int>mp1,mp2;
        for(int i=0;i<n;i++){
            cin>>v[i];
            mp1[v[i]]++;
        }
        for(int i=0;i<m;i++){
            cin>>w[i];
            mp2[w[i]]++;
        }

        int test = 1;
        for(auto it = mp2.rbegin(); it != mp2.rend(); it++){
            int num = (*it).first, fre = (*it).second;
            if(num == 1 and mp1[1] < fre){test = 0; cout<<"NO"<<endl; break;}
            if(mp1[num] >= fre) continue;
            int dif = fre - mp1[num];
            mp1[num/2] -= dif;
            mp1[num/2 + (num%2)] -= dif;
            mp1[num] = fre;
        }

        if(!test) continue;
        
        vector<int>check;
        for(auto it: mp1){
            if(it.second < 0){check.push_back(it.first);}
        }

        reverse(check.begin(), check.end());

        for(auto it: check){
            int fre = mp1[it];
            mp1[it/2] += fre;
            mp1[it/2 + (it%2)] += fre;
            mp1[it] = 0;
        }

        for(auto it: mp1){
            if(it.second < 0){cout<<"no"<<endl; test = 0; break;}
            if(it.second and it.second != mp2[it.first]){test = 0; cout<<"no"<<endl;break;}
        }
        if(test) cout<<"yes"<<endl;

    }

}


Submission (verdict: Accepted):
// bb3f16a4-5293-476c-b541-4303b28cb684
#include <bits/stdc++.h>
using namespace std;

bool possible(multiset<int>& nums, int target) {
    while (!nums.empty()) {
        int x = *prev(nums.end());
        
        // If largest number already matches target, we remove and return success
        if (x == target) {
            nums.erase(prev(nums.end()));
            return true;
        }
        
        // We must combine: optimal choice is combining largest number with number differing by at most 1, prioritizing x-1
        nums.erase(prev(nums.end())); // remove the largest
        
        auto it = nums.upper_bound(x); // greater than x, so need to check x or x-1
        int chosen = -1;

        // now we must select number with |x - y| ≤ 1, priority on larger to faster get 'target'
        if (it != nums.begin()) {
            --it;
            if (abs(x - *it) <= 1) {
                chosen = *it;
                nums.erase(it);
            }
        }

        if (chosen == -1) return false; // unable to pair up to continue

        // After combining, new element x + chosen must be inserted back
        nums.insert(x + chosen);
    }

    return false; // never reached exactly target
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    for(int &x : a) cin >> x;
    for(int &x : b) cin >> x;

    // Sort the b sequence in descending order to process largest nums first
    sort(b.rbegin(), b.rend());

    multiset<int> nums;
    for(int x : a) nums.insert(x);

    bool possible_all = true;
    for(int target : b) {
        if (!possible(nums, target)) {
            possible_all = false;
            break;
        }
    }

    cout << (possible_all ? "Yes" : "No") << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}