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

Previous submission (verdict: Wrong answer on test 3):
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

namespace std

{

    using namespace __gnu_pbds;

    template <typename T>

    using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

};



#ifndef ONLINE_JUDGE

#define debug(x)              \

    cerr << #x << " = [ ";    \

    for (const auto &val : x) \

        cerr << val << " ";   \

    cerr << "]\n";

#define error(args...)                           \

    {                                            \

        string _s = #args;                       \

        replace(_s.begin(), _s.end(), ',', ' '); \

        stringstream _ss(_s);                    \

        istream_iterator<string> _it(_ss);       \

        _err(_it, args);                         \

    }

void _err(istream_iterator<string> it) { cerr << "\n"; }

template <typename T, typename... Args>

void _err(istream_iterator<string> it, T a, Args... args)

{

    cerr << *it << " = " << a << " | ";

    _err(++it, args...);

}

#else

#define debug(...)

#define error(...)

#endif



// Type definitions

typedef long long ll;

typedef long long lli;

typedef long double lld;

using ull = uint64_t;

typedef pair<int, int> pii;

typedef pair<lli, lli> pll;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<lli> vll;

typedef vector<vll> vvl;

typedef vector<bool> vb;

typedef vector<string> vs;

typedef vector<char> vc;

typedef vector<vc> vvc;



// Constants

const lli mod = 1e9 + 7; // 998244353;

const lli mod1 = 1e9 + 7;

const lli mod2 = 998244353;

const lli MAXN = 1e6;

const double eps = 1e-10;

const lli LINF = 1001002003004005006ll;

const int INF = 1001001001;

const int MX = 200005;

constexpr int dx[] = {0, 0, 1, -1};

constexpr int dy[] = {1, -1, 0, 0};

const string ny[] = {"NO", "YES"};

const string nys[] = {"No", "Yes"};



// Macros

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define rev(x) reverse(all(x))

#define srt(x) sort(all(x))

#define maxx (lli) fmax

#define minn (lli) fmin

#define pb push_back

#define F first

#define S second

#define pi acos(-1.0) // 2*asinl(1.0);

#define sq(x) ((x) * (x))

#define issq(x) (((lli)(sqrt((x)))) * ((lli)(sqrt((x)))) == (x))

#define lg(r, n) (lli)(log2l(n) / log2l(r))

#define setpre(n) fixed << setprecision(n)

#define UNIQUE(a) (a).erase(unique(all(a)), (a).end())

#define take(p, n, T) \

    vector<T> p(n);   \

    for (auto &i : p) \

        cin >> i;

#define input(x)      \

    for (auto &i : x) \

    cin >> i

#define print(x)              \

    cerr << #x << " = [ ";    \

    for (const auto &val : x) \

        cout << val << " ";   \

    cerr << "]";              \

    cout << endl;

// Loop macros

#define loop(i, n) for (int i = 0; i < (n); ++i)

#define rloop(i, n) for (int i = (n) - 1; i >= 0; --i)

#define sloop(i, s, t) for (int i = s; i < t; ++i)



// Utility macros

#define arrsum(arr) std::accumulate(arr.begin(), arr.end(), 0LL)

#define maxs(x, y) (x = max(x, y))

#define mins(x, y) (x = min(x, y))

#define limit(x, l, r) max(l, min(x, r))

#define lims(x, l, r) (x = max(l, min(x, r)))

#define isin(x, l, r) ((l) <= (x) && (x) <= (r))

#define pcnt __builtin_popcountll

#define snuke srand((unsigned)clock() + (unsigned)time(NULL))

#define show_var(x) cout << #x << " = " << x << endl

#define PQ(T) priority_queue<T, vector<T>, greater<T>>

#define bn(x) ((1 << (x)) - 1)

#define dup(x, y) (((x) + (y) - 1) / (y))

#define drnd(x, y) (((x) + (y) / 2) / (y))

#define nl cout << endl;

#define YES cout << "YES" << endl;

#define NO cout << "NO" << endl;

#define tc    \

    int t;    \

    cin >> t; \

    for (int ii = 1; ii <= t; ++ii)

#define tc0    \

    int t = 1; \

    for (int ii = 1; ii <= t; ++ii)

#define gp " "

#define case(ii) cout << "Case " << ii << ": "

#define freopen                       \

    freopen("input.txt", "r", stdin); \

    freopen("output.txt", "w", stdout);

// Graph input macro

#define readgraph(arr, edges)         \

    for (int i = 0; i < (edges); i++) \

    {                                 \

        int a, b;                     \

        cin >> a >> b;                \

        arr[a].pb(b);                 \

        arr[b].pb(a);                 \

    }

#define by(x) [](const auto &a, const auto &b) { return a.x < b.x; }

#define tolower_str(s) transform(all(s), s.begin(), ::tolower)

#define toupper_str(s) transform(all(s), s.begin(), ::toupper)

#define cng_str(s) transform(all(s), s.begin(), [](char c) { return c ^ ' '; })

#define clr(arr, val) memset(arr, val, sizeof(arr))



// Utility functions

inline lli gcd(lli a, lli b) { return b == 0 ? a : gcd(b, a % b); }

