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

Previous submission (verdict: Time limit exceeded on test 7):
// This code sucks, you know it and I know it.
// Move on and call me an idiot later.
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // *A.find_by_order(i), A.order_of_key(x)

#define fast_io                  \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr);
#define int long long
#define double long double
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define ins insert
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define endl "\n"
#define Endl "\n"
#define in(T, x) \
    T x;         \
    cin >> x
#define fr(i, a, b) for (int i = (a); i < (int)(b); i++)
#define frr(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define oyes cout << "YES" << endl
#define ono cout << "NO" << endl
#define ve1 cout << "-1" << endl
#define ff first
#define ss second
#define MAX INT64_MAX
#define MIN INT64_MIN
#define vi_in(vec, n) \
    fr(i, 0, n) { cin >> vec[i]; }
#define vvi_in(vec, n, m)                 \
    fr(i, 0, n)                           \
    {                                     \
        fr(j, 0, m) { cin >> vec[i][j]; } \
    }
#define vp_in(vec, n) \
    fr(i, 0, n) { cin >> vec[i].ff >> vec[i].ss; }
#define vi_out(vec)        \
    for (auto &it : vec)   \
    {                      \
        cout << it << " "; \
    }                      \
    cout << endl;
#define vvi_out(vec)            \
    for (auto &it : vec)        \
    {                           \
        for (auto &it2 : it)    \
        {                       \
            cout << it2 << " "; \
        }                       \
        cout << endl;           \
    }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef set<int> sti;
typedef map<int, int> mpii;
typedef map<char, int> mpci;
typedef unordered_map<int, int> umpii;
typedef unordered_map<char, int> umpci;

void solve()
{
#define testcase
    int n, m;
    cin >> n >> m;
    vi a(n);
    vi b(m);
    vi_in(a, n);
    vi_in(b, m);

    multiset<int> mst_a, mst_b;
    int s1 = 0, s2 = 0;

    fr(i, 0, n)
    {
        mst_a.ins(a[i]);
        s1 += a[i];
    }
    fr(i, 0, m)
    {
        mst_b.ins(b[i]);
        s2 += b[i];
    }

    if (s1 != s2)
    {
        ono;
        return;
    }

    int ct = 0;

    while (!mst_a.empty() && !mst_b.empty())
    {
        if (ct > (n - m))
        {
            break;
        }
        auto it = mst_b.end();
        it--;
        int curr = *it;

        if (mst_a.size() < mst_b.size())
        {
            break;
        }

        if (*mst_a.rbegin() > *mst_b.rbegin())
        {
            break;
        }

        auto del = lower_bound(all(mst_a), curr);
        if (del != mst_a.end() && *del == curr)
        {
            mst_a.erase(del);
        }
        else
        {
            if (curr == 1)
            {
                break;
            }
            mst_b.ins(curr / 2);
            if (curr & 1)
            {
                mst_b.ins(curr / 2 + 1);
            }
            else
            {
                mst_b.ins(curr / 2);
            }
            ct++;
        }
        mst_b.erase(it);
    }

    if (mst_a.empty() && mst_b.empty())
    {
        oyes;
    }
    else
    {
        ono;
    }
}

signed main()
{
    fast_io;
    int t = 1;
#ifdef testcase
    cin >> t;
#endif
    while (t--)
    {
        solve();
    }

    return 0;
}

Submission (verdict: Accepted):
// This code sucks, you know it and I know it.
// Move on and call me an idiot later.
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // *A.find_by_order(i), A.order_of_key(x)

#define fast_io                  \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr);
#define int long long
#define double long double
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define ins insert
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define endl "\n"
#define Endl "\n"
#define in(T, x) \
    T x;         \
    cin >> x
#define fr(i, a, b) for (int i = (a); i < (int)(b); i++)
#define frr(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define oyes cout << "YES" << endl
#define ono cout << "NO" << endl
#define ve1 cout << "-1" << endl
#define ff first
#define ss second
#define MAX INT64_MAX
#define MIN INT64_MIN
#define vi_in(vec, n) \
    fr(i, 0, n) { cin >> vec[i]; }
#define vvi_in(vec, n, m)                 \
    fr(i, 0, n)                           \
    {                                     \
        fr(j, 0, m) { cin >> vec[i][j]; } \
    }
#define vp_in(vec, n) \
    fr(i, 0, n) { cin >> vec[i].ff >> vec[i].ss; }
#define vi_out(vec)        \
    for (auto &it : vec)   \
    {                      \
        cout << it << " "; \
    }                      \
    cout << endl;
#define vvi_out(vec)            \
    for (auto &it : vec)        \
    {                           \
        for (auto &it2 : it)    \
        {                       \
            cout << it2 << " "; \
        }                       \
        cout << endl;           \
    }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef set<int> sti;
typedef map<int, int> mpii;
typedef map<char, int> mpci;
typedef unordered_map<int, int> umpii;
typedef unordered_map<char, int> umpci;

void solve()
{
#define testcase
    int n, m;
    cin >> n >> m;
    vi a(n);
    vi b(m);
    vi_in(a, n);
    vi_in(b, m);

    multiset<int> mst_a, mst_b;
    int s1 = 0, s2 = 0;

    fr(i, 0, n)
    {
        mst_a.ins(a[i]);
        s1 += a[i];
    }
    fr(i, 0, m)
    {
        mst_b.ins(b[i]);
        s2 += b[i];
    }

    if (s1 != s2)
    {
        ono;
        return;
    }

    int ct = 0;

    while (!mst_a.empty() && !mst_b.empty())
    {
        if (ct > (n - m))
        {
            break;
        }
        auto it = mst_b.end();
        it--;
        int curr = *it;

        if (mst_a.size() < mst_b.size())
        {
            break;
        }

        if (*mst_a.rbegin() > *mst_b.rbegin())
        {
            break;
        }

        auto del = mst_a.lower_bound(curr);
        if (del != mst_a.end() && *del == curr)
        {
            mst_a.erase(del);
        }
        else
        {
            if (curr == 1)
            {
                break;
            }
            mst_b.ins(curr / 2);
            if (curr & 1)
            {
                mst_b.ins(curr / 2 + 1);
            }
            else
            {
                mst_b.ins(curr / 2);
            }
            ct++;
        }
        mst_b.erase(it);
    }

    if (mst_a.empty() && mst_b.empty())
    {
        oyes;
    }
    else
    {
        ono;
    }
}

signed main()
{
    fast_io;
    int t = 1;
#ifdef testcase
    cin >> t;
#endif
    while (t--)
    {
        solve();
    }

    return 0;
}