Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)

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

void sol() {
    int n, m; cin >> n >> m;
    map<int, int>a, b;
    vector<int> A;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(!a[x]) A.push_back(x);
        a[x]++;
    }
    for(int i = 1; i <= m; ++i) {
        int x; cin >> x;
        b[x]++;
    }
    sort(A.begin(), A.end());
    for(auto x : A) {
        int mn = min(a[x], b[x]);
        a[x] -= mn; b[x] -= mn;
        if(a[x + 1]) {
            mn = min({a[x], a[x + 1], b[2 * x + 1]});
            a[x] -= mn; a[x + 1] -= mn; b[2 * x + 1] -= mn;
        } else {
            if(x == 1) continue;
            mn = min({a[1] / x, a[x], b[2 * x]});
            a[1] -= (x * mn); a[x] -= mn; b[2 * x] -= mn;
            mn = min({a[1] / (x - 1), a[x], b[2 * x - 1]});
            a[1] -= ((x - 1) * mn); a[x] -= mn; b[2 * x - 1] -= mn;
            a[2 * x] += a[x] / 2;
            a[x] %= 2;
            mn = min(a[2 * x], b[2 * x]);
            a[2 * x] -= mn; b[2 * x] -= mn;
            mn = min({a[1] / x, a[x], b[2 * x]});
            a[1] -= (x * mn); a[x] -= mn; b[2 * x] -= mn;
            mn = min({a[1] / (x - 1), a[x], b[2 * x - 1]});
            a[1] -= ((x - 1) * mn); a[x] -= mn; b[2 * x - 1] -= mn;
        }
    }
    int d = 0;
    for(auto [x, cnt] : a) {
        if(x == 1) continue;
        d += (cnt > 0);
    }
    if(d) return void(cout << "NO\n");
    for(auto[x, cnt] : b) {
        if(a[1] < (cnt * x)) return void(cout << "NO\n");
        else a[1] -= (cnt * x);
    }
    if(a[1]) cout << "NO\n";
    else cout << "YES\n";
}

int32_t main() {
    ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
    int T = 1; cin >> T;
    while (T--) sol();
}

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

void sol() {
    int n, m; cin >> n >> m;
    map<int, int>a, b;
    vector<int> A;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(!a[x]) A.push_back(x);
        a[x]++;
    }
    for(int i = 1; i <= m; ++i) {
        int x; cin >> x;
        b[x]++;
    }

    sort(A.begin(), A.end());
    for(auto x : A) {
        int mn = min(a[x], b[x]);
        a[x] -= mn; b[x] -= mn;
    }
    for(auto x : A) {
        int mn = min(a[x], b[x]);
        a[x] -= mn; b[x] -= mn;
        if(a[x + 1]) {
            if(x != 1) {
                mn = min(a[x], a[x + 1]);
                a[x] -= mn; a[x + 1] -= mn;
                a[2 * x + 1] += mn;
                mn = min(a[2 * x + 1], b[2 * x + 1]);
                a[2 * x + 1] -= mn; b[2 * x + 1] -= mn;
                a[2 * x] += a[x] / 2;
                a[x] %= 2;
            } else {
                x++;
                mn = min(a[1] / x, a[x]);
                a[1] -= (x * mn); a[x] -= mn;
                a[2 * x] += mn;
                mn = min(a[2 * x], b[2 * x]);
                a[2 * x] -= mn; b[2 * x] -= mn;
                mn = min(a[1] / (x - 1), a[x]);
                a[1] -= ((x - 1) * mn); a[x] -= mn;
                a[2 * x - 1] += mn;
                mn = min(a[2 * x - 1], b[2 * x - 1]);
                a[2 * x - 1] -= mn; b[2 * x - 1] -= mn;
            }
            //cout << x << ' ' << a[1] << ' ' << a[2] << ' ' << a[3] << '\n';
        } else {
            if(x == 1) continue;
            mn = min(a[1] / x, a[x]);
            a[1] -= (x * mn); a[x] -= mn;
            a[2 * x] += mn;
            mn = min(a[2 * x], b[2 * x]);
            a[2 * x] -= mn; b[2 * x] -= mn;
            mn = min(a[1] / (x - 1), a[x]);
            a[1] -= ((x - 1) * mn); a[x] -= mn;
            a[2 * x - 1] += mn;
            mn = min(a[2 * x - 1], b[2 * x - 1]);
            a[2 * x - 1] -= mn; b[2 * x - 1] -= mn;
            a[2 * x] += a[x] / 2;
            a[x] %= 2;
            mn = min(a[2 * x], b[2 * x]);
            a[2 * x] -= mn; b[2 * x] -= mn;
            mn = min(a[1] / x, a[x]);
            a[1] -= (x * mn); a[x] -= mn;
            a[2 * x] += mn;
            mn = min(a[2 * x], b[2 * x]);
            a[2 * x] -= mn; b[2 * x] -= mn;
            mn = min(a[1] / (x - 1), a[x]);
            a[1] -= ((x - 1) * mn); a[x] -= mn;
            a[2 * x - 1] += mn;
            mn = min(a[2 * x - 1], b[2 * x - 1]);
            a[2 * x - 1] -= mn; b[2 * x - 1] -= mn;
        }
    }
    int d = 0;
    for(auto [x, cnt] : a) {
        if(x == 1) continue;
        d += (cnt > 0);
    }
    if(d) return void(cout << "NO\n");
    for(auto[x, cnt] : b) {
        if(a[1] < (cnt * x)) return void(cout << "NO\n");
        else a[1] -= (cnt * x);
    }
    if(a[1]) cout << "NO\n";
    else cout << "YES\n";
}

int32_t main() {
    ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
    int T = 1; cin >> T;
    while (T--) sol();
}