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

Previous submission (verdict: Runtime error on test 3):
#include <bits/stdc++.h>



#define pb push_back

#define eb emplace_back

#define all0(a) a.begin(), a.end()

#define all1(a) a.begin() + 1, a.end()

#define int long long

using namespace std;

typedef long long LL;

typedef pair<int, int> PII;

typedef pair<double, int> PDI;

const int N = 2e5 + 10, INF = 0x3f3f3f3f3f3f3f3f, MOD = 998244353, mod = 1e9 + 7;



void solve() {

    int n, m;

    cin >> n >> m;

    vector<int> a(n + 1), b(m + 1);

    map<int, int> mp1, mp2;

    for (int i = 1; i <= n;i++) cin >> a[i],mp1[a[i]]++;

    for (int i = 1; i <= m;i++) cin >> b[i],mp2[b[i]]++;

    sort(all1(a)), sort(all1(b));

    bool ok = 1;



    auto calc = [&](auto self, int x) -> void {

        if(x == 1)  return;

        if(mp1[x])  return;

        if(x % 2 == 0){

            int l, r;

            l = r = x / 2;

            if(mp1[l] >= 2){

                mp1[x]++;

                mp1[l] -= 2;

                return;

            }else    self(self, l);

        }else{

            int l, r;

            l = x / 2, r = (x + 1) / 2;

            if(mp1[l] && mp1[r]){

                mp1[x]++;

                mp1[l]--, mp1[r]--;

            }else{

                self(self, l), self(self, r);

            }

        }

    };

    for(auto [x,y] : mp2){

        if(mp1[x] >= y) mp1[x] -= y;

        else{

            int t1 = x, cnt = y - mp1[x];

            int l, r;

            if(x % 2 == 0)  l = r = x / 2;

            else l = x / 2,r = (x + 1) / 2;

            while(cnt > 0){

                if(l == r){

                    if(mp1[l] < 2)  calc(calc,l),calc(calc,l);

                    if(mp1[l] >= 2) mp1[l] -= 2,cnt--;

                    else break;

                }else{

                    if(!mp1[l] || !mp1[r])  calc(calc,l), calc(calc,r);

                    if(mp1[l] && mp1[r])    mp1[l]--,mp1[r]--,cnt--;

                    else break;

                }

            }

            if(cnt){

                ok = 0;

                break;

            }

        }

    }

    vector<int> c, d;

    for(auto [x,y] : mp1)

        for (int j = 0; j < y;j++)  c.pb(x);

    sort(all0(c));

    if(c.size() != 0) ok = 0; 

    if(ok)  cout << "Yes\n";

    else cout << "No\n";

}

signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr), cout.tie(nullptr);

    // freopen("in.txt", "r", stdin);

    // freopen("out.txt", "w", stdout);

    int T = 1;

    cin >> T;

    while (T--) {

        solve();

    }

    return 0;

}

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



#define pb push_back

#define eb emplace_back

#define all0(a) a.begin(), a.end()

#define all1(a) a.begin() + 1, a.end()

#define int long long

using namespace std;

typedef long long LL;

typedef pair<int, int> PII;

typedef pair<double, int> PDI;

const int N = 2e5 + 10, INF = 0x3f3f3f3f3f3f3f3f, MOD = 998244353, mod = 1e9 + 7;



void solve() {

    int n, m;

    cin >> n >> m;

    priority_queue<int> a, b;

    for (int i = 0; i < n;i++){

        int x;cin >> x;

        a.push(x);

    }

    for (int i = 0; i < m; i++) {

        int x;

        cin >> x;

        b.push(x);

    }

    while(a.size()){

        if(b.empty() || b.size() > a.size()){

            cout << "No\n";

            return;

        }

        int x = b.top();

        b.pop();

        if(x < a.top()){

            cout << "No\n";

            return;

        }

        if(x == a.top())    a.pop();

        else{

            b.push(x / 2);

            b.push((x + 1) / 2);

        }

    }

    if(b.size())    cout << "No\n";

    else cout << "Yes\n";

}

signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr), cout.tie(nullptr);

    // freopen("in.txt", "r", stdin);

    // freopen("out.txt", "w", stdout);

    int T = 1;

    cin >> T;

    while (T--) {

        solve();

    }

    return 0;

}