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


Submission (verdict: Wrong answer on test 2):

#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long

#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626

vector< ll> primes, is_prime, spf;

void sieve( ll n);
ll expo(ll a, ll b); // gives a^b 
bool isPrime(ll num) ;


const  ll mod = 1'000'000'007;
const  ll N = 3e5, M = N;
//=======================


void solve() {

  int n, m;
  cin >> n >> m;
  vector <int> vn(n);
  vector <int> vm(m);
  map <int, int> mn;
  map <int, int> mm;
  fo(i, n) {
  	cin >> vn[i];
  	mn[vn[i]]++;
  }
  fo(i, m) {
  	cin >> vm[i];
  	mm[vm[i]]++;
  }
  for ( auto it : mn) {
  	if (mm[it.first] >= 1 && it.second >= 1) {
  		int m = min(it.second, mm[it.first]);
  		mm[it.first] -= m;
  		mn[it.first] -= m;  		
  		// if (mm[it.first] == 0) mm.erase(it.first);
  		// if (mn[it.first] == 0) mn.erase(it.first);
  	} 
  }
  // for (auto it : mn) {
  // 	cout << it.first << " " << it.second << endl;
  // }
  // for (auto it : mm) {
  // 	cout << it.first << " " << it.second << endl;
  // }
  set <int> sn;
  set <int> sm;
  for (auto it : mn) {
  	if (it.second > 0) sn.insert(it.first);
  }
  for (auto it : mm) {
  	if (it.second > 0) sm.insert(it.first);
  }
  if (sn.size() == 0 && sm.size() == 0) {
  	cout << "YES" << endl;
  	return;
  }
  // else if (sn.size() == sm.size()) {
  // 	cout << "NO" << endl;
  // 	return;
  // }
  else if (*sm.begin() < *sn.begin()) {
  	cout << "NO" << endl;
  	return;
  }
  for (int it : sn) {
  	if (mm[it] >= 1 && mn[it] >= 1) {
  		int m = min(mn[it], mm[it]);
  		mm[it] -= m;
  		mn[it] -= m;  
  	}
  	if (mn[it] > 1) {
  		mn[it * 2] += mn[it]/2;
  		sn.insert(it * 2);
  		mn[it] %= 2;
  	}
  	if (mn[it] >= 1 && mn[it+1] >= 1) {
  		int m = min(mn[it], mn[it+1]);
  		mn[it] -= m;
  		mn[it+1] -= m;
  		mn[it*2 + 1] += m;
  		sn.insert(it*2 + 1);
  	}
  	if (mn[it] != 0) {
  		cout << "NO" << endl;
  		return;
  	}
  }
  cout << "YES" << endl;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    int t = 1;
    cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}






void sieve( ll n) {
    primes.clear();
    is_prime.assign(n + 1, 1);
    spf.assign(n + 1, 0);
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i <= n; i++) {
        if (is_prime[i]) {
            primes.push_back(i);
            spf[i] = i;
            for (ll j = i * i; j <= n; j += i) {
                is_prime[j] = false;
                spf[j] = i;
            }
         }
     }
}

long long expo(long long a, long long b) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

bool isPrime(ll num) {
    if (num <= 1) return false; // 1 or smaller are not prime
    if (num == 2) return true;  // 2 is the only even prime number
    if (num % 2 == 0) return false; // Other even numbers are not prime

    ll limit = static_cast<ll>(sqrt(num)); // We only need to check up to the square root
    for (ll i = 3; i <= limit; i += 2) { // Check odd numbers only
        if (num % i == 0) {
            return false; // Found a divisor, so not prime
        }
    }
    return true; // No divisors found, it is prime
}