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

Previous submission (verdict: Memory limit exceeded on test 6):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastIO\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);\
    cout.precision(numeric_limits<double>::max_digits10);
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define mii map<int, int>
#define pb push_back
#define all(x) (x).begin(), (x).end()
const int P = 1e9+7;

int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;
        vi ai(n), bi(m);

        int sum1 = 0, sum2 = 0;
        mii mp;

        for(int& i : ai){
            cin >> i;
            sum1 += i;
            mp[i]++;
        }
        for(int& i : bi){
            cin >> i;
            sum2 += i;
        }

        if(sum1 == sum2){
            int flag = 1; // default: it will work
            queue<int> q;

            for(int& b : bi){
                q.push(b);

                while(!q.empty()){
                    int a = q.front();
                    if(mp[a] > 0){
                        mp[a]--;
                        q.pop();
                    }
                    else{
                        if(a == 1){
                            flag = 0;
                            break;
                        }
                        else{
                            q.pop();
                            q.push(a/2);
                            q.push(a/2+a%2);
                        }
                    }
                }

                if(flag == 0){
                    break;
                }
            }

            if(flag == 1){
                cout << "Yes\n";
            }
            else{
                cout << "No\n";
            }

        }
        else{
            cout << "No\n";
        }

    }

    return 0;
}

/* 
// Memory limit exceeded

int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;
        vi ai(n), bi(m);
        priority_queue<int> pq;

        int sum1 = 0, sum2 = 0;
        mii mp;

        for(int& i : ai){
            cin >> i;
            sum1 += i;
            mp[i]++;
        }
        for(int& i : bi){
            cin >> i;
            sum2 += i;
            pq.push(i);
        }

        if(sum1 == sum2){
            int flag = 1; // default: it will work

            while(!pq.empty()){
                int a = pq.top();
                if(mp[a] > 0){
                    mp[a]--;
                    pq.pop();
                }
                else{
                    if(a == 1){
                        flag = 0;
                        break;
                    }
                    else{
                        pq.pop();
                        pq.push(a/2);
                        pq.push(a/2+a%2);
                    }
                }
            }

            if(flag == 1){
                cout << "Yes\n";
            }
            else{
                cout << "No\n";
            }

        }
        else{
            cout << "No\n";
        }

    }

    return 0;
} */

/* 
Tried - wrong

int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;

        mii mp1, mp2;
        for(int i = 0; i < n; i++){
            int temp;
            cin >> temp;

            mp1[temp]++;
        }
        for(int i = 0; i < m; i++){
            int temp;
            cin >> temp;

            mp2[temp]++;
        }

        for(auto& i : mp1){
            int k = min(i.second, mp2[i.first]);

            i.second -= k;
            mp2[i.first] -= k;
        }

        priority_queue<int, vi, greater<int>> pqa, pqb;

        for(auto& i : mp1){
            while(i.second--){
                pqa.push(i.first);
            }
        }
        for(auto& i : mp2){
            while(i.second--){
                pqb.push(i.first);
            }
        }

        int flag = 1; // default: true

        while(!pqb.empty() && !pqa.empty()){

            if(pqa.top() == pqb.top()){

                pqa.pop();
                pqb.pop();
            }
            else if(pqa.top() < pqb.top()){

                int a1 = pqa.top();
                pqa.pop();

                if(pqa.empty() || (pqa.top()-a1) > 1){
                    flag = 0;
                    break;
                }
                else{
                    int a2 = pqa.top();
                    pqa.pop();

                    pqa.push(a1+a2);
                }
            }
            else{ // (pqa.top() > pqb.top())

                flag = 1;
                break;
            }
        }

        if(flag == 1 && pqa.empty() && pqb.empty()){
            cout << "Yes\n";
        }
        else{
            cout << "No\n";
        }

    }
    return 0;
} */


// Think from the result
// Think wrt range (only 26 alphabets/ operations, range of int)
// Think it as separate
// Think if it can be done linearly/ bitwise
// Observe the very basic thing that it follows
// Think of limits of answer
// Reduce the number of variables used (make 2 variables as +&- and convert to 1)
// Think to iterate from both left to right & right to left
// Think of result required before calculation
// Change the initial assumption which you set
// Dont use predefined function (set or map or sort) if the no. of elements is large
// Think it as very simple
// Change the order of operation in multiple for-loops
// Think if it can be done in dp

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastIO\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);\
    cout.precision(numeric_limits<double>::max_digits10);
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define mii map<int, int>
#define pb push_back
#define all(x) (x).begin(), (x).end()
const int P = 1e9+7;

