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

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
#define int long long
#define float long double
#define pp pair<int, int>
#define tp tuple<int, int, int>
#define mtp make_tuple
#define mp make_pair
using namespace std;
// using lf = __float128;
// using ld = __int128;
// float pi = 3.141592653589793238462643383279502884197169399375105Q;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int ddx[8] = {0, 0, 1, -1, 1, 1, -1, 1}, ddy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

map<int, int> adict, bdict;
int go(int bi)
{
    if (adict[bi] != 0)
    {
        adict[bi]--;
        return 0;
    }
    if (bi == 1)
        return 1;
    if (bi % 2 == 0)
    {
        int x = bi >> 1;

        if (adict[x] >= 2)
        {
            adict[x] -= 2;
            return 0;
        }
        go(x);
        return go(x);
    }
    else
    {
        int hol = bi + 1 >> 1, jjak = bi + 1 >> 1;
        if (hol % 2 == 0)
            hol--;
        else
            jjak--;

        int ret = 0;
        if (adict[jjak] == 0)
        {
            ret = go(jjak);
        }
        else
            adict[jjak]--;
        if (ret)
            return 1;
        return go(hol);
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int tt;
    cin >> tt;
    while (tt--)
    {
        adict.clear();
        bdict.clear();
        int n, m;
        cin >> n >> m;
        vector<int> arr(n), brr(m);
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            adict[arr[i]]++;
        }
        for (int i = 0; i < m; i++)
        {
            cin >> brr[i];
            bdict[brr[i]]++;
        }

        int flag = 0;
        for (int i = 0; i < m; i++)
        {
            flag = max(flag, go(brr[i]));
            bdict[brr[i]]--;
        }
        // for (int i = 0; i <= 5; i++)
        // {
        //     cout << adict[i] << ' ';
        // }
        // return 0;

        if (flag)
        {
            cout << "No";
            cout << '\n';
            continue;
        }
        for (auto p : bdict)
        {
            auto [x, counter] = p;
            if (adict[x] != counter)
            {
                flag = 1;
                break;
            }
        }
        for (auto p : adict)
        {
            auto [x, counter] = p;
            if (counter == 0)
                continue;
            if (bdict[x] != counter)
            {
                flag = 1;
                break;
            }
        }
        if (flag)
        {
            cout << "No";
            cout << '\n';
            continue;
        }
        cout << "Yes\n";
    }
    return 0;
}

Submission (verdict: Time limit exceeded on test 23):
#include <bits/stdc++.h>
#define int long long
#define float long double
#define pp pair<int, int>
#define tp tuple<int, int, int>
#define mtp make_tuple
#define mp make_pair
using namespace std;
// using lf = __float128;
// using ld = __int128;
// float pi = 3.141592653589793238462643383279502884197169399375105Q;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int ddx[8] = {0, 0, 1, -1, 1, 1, -1, 1}, ddy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

void er(map<int, int> &dict, int x)
{
    if (dict[x] == 0)
        dict.erase(x);
}

map<int, int> adict, bdict;
int go(int bi)
{
    // 지금 만들고자 하는 수가 최솟값보다 작으면 어차피 못 만든다.
    if (bi < adict.begin()->first)
        return 1;
    if (adict.count(bi))
    {
        adict[bi]--;
        er(adict, bi);
        return 0;
    }
    if (bi == 1)
        return 1;
    if (bi % 2 == 0)
    {
        int x = bi >> 1;

        if (adict.count(x) && adict[x] >= 2)
        {
            adict[x] -= 2;
            er(adict, x);
            return 0;
        }
        go(x);
        return go(x);
    }
    else
    {
        int hol = bi + 1 >> 1, jjak = bi + 1 >> 1;
        if (hol % 2 == 0)
            hol--;
        else
            jjak--;

        int ret = 0;
        if (adict.count(jjak) == 0)
        {
            ret = go(jjak);
        }
        else
        {
            adict[jjak]--;
            er(adict, jjak);
        }
        if (ret)
            return 1;
        return go(hol);
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int tt;
    cin >> tt;
    while (tt--)
    {
        adict.clear();
        bdict.clear();
        int n, m;
        cin >> n >> m;
        vector<int> arr(n), brr(m);
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            adict[arr[i]]++;
        }
        for (int i = 0; i < m; i++)
        {
            cin >> brr[i];
            bdict[brr[i]]++;
        }

        int flag = 0;
        for (int i = 0; i < m; i++)
        {
            flag = max(flag, go(brr[i]));
            // brr을 만든다.
            bdict[brr[i]]--;
            er(bdict, brr[i]);
        }

        if (adict.size() != 0 || bdict.size() != 0)
            flag = 1;

        if (flag)
        {
            cout << "No";
            cout << '\n';
            continue;
        }

        if (flag)
        {
            cout << "No";
            cout << '\n';
            continue;
        }
        cout << "Yes\n";
    }
    return 0;
}