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

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
const int inf = INT_MAX;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n+1,0), b(m+1, 0);
    map<int, int> mp;
    ll ans1=0, ans2 = 0;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
        ans1+=a[i];
        mp[a[i]]++;
    }
    priority_queue<int> q;
    for (int i = 1; i <= m; i++){
        cin >> b[i];
        ans2 += b[i];
        q.push(b[i]);
    }
    if (ans1 != ans2){
        cout << "No\n";
        return;
    }
    int cnt1=0;
    while(!q.empty()){
        int u = q.top();
        q.pop();
        if (mp[u]){
            mp[u]--;
            continue;
        }
        if (u == 1){
            cnt1++;
            continue;
        }
        if (u % 2 == 0){
            int x = u / 2;
            if (mp[x] == 1){
                mp[x] = 0;
                q.push(x);
            }
            else if (mp[x] == 0){
                q.push(x);
                q.push(x);
            }
            else
                mp[x] -= 2;
        }
        else if (u > 1){
            int x = u/2, y = u/2+1;
            if (mp[x]){
                mp[x]--;
            }
            else{
                q.push(x);
            }
            if (mp[y]){
                mp[y]--;
            }
            else
                q.push(y);
        }
    }
    int ans = 1;
    for (auto [x, y] : mp){
        if (y != 0){
            ans = 0;
            break;
        }
    }
    if (cnt1 != mp[1])
        ans = 0;
    if (ans){
        cout << "Yes\n";
    }
    else{
        cout << "No\n";
    }


}

signed main()
{
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
const int inf = INT_MAX;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n+1,0), b(m+1, 0);
    map<int, int> mp;
    ll ans1=0, ans2 = 0;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
        ans1+=a[i];
        mp[a[i]]++;
    }
    priority_queue<int> q;
    int cnt = 0;
    for (int i = 1; i <= m; i++){
        cin >> b[i];
        ans2 += b[i];
        q.push(b[i]);
    }
    if (ans1 != ans2){
        cout << "No\n";
        return;
    }
    int cnt1=0;
    while(!q.empty()){
        int u = q.top();
        q.pop();
        if (mp[u]){
            mp[u]--;
            continue;
        }
        if (u == 1){
            cnt1++;
            continue;
        }
        cnt++;
        if (cnt > n-m){
            break;
        }
        if (u % 2 == 0){
            int x = u / 2;
            if (mp[x] == 1){
                mp[x] = 0;
                q.push(x);
            }
            else if (mp[x] == 0){
                q.push(x);
                q.push(x);
            }
            else
                mp[x] -= 2;
        }
        else if (u > 1){
            int x = u/2, y = u/2+1;
            if (mp[x]){
                mp[x]--;
            }
            else{
                q.push(x);
            }
            if (mp[y]){
                mp[y]--;
            }
            else
                q.push(y);
        }
    }
    int ans = 1;
    for (auto [x, y] : mp){
        if (y != 0){
            ans = 0;
            break;
        }
    }
    if (cnt1 != mp[1])
        ans = 0;
    if (ans){
        cout << "Yes\n";
    }
    else{
        cout << "No\n";
    }


}

signed main()
{
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}