#define lcm(a, b) (1LL * (a) * (b)) / gcd(a, b)



inline lli madd(lli a, lli b, lli m = mod) { return ((a % m) + (b % m)) % m; }

inline lli msub(lli a, lli b, lli m = mod) { return (((a % m) - (b % m)) % m + m) % m; }

inline lli mmul(lli a, lli b, lli m = mod) { return ((a % m) * (b % m)) % m; }

inline lli mpow(lli base, lli exp, lli m = mod)

{

    lli res = 1LL;

    while (exp > 0)

    {

        if (exp & 1)

            res = mmul(res, base, m);

        base = mmul(base, base, m);

        exp >>= 1LL;

    }

    return res;

}

inline lli minv(lli a, lli m = mod) { return mpow(a, m - 2, m); }



// *****************************************************

// ------------------- CODE STARTS HERE ----------------

// *****************************************************

void pm(map<ll, ll> mp)

{

    for (auto [_, __] : mp)

    {

        cout << _ << gp << __ << endl;

    }

    nl

}



bool rec(map<lli, lli> &m, lli a)

{

    // pm(m);

    if (m[a] > 0)

        return true;

    if (a <= 0)

        return false;

    if (a == 1)

    {

        if (m[1] > 0)

        {

            m[1]--;

            return true;

        }

        return false;

    }

    bool ok1 = rec(m, a / 2);

    bool ok2 = rec(m, a - a / 2);

    if (ok1 and ok2)

    {

        m[a / 2]--;

        m[a - a / 2]--;

        m[a]++;

        return true;

    }

    return false;

}



map<lli, lli> mp[2];

void action()

{

    mp[0].clear();

    mp[1].clear();



    lli n, m;

    cin >> n >> m;

    take(p, n, lli);

    take(q, m, lli);

    srt(p);

    srt(q);



    loop(i, n)

        mp[0][p[i]]++;

    loop(i, m)

        mp[1][q[i]]++;



    if (arrsum(p) != arrsum(q) || p.back() > q.back())

    {

        cout << "No" << endl;

        return;

    }

    lli flag = 0;

    for (auto i : q)

    {

        if (mp[0][i])

        {

            mp[0][i]--;

            mp[1][i]--;

        }

        else

        {

            if (rec(mp[0], i))

            {

                mp[0][i]--;

                mp[1][i]--;

            }

            else

            {

                cout << "No" << endl;

                return;

            }

        }

    }



    cout << nys[flag == 0] << endl;

}

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    tc

    {

        action();

    }

}

Submission (verdict: Wrong answer on test 3):
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

namespace std

{

    using namespace __gnu_pbds;

    template <typename T>

    using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

};



#ifndef ONLINE_JUDGE

#define debug(x)              \

    cerr << #x << " = [ ";    \

    for (const auto &val : x) \

        cerr << val << " ";   \

    cerr << "]\n";

#define error(args...)                           \

    {                                            \

        string _s = #args;                       \

        replace(_s.begin(), _s.end(), ',', ' '); \

        stringstream _ss(_s);                    \

        istream_iterator<string> _it(_ss);       \

        _err(_it, args);                         \

    }

void _err(istream_iterator<string> it) { cerr << "\n"; }

template <typename T, typename... Args>

void _err(istream_iterator<string> it, T a, Args... args)

{

    cerr << *it << " = " << a << " | ";

    _err(++it, args...);

}

#else

#define debug(...)

#define error(...)

#endif



// Type definitions

typedef long long ll;

typedef long long lli;

typedef long double lld;

using ull = uint64_t;

typedef pair<int, int> pii;

typedef pair<lli, lli> pll;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<lli> vll;

typedef vector<vll> vvl;

typedef vector<bool> vb;

typedef vector<string> vs;

typedef vector<char> vc;

typedef vector<vc> vvc;



// Constants

const lli mod = 1e9 + 7; // 998244353;

const lli mod1 = 1e9 + 7;

const lli mod2 = 998244353;

const lli MAXN = 1e6;

const double eps = 1e-10;

const lli LINF = 1001002003004005006ll;

const int INF = 1001001001;

const int MX = 200005;

constexpr int dx[] = {0, 0, 1, -1};

constexpr int dy[] = {1, -1, 0, 0};

const string ny[] = {"NO", "YES"};

const string nys[] = {"No", "Yes"};



// Macros

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define rev(x) reverse(all(x))

#define srt(x) sort(all(x))

#define maxx (lli) fmax

#define minn (lli) fmin

#define pb push_back

#define F first

#define S second

#define pi acos(-1.0) // 2*asinl(1.0);

#define sq(x) ((x) * (x))

#define issq(x) (((lli)(sqrt((x)))) * ((lli)(sqrt((x)))) == (x))

#define lg(r, n) (lli)(log2l(n) / log2l(r))

#define setpre(n) fixed << setprecision(n)

#define UNIQUE(a) (a).erase(unique(all(a)), (a).end())

#define take(p, n, T) \

    vector<T> p(n);   \

    for (auto &i : p) \

        cin >> i;

