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

Previous submission (verdict: Time limit exceeded on test 22):
/*

    * Template for competitive programming

    * Author: Bratin Mondal



    * Deparment of Computer Science and Engineering

    * Indian Institue of Technology, Kharagpur

*/



#include <bits/stdc++.h>

#define ll long long

#define ull unsigned long long

#define endl "\n"

#define vi vector<int>

#define vll vector<ll>

#define pii pair<int, int>

#define pll pair<ll, ll>

#define vpii vector<pii>

#define vpll vector<pll>

#define vvi vector<vi>

#define vvl vector<vll>

#define t(k) cin >> k

#define c(x) cout << x << endl

#define l(i, x, y) for (ll i = x; i < y; i++)

#define le(i, x, y) for (ll i = x; i <= y; i++)

#define lr(i, x, y) for (ll i = x - 1; i >= y; i--)

#define lre(i, x, y) for (ll i = x; i >= y; i--)

#define yes cout << "Yes" << endl

#define no cout << "No" << endl

#define prDouble(x) cout << fixed << setprecision(10) << x

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

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

#define maxv(v) *max_element(all(v))

#define minv(v) *min_element(all(v))

#define maxa(a) *max_element(a, a + n)

#define mina(a) *min_element(a, a + n)

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

#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl

#define sortall(x) sort(all(x))

#define PI acos(-1)

#define cin_2d(vec, n, m)                               \

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

        for (int j = 0; j < m && cin >> vec[i][j]; j++) \

            ;

#define cout_2d(vec, n, m)                                      \

    for (int i = 0; i < n; i++, cout << "\n")                   \

        for (int j = 0; j < m && cout << vec[i][j] << " "; j++) \

            ;

#define fast                          \

    ios_base::sync_with_stdio(false); \

    cin.tie(nullptr);                 \

    cout.tie(nullptr);

using namespace std;

const ll MOD = 1e9 + 7;

const ll MAXN = 1e5 + 9;



ll fast_mul(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }



ll binpow(ll a, ll b)

{

    if (b == 0)

        return 1;

    int ans = binpow(a, b / 2);

    ans = fast_mul(ans, ans);

    if (b % 2)

    {

        return fast_mul(ans, a);

    }

    return ans;

}



ll mod_inv(ll a, ll m = MOD)

{

    return binpow(a, m - 2);

}



ll n, m;

multiset<ll> a, b;



bool generate(ll x)

{

    // If x is directly present, remove one copy and return true.

    auto it = a.find(x);

    if (it != a.end())

    {

        a.erase(it);

        return true;

    }



    // Base cases: cannot generate 0 or 1.

    if (x == 0 || x == 1)

        return false;



    if (x % 2 == 1)

    { // x is odd

        ll x1 = (x + 1) / 2;

        ll x2 = (x - 1) / 2;

        // Random 0-1 value to decide which half to generate first

        auto random = rand() % 2;

        if (random == 0)

        {

            // Generate x1 first

            bool gen1 = generate(x1);

            if (!gen1)

                return false;

            bool gen2 = generate(x2);

            return gen2;

        }

        else

        {

            // Generate x2 first

            bool gen1 = generate(x2);

            if (!gen1)

                return false;

            bool gen2 = generate(x1);

            return gen2;

        }

    }

    else

    { // x is even

        ll half = x / 2;

        if (a.count(half) >= 2)

        {

            auto it1 = a.find(half);

            a.erase(it1);

            auto it2 = a.find(half);

            a.erase(it2);

            return true;

        }

        else

        {

            // Otherwise, generate two copies recursively.

            bool gen1 = generate(half);

            if (!gen1)

                return false;

            bool gen2 = generate(half);

            return gen2;

        }

    }

}



void solve()

{

    // your code goes here

    cin >> n >> m;

    a.clear();

    b.clear();

    ll sum1 = 0, sum2 = 0;

    ll x;

    l(i, 0, n)

    {

        cin >> x;

        a.insert(x);

        sum1 += x;

    }

    l(i, 0, m)

    {

        cin >> x;

        if (a.find(x) != a.end())

        {

            a.erase(a.find(x));

        }

        else

        {

            b.insert(x);

        }

        sum2 += x;

    }

    if(sum1!= sum2)

    {

        no;

        return;

    }

    for (auto it : b)

    {

        if (!generate(it))

        {

            no;

            return;

        }

    }

    if (a.empty())

    {

        yes;

    }

    else

    {

        no;

    }

}



