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

Previous submission (verdict: Wrong answer on test 3):
#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")



#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <sstream>

#include <queue>

#include <deque>

#include <bitset>

#include <iterator>

#include <list>

#include <stack>

#include <utility>

#include <map>

#include <set>

#include <numeric>

#include <cmath>

#include <cstring>



using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef multiset<int> msi;



#define loop(i, l, n) for (decltype(n) i = l; i < n; i++)

#define rloop(i, l, n) for (decltype(n) i = l; i >= n; i--)

#define pb push_back

#define YES cout << "YES\n"

#define NO cout << "NO\n"

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

int maxm;

int f(int x, multiset<int>& ms, int maxi) {

    

    if (maxi < 0 || x<maxm) return -1;

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

    {

        return 1;

    }

    int a, b;

    if (x % 2 == 0) {

        a = b = x / 2;

    } else {

        a = x / 2;

        b = x / 2 + 1;

    }



    int flag = 0;



    // Try to use a

    if (ms.find(a) != ms.end()) {

        ms.erase(ms.find(a));

        flag++;

    } else {

        if (f(a, ms, maxi - 1) != -1) flag++;

    }



    // Try to use b

    if (ms.find(b) != ms.end()) {

        ms.erase(ms.find(b));

        flag++;

    } else {

        if (f(b, ms, maxi - 1) != -1) flag++;

    }



    return (flag == 2) ? 1 : -1;

}



void solve() {

    int n, m;

    cin >> n >> m;

     maxm = INT_MAX;

    ll sum1 = 0;

    ll sum2= 0;

    multiset<int> ms;

    loop(i, 0, n) {

        int x;

        cin >> x;

        maxm = min(x , maxm);

        sum1+= x;

        ms.insert(x);

    }



    vi v2(m);

    loop(i, 0, m) 

    {cin >> v2[i];

     sum2+= v2[i];

    }

    if(sum1 != sum2)

    {

        NO;

        return;

    }



    // sort(all(v2));

    int maxi = n-m;;



    for (int i = m - 1; i >= 0; i--) {

        if (f(v2[i], ms, maxi) == -1) {

            NO;

            return;

        }

    }



    YES;

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(NULL);



    int t;

    cin >> t;

    while (t--) {

        solve();

    }



    return 0;

}



Submission (verdict: Accepted):
#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")



#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <sstream>

#include <queue>

#include <deque>

#include <bitset>

#include <iterator>

#include <list>

#include <stack>

#include <utility>

#include <map>

#include <set>

#include <numeric>

#include <cmath>

#include <cstring>



using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef multiset<int> msi;



#define loop(i, l, n) for (decltype(n) i = l; i < n; i++)

#define rloop(i, l, n) for (decltype(n) i = l; i >= n; i--)

#define pb push_back

#define YES cout << "YES\n"

#define NO cout << "NO\n"

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

int maxm;

int f(int x, multiset<int>& ms, int maxi) {

    

    if (maxi < 0 || x<maxm) return -1;

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

    {

        ms.erase(ms.find(x));

        return 1;

    }

    int a, b;

    if (x % 2 == 0) {

        a = b = x / 2;

    } else {

        a = x / 2;

        b = x / 2 + 1;

    }



    int flag = 0;



    // Try to use a

    if (ms.find(a) != ms.end()) {

        ms.erase(ms.find(a));

        flag++;

    } else {

        if (f(a, ms, maxi - 1) != -1) flag++;

    }



    // Try to use b

    if (ms.find(b) != ms.end()) {

        ms.erase(ms.find(b));

        flag++;

    } else {

        if (f(b, ms, maxi - 1) != -1) flag++;

    }



    return (flag == 2) ? 1 : -1;

}



void solve() {

    int n, m;

    cin >> n >> m;

     maxm = INT_MAX;

    ll sum1 = 0;

    ll sum2= 0;

    multiset<int> ms;

    loop(i, 0, n) {

        int x;

        cin >> x;

        maxm = min(x , maxm);

        sum1+= x;

        ms.insert(x);

    }



    vi v2(m);

    loop(i, 0, m) 

    {cin >> v2[i];

     sum2+= v2[i];

    }

    if(sum1 != sum2)

    {

        NO;

        return;

    }



    // sort(all(v2));

    int maxi = n-m;;



    for (int i = m - 1; i >= 0; i--) {

        if (f(v2[i], ms, maxi) == -1) {

            NO;

            return;

        }

    }



    YES;

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(NULL);



    int t;

    cin >> t;

    while (t--) {

        solve();

    }



    return 0;

}