#define input(x)      \

    for (auto &i : x) \

    cin >> i

#define print(x)              \

    cerr << #x << " = [ ";    \

    for (const auto &val : x) \

        cout << val << " ";   \

    cerr << "]";              \

    cout << endl;

// Loop macros

#define loop(i, n) for (int i = 0; i < (n); ++i)

#define rloop(i, n) for (int i = (n) - 1; i >= 0; --i)

#define sloop(i, s, t) for (int i = s; i < t; ++i)



// Utility macros

#define arrsum(arr) std::accumulate(arr.begin(), arr.end(), 0LL)

#define maxs(x, y) (x = max(x, y))

#define mins(x, y) (x = min(x, y))

#define limit(x, l, r) max(l, min(x, r))

#define lims(x, l, r) (x = max(l, min(x, r)))

#define isin(x, l, r) ((l) <= (x) && (x) <= (r))

#define pcnt __builtin_popcountll

#define snuke srand((unsigned)clock() + (unsigned)time(NULL))

#define show_var(x) cout << #x << " = " << x << endl

#define PQ(T) priority_queue<T, vector<T>, greater<T>>

#define bn(x) ((1 << (x)) - 1)

#define dup(x, y) (((x) + (y) - 1) / (y))

#define drnd(x, y) (((x) + (y) / 2) / (y))

#define nl cout << endl;

#define YES cout << "YES" << endl;

#define NO cout << "NO" << endl;

#define tc    \

    int t;    \

    cin >> t; \

    for (int ii = 1; ii <= t; ++ii)

#define tc0    \

    int t = 1; \

    for (int ii = 1; ii <= t; ++ii)

#define gp " "

#define case(ii) cout << "Case " << ii << ": "

#define freopen                       \

    freopen("input.txt", "r", stdin); \

    freopen("output.txt", "w", stdout);

// Graph input macro

#define readgraph(arr, edges)         \

    for (int i = 0; i < (edges); i++) \

    {                                 \

        int a, b;                     \

        cin >> a >> b;                \

        arr[a].pb(b);                 \

        arr[b].pb(a);                 \

    }

#define by(x) [](const auto &a, const auto &b) { return a.x < b.x; }

#define tolower_str(s) transform(all(s), s.begin(), ::tolower)

#define toupper_str(s) transform(all(s), s.begin(), ::toupper)

#define cng_str(s) transform(all(s), s.begin(), [](char c) { return c ^ ' '; })

#define clr(arr, val) memset(arr, val, sizeof(arr))



// Utility functions

inline lli gcd(lli a, lli b) { return b == 0 ? a : gcd(b, a % b); }

#define lcm(a, b) (1LL * (a) * (b)) / gcd(a, b)



inline lli madd(lli a, lli b, lli m = mod) { return ((a % m) + (b % m)) % m; }

inline lli msub(lli a, lli b, lli m = mod) { return (((a % m) - (b % m)) % m + m) % m; }

inline lli mmul(lli a, lli b, lli m = mod) { return ((a % m) * (b % m)) % m; }

inline lli mpow(lli base, lli exp, lli m = mod)

{

    lli res = 1LL;

    while (exp > 0)

    {

        if (exp & 1)

            res = mmul(res, base, m);

        base = mmul(base, base, m);

        exp >>= 1LL;

    }

    return res;

}

inline lli minv(lli a, lli m = mod) { return mpow(a, m - 2, m); }



// *****************************************************

// ------------------- CODE STARTS HERE ----------------

// *****************************************************

void pm(map<ll, ll> mp)

{

    for (auto [_, __] : mp)

    {

        cout << _ << gp << __ << endl;

    }

    nl

}



bool rec(map<lli, lli> &m, lli a)

{

    // pm(m);

    if (m[a] > 0)

    {

        m[a]--;

        return true;

    }

    if (a <= 0)

        return false;

    if (a == 1)

    {

        if (m[1] > 0)

        {

            m[1]--;

            return true;

        }

        return false;

    }

    bool ok1 = rec(m, a / 2);

    bool ok2 = rec(m, a - a / 2);

    if (ok1 and ok2)

    {

        m[a]++;

        return true;

    }

    m[a/2]++;

    m[a-a/2]++;

    return false;

}



map<lli, lli> mp[2];

void action()

{

    mp[0].clear();

    mp[1].clear();



    lli n, m;

    cin >> n >> m;

    take(p, n, lli);

    take(q, m, lli);

    srt(p);

    srt(q);



    loop(i, n)

        mp[0][p[i]]++;

    loop(i, m)

        mp[1][q[i]]++;



    if (arrsum(p) != arrsum(q) || p.back() > q.back())

    {

        cout << "No" << endl;

        return;

    }

    lli flag = 0;

    for (auto i : q)

    {

        if (mp[0][i])

        {

            mp[0][i]--;

            mp[1][i]--;

        }

        else

        {

            if (rec(mp[0], i))

            {

                // mp[0][i]--;

                mp[1][i]--;

            }

            else

            {

                cout << "No" << endl;

                return;

            }

        }

    }



    cout << nys[flag == 0] << endl;

}

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    tc

    {

        action();

    }

}