Problem: 2061D - Kevin and Numbers
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;
typedef long long llt;
#define mod 998244353

bool check(llt &num, map<llt, llt> &mp) 
{
    queue<pair<llt, llt>> q;
    q.push({num, 1});

    while (!q.empty())
    {
        auto i = q.front();
        llt val=i.first;
        llt cnt=i.second;
        q.pop();

        if (mp[val] >= cnt)
        {
            mp[val] -= cnt;
            if (mp[val] == 0) 
                mp.erase(val);
            continue;
        }
        else
        {
            cnt -= mp[val];
            mp.erase(val);
        }

        if (val == 1) return false; 

        if (val % 2 == 0)
        {
            q.push({val / 2, cnt * 2});
        }
        else
        {
            q.push({val / 2, cnt});
            q.push({val / 2 + 1, cnt});
        }
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    llt t;
    cin >> t;
    while (t--)
    {
        llt n, m;
        cin >> n >> m;
        vector<llt> a(n), b(m);
        llt suma = 0, sumb = 0;

        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            suma += a[i];
        }
        for (int i = 0; i < m; i++)
        {
            cin >> b[i];
            sumb += b[i];
        }

        if (suma != sumb)
        {
            cout << "NO" << endl;
            continue;
        }

        map<llt, llt> mp;
        for (int i = 0; i < n; i++)
        {
            mp[a[i]]++;
        }

        string ans = "YES";
        for (int i = 0; i < m; i++)
        {
            if (!check(b[i], mp))
            {
                ans = "NO";
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
typedef long long llt;
#define mod 998244353

bool check(llt &num, map<llt, llt> &mp)
{
    map<llt, llt> mp2;
    mp2[num] = 1;
    while (true)
    {
        map<llt, llt> mp3;
        mp3 = mp2;
        if (mp2.size() == 0)
        {
            break;
        }
        for (auto i : mp3)
        {
            if (mp[i.first] >= i.second)
            {

                mp[i.first] -= i.second;
                mp2.erase(i.first);
                continue;
            }
            else if (mp[i.first] < i.second)
            {

                mp2[i.first] -= mp[i.first];
                mp.erase(i.first);
            }
            if (i.first != 1)
            {
                if (i.first % 2 == 1)
                {
                    mp2[i.first / 2] += mp2[i.first];
                    mp2[i.first / 2 + 1] += mp2[i.first];
                }
                else if (i.first % 2 == 0)
                {
                    mp2[i.first / 2] += mp2[i.first] * 2;
                }

                mp2.erase(i.first);
            }
        }

        for (auto i : mp2)
        {
            if (i.first == 1 && mp[1] < i.second)
            {
                return false;
            }
        }
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    llt t;
    cin >> t;
    while (t--)
    {
        llt n, m;
        cin >> n >> m;
        vector<llt> a(n), b(m);
        llt suma = 0, sumb = 0;

        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            suma += a[i];
        }
        for (int i = 0; i < m; i++)
        {
            cin >> b[i];
            sumb += b[i];
        }
        map<llt, llt> mp;
        if (suma != sumb)
        {
            cout << "NO" << endl;
            continue;
        }
        for (int i = 0; i < n; i++)
        {
            mp[a[i]]++;
        }
        string ans = "YES";
        for (int i = 0; i < m; i++)
        {
            if (check(b[i], mp))
            {
                continue;
            }
            else
            {
                ans = "NO";
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}