int main()

{

    fast;

    ll t = 1;

    cin >> t; // comment out if only one test case

    // Seed a random number generator

    srand(time(0));

    while (t--)

    {

        solve();

    }

    exit(0);

}

Submission (verdict: Accepted):
/*

    * Template for competitive programming

    * Author: Bratin Mondal



    * Deparment of Computer Science and Engineering

    * Indian Institue of Technology, Kharagpur

*/



#include <bits/stdc++.h>

#define ll long long

#define ull unsigned long long

#define endl "\n"

#define vi vector<int>

#define vll vector<ll>

#define pii pair<int, int>

#define pll pair<ll, ll>

#define vpii vector<pii>

#define vpll vector<pll>

#define vvi vector<vi>

#define vvl vector<vll>

#define t(k) cin >> k

#define c(x) cout << x << endl

#define l(i, x, y) for (ll i = x; i < y; i++)

#define le(i, x, y) for (ll i = x; i <= y; i++)

#define lr(i, x, y) for (ll i = x - 1; i >= y; i--)

#define lre(i, x, y) for (ll i = x; i >= y; i--)

#define yes cout << "Yes" << endl

#define no cout << "No" << endl

#define prDouble(x) cout << fixed << setprecision(10) << x

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

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

#define maxv(v) *max_element(all(v))

#define minv(v) *min_element(all(v))

#define maxa(a) *max_element(a, a + n)

#define mina(a) *min_element(a, a + n)

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

#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl

#define sortall(x) sort(all(x))

#define PI acos(-1)

#define cin_2d(vec, n, m)                               \

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

        for (int j = 0; j < m && cin >> vec[i][j]; j++) \

            ;

#define cout_2d(vec, n, m)                                      \

    for (int i = 0; i < n; i++, cout << "\n")                   \

        for (int j = 0; j < m && cout << vec[i][j] << " "; j++) \

            ;

#define fast                          \

    ios_base::sync_with_stdio(false); \

    cin.tie(nullptr);                 \

    cout.tie(nullptr);

using namespace std;

const ll MOD = 1e9 + 7;

const ll MAXN = 1e5 + 9;



ll fast_mul(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }



ll binpow(ll a, ll b)

{

    if (b == 0)

        return 1;

    int ans = binpow(a, b / 2);

    ans = fast_mul(ans, ans);

    if (b % 2)

    {

        return fast_mul(ans, a);

    }

    return ans;

}



ll mod_inv(ll a, ll m = MOD)

{

    return binpow(a, m - 2);

}



ll n, m;

multiset<ll> a, b;



bool multiset_compare(const multiset<ll> &a, const multiset<ll> &b)

{

    // Check if the multisets are exactly same

    if (a.size() != b.size())

        return false;

    auto it1 = a.begin();

    auto it2 = b.begin();

    while (it1 != a.end() && it2 != b.end())

    {

        if (*it1 != *it2)

            return false;

        ++it1;

        ++it2;

    }

    return true;

}



void solve()

{

    // your code goes here

    cin >> n >> m;

    a.clear();

    b.clear();

    ll sum1 = 0, sum2 = 0;

    ll x;

    l(i, 0, n)

    {

        cin >> x;

        a.insert(x);

        sum1 += x;

    }

    l(i, 0, m)

    {

        cin >> x;

        if (a.find(x) != a.end())

        {

            a.erase(a.find(x));

        }

        else

        {

            b.insert(x);

        }

        sum2 += x;

    }

    if (sum1 != sum2)

    {

        no;

        return;

    }



    l(i, 0, n - m)

    {

        // Get the highest element from b

        auto it = b.end();

        it--;

        ll x = *it;

        // Erase the highest element from b

        b.erase(it);

        if (a.find(x) != a.end())

        {

            a.erase(a.find(x));

            i--;

        }

        else

        {

            ll x1 = ceil((double)x / 2);

            ll x2 = x - x1;

            // deb2(x1, x2);

            b.insert(x1);

            b.insert(x2);

        }

    }

    if (multiset_compare(a, b))

    {

        yes;

    }

    else

    {

        no;

    }

}



int main()

{

    fast;

    ll t = 1;

    cin >> t; // comment out if only one test case

    // Seed a random number generator

    srand(time(0));

    while (t--)

    {

        solve();

    }

    exit(0);

}