Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Idleness limit exceeded on test 1):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using os = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long 
#define pb push_back
#define ceils(x, y) (x + y - 1) / y 
ll fact[1000005], ifact[1000005];
#define M 998244353
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll exp(ll a, ll b, ll mod = M) {a %= mod; ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll inv(ll a, ll b = M) {return exp(a, b - 2, b);}
ll nCr(ll n, ll r, ll m = M) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
ll add(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mul(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll sub(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll dvd(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (mul(a, inv(b, m), m) + m) % m;}

// ------------------------------------------------------- main code ----------------------------------------------------------------//

void solve(ll test) {
  priority_queue<ll,vector<ll>,greater<ll>> pq;
  ll n,m;cin >> n >> m;
  vector<ll>a(n),b(m);
  for(int i = 0;i < n;i++) {
    cin >> a[i];
    pq.push(a[i]);
  }
  for(int i = 0;i < m;i++) {
    cin >> b[i];
  }
  sort(b.begin(),b.end());
  ll i = 0;
  while(i < m) {
    if(pq.top() == b[i]) {
      i++;
      pq.pop();
    }else {
      ll f = pq.top();pq.pop();
      if(f > b[i]) {
        cout << "No\n";return;
      }
      if(pq.size() == 1) {
        cout << "No\n";return;
      }
      ll s = pq.top();pq.pop();
      if(abs(f - s) > 1) {
        cout << "No\n";return;
      }
      pq.push(f + s);
    }
  }
  if(pq.size() > 0)cout << "No\n";
  else cout << "Yes\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);cout.tie(0);
  int t = 1; 
  cin >> t;
  // fact[0] = 1;
  // for(int i = 1;i <= 1000005;i++) {
  //   fact[i] = mul(i,fact[i - 1]);
  // }
  // ifact[1000005] = inv(fact[1000005]);
  // ifact[0] = 1;
  // for(int i = 1000005 - 1;i >= 1;i--) {
  //   ifact[i] = mul(i + 1,ifact[i + 1]);
  // }
  while(t > 0) {
    solve(t);
    t--;
  }
}


//seperate case for k = 1
// for k > 1 
// 

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using os = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long 
#define pb push_back
#define ceils(x, y) (x + y - 1) / y 
ll fact[1000005], ifact[1000005];
#define M 998244353
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll exp(ll a, ll b, ll mod = M) {a %= mod; ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll inv(ll a, ll b = M) {return exp(a, b - 2, b);}
ll nCr(ll n, ll r, ll m = M) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
ll add(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mul(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll sub(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll dvd(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (mul(a, inv(b, m), m) + m) % m;}

// ------------------------------------------------------- main code ----------------------------------------------------------------//

void solve(ll test) {
  priority_queue<ll,vector<ll>,greater<ll>> pq;
  ll n,m;cin >> n >> m;
  vector<ll>a(n),b(m);
  set<ll,greater<ll>> pa,pb;
  map<ll,ll>mpa,mpb;
  for(int i = 0;i < n;i++) {
    cin >> a[i];
    pa.insert(a[i]);
    mpa[a[i]]++;
  }
  for(int i = 0;i < m;i++) {
    cin >> b[i];
    pb.insert(b[i]);
    mpb[b[i]]++;
  }
  while(true) {
    if(pa.empty() && pb.empty()) {
      cout << "Yes\n";return;
    }
    if(pa.empty() || pb.empty()) {
      cout << "No\n";return;
    }
    ll mxa = *pa.begin();
    ll mxb = *pb.begin();
    if(mxa > mxb) {
      cout << "No\n";return;
    }else if(mxa == mxb) {
      ll mn = min(mpa[mxa],mpb[mxb]);
      mpa[mxa] -= mn;
      mpb[mxb] -= mn;
      if(mpa[mxa] == 0)pa.erase(mxa);
      if(mpb[mxb] == 0)pb.erase(mxb);
    }else if(mxa < mxb) {
      ll l = (mxb/2),r = mxb - l;
      pb.erase(mxb);
      mpb[l] += mpb[mxb];
      mpb[r] += mpb[mxb];
      pb.insert(l);
      pb.insert(r);
      mpb[mxb] = 0;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);cout.tie(0);
  int t = 1; 
  cin >> t;
  // fact[0] = 1;
  // for(int i = 1;i <= 1000005;i++) {
  //   fact[i] = mul(i,fact[i - 1]);
  // }
  // ifact[1000005] = inv(fact[1000005]);
  // ifact[0] = 1;
  // for(int i = 1000005 - 1;i >= 1;i--) {
  //   ifact[i] = mul(i + 1,ifact[i + 1]);
  // }
  while(t > 0) {
    solve(t);
    t--;
  }
}


//seperate case for k = 1
// for k > 1 
//