// stack is using DFS (depth first search)
int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;
        vi ai(n), bi(m);

        int sum1 = 0, sum2 = 0;
        mii mp;

        for(int& i : ai){
            cin >> i;
            sum1 += i;
            mp[i]++;
        }
        for(int& i : bi){
            cin >> i;
            sum2 += i;
        }

        if(sum1 == sum2){
            int flag = 1; // default: it will work
            stack<int> st;

            for(int& b : bi){
                st.push(b);

                while(!st.empty()){
                    int a = st.top();
                    if(mp[a] > 0){
                        mp[a]--;
                        st.pop();
                    }
                    else{
                        if(a == 1){
                            flag = 0;
                            break;
                        }
                        else{
                            st.pop();
                            st.push(a/2);
                            st.push(a/2+a%2);
                        }
                    }
                }

                if(flag == 0){
                    break;
                }
            }

            if(flag == 1){
                cout << "Yes\n";
            }
            else{
                cout << "No\n";
            }

        }
        else{
            cout << "No\n";
        }

    }

    return 0;
}

/* 
// Memory limit exceeded - Using queue and working on one element at once
// queue is using BFS (breadth first search)

int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;
        vi ai(n), bi(m);

        int sum1 = 0, sum2 = 0;
        mii mp;

        for(int& i : ai){
            cin >> i;
            sum1 += i;
            mp[i]++;
        }
        for(int& i : bi){
            cin >> i;
            sum2 += i;
        }

        if(sum1 == sum2){
            int flag = 1; // default: it will work
            queue<int> q;

            for(int& b : bi){
                q.push(b);

                while(!q.empty()){
                    int a = q.front();
                    if(mp[a] > 0){
                        mp[a]--;
                        q.pop();
                    }
                    else{
                        if(a == 1){
                            flag = 0;
                            break;
                        }
                        else{
                            q.pop();
                            q.push(a/2);
                            q.push(a/2+a%2);
                        }
                    }
                }

                if(flag == 0){
                    break;
                }
            }

            if(flag == 1){
                cout << "Yes\n";
            }
            else{
                cout << "No\n";
            }

        }
        else{
            cout << "No\n";
        }

    }

    return 0;
} */

/* 
// Memory limit exceeded - Using priority_queue and working on all element at once

int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;
        vi ai(n), bi(m);
        priority_queue<int> pq;

        int sum1 = 0, sum2 = 0;
        mii mp;

        for(int& i : ai){
            cin >> i;
            sum1 += i;
            mp[i]++;
        }
        for(int& i : bi){
            cin >> i;
            sum2 += i;
            pq.push(i);
        }

        if(sum1 == sum2){
            int flag = 1; // default: it will work

            while(!pq.empty()){
                int a = pq.top();
                if(mp[a] > 0){
                    mp[a]--;
                    pq.pop();
                }
                else{
                    if(a == 1){
                        flag = 0;
                        break;
                    }
                    else{
                        pq.pop();
                        pq.push(a/2);
                        pq.push(a/2+a%2);
                    }
                }
            }

            if(flag == 1){
                cout << "Yes\n";
            }
            else{
                cout << "No\n";
            }

        }
        else{
            cout << "No\n";
        }

    }

    return 0;
} */

/* 
Tried - wrong

int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;

        mii mp1, mp2;
        for(int i = 0; i < n; i++){
            int temp;
            cin >> temp;

            mp1[temp]++;
        }
        for(int i = 0; i < m; i++){
            int temp;
            cin >> temp;

            mp2[temp]++;
        }

        for(auto& i : mp1){
            int k = min(i.second, mp2[i.first]);

            i.second -= k;
            mp2[i.first] -= k;
        }

        priority_queue<int, vi, greater<int>> pqa, pqb;

        for(auto& i : mp1){
            while(i.second--){
                pqa.push(i.first);
            }
        }
        for(auto& i : mp2){
            while(i.second--){
                pqb.push(i.first);
            }
        }

        int flag = 1; // default: true

        while(!pqb.empty() && !pqa.empty()){

            if(pqa.top() == pqb.top()){

                pqa.pop();
                pqb.pop();
            }
            else if(pqa.top() < pqb.top()){

                int a1 = pqa.top();
                pqa.pop();

                if(pqa.empty() || (pqa.top()-a1) > 1){
                    flag = 0;
                    break;
                }
                else{
                    int a2 = pqa.top();
                    pqa.pop();

                    pqa.push(a1+a2);
                }
            }
            else{ // (pqa.top() > pqb.top())

                flag = 1;
                break;
            }
        }

        if(flag == 1 && pqa.empty() && pqb.empty()){
            cout << "Yes\n";
        }
        else{
            cout << "No\n";
        }

    }
    return 0;
} */


// Think from the result
// Think wrt range (only 26 alphabets/ operations, range of int)
// Think it as separate
// Think if it can be done linearly/ bitwise
// Observe the very basic thing that it follows
// Think of limits of answer
// Reduce the number of variables used (make 2 variables as +&- and convert to 1)
// Think to iterate from both left to right & right to left
// Think of result required before calculation
// Change the initial assumption which you set
// Dont use predefined function (set or map or sort) if the no. of elements is large
// Think it as very simple
// Change the order of operation in multiple for-loops
// Think if it can be done in dp