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


Submission (verdict: Wrong answer on test 3):


#include <bits/stdc++.h>
#pragma GCC optimize("O3") // O3 or we may time out :(
using namespace std; 
// #define SUM(X) accumulate(X.begin(), X.end(), 0LL)


#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;


// lc
#define vi vector<int>
#define vvii vector<vi>
#define vpii vector<pair<int,int>>
#define vs vector<string>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define minpq(a) priority_queue<a,vi,greater<a>> pq
#define maxpq(a) priority_queue<a> pq 
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define sall(x) sort(x.begin(), x.end());
#define rall(x) sort(x.rbegin(), x.rend())
#define pb push_back 
#define in insert
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(x) x.begin(), x.end()
#define sall(x) sort(x.begin(), x.end());
#define rall(x) sort(x.rbegin(), x.rend())
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define SUM(X) accumulate(X.begin(), X.end(), 0LL)
#define rev(a) reverse(a.begin(), a.end());





template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long 
#define pii pair<int, int>
#define mod1 998244353 
#define mod 1000000007
#define sei set<int>
#define ses set<string>
#define ues unordered_set<string> 
#define MEM(x, y) memset(x, y, sizeof(x))
#define binone(x) __builtin_popcountll(x) 
const int N = 1e5 + 7;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define ret return 0
#define endl '\n' // remove for interactives

// ================================== take ip/op like vector,pairs directly!==================================
template <typename typC, typename typD>
istream &operator>>(istream &cin, pair<typC, typD> &a) { return cin >> a.first >> a.second; }
template <typename typC>
istream &operator>>(istream &cin, vector<typC> &a)
{
    for (auto &x : a)
        cin >> x;
    return cin;
}
template <typename typC, typename typD>
ostream &operator<<(ostream &cout, const pair<typC, typD> &a) { return cout << a.first << ' ' << a.second; }
template <typename typC, typename typD>
ostream &operator<<(ostream &cout, const vector<pair<typC, typD>> &a)
{
    for (auto &x : a)
        cout << x << '\n';
    return cout;
}
template <typename typC>
ostream &operator<<(ostream &cout, const vector<typC> &a)
{
    int n = a.size();
    if (!n)
        return cout;
    cout << a[0];
    for (int i = 1; i < n; i++)
        cout << ' ' << a[i];
    return cout;
}
// ===================================END Of the input module ==========================================


 
 
//--------------------- Segment Tree ------------------------------------------------//
vector<int> Segtree, height,euler,vis,first;
 
void build(vector<int> &arr, int low, int end, int index)
{ // time complexity--> O(n)
 
    if (low == end)
    {
        Segtree[index] = arr[low]; 
        return;
    }
 
    int mid = (low + end) / 2; 
 
    build(arr, low, mid, 2*index +1);
    build(arr, mid + 1, end, 2*index+2);
 
    int l = Segtree[2*index+1], r= Segtree[2*index+2];
 
    Segtree[index] = ( height[l] > height[r] ? r : l );
} 
 
 
int query(int low, int high, int index, int l, int r)
{ // time complexity--> O(log n)
    // complete overlap       l........low.........high........r
    if (low >= l and high <= r)
        return Segtree[index];
 
 
    // disjoint
    if (l > high or r < low)
        return -1;
 
       
 
    int mid = (low + high) >> 1;
    int left = query(low, mid, 2 * index + 1, l, r);
    int right = query(mid + 1, high, 2 * index + 2, l, r);
 
    if(left==-1 and right!=-1) return right;
    if(right==-1 and left!=-1) return left;
 
 
    int ans = (height[left] > height[right]) ? right : left;
 
    return ans;
} 
 
 
 
void dfs(int node, vvii &adj, int h) {
     vis[node]=1;
     euler.pb(node);
     height[node] = h;
     first[node] = euler.size();
 
     int cnt=0;
 
     for(auto it:adj[node]) {
        if(!vis[it]) {
            dfs(it,adj,h+1);
            euler.pb(node);
        }
     }
}
 
int lca(int u, int v) {
    int left = first[u], right = first[v];
    left--; right--; 
    if(left > right) swap(left,right);
 
 
    return query(0,euler.size()-1,0,left,right);
}

int dfs(int node, int parent, vvii &adj, vi &c, vi &a) {
     c[node] = a[node];
     for(auto it:adj[node]) {
        if( it!=parent ) {
            c[node] += dfs(it, node, adj, c, a);
        }
     }
     return c[node];
}
void dfs(int node, vvii &adj, vi &vis){
    vis[node] = 1;
    for(auto it: adj[node]) {
        if(!vis[it]) {
            dfs(it, adj, vis);
        }
    }
}
 
 
int solve()
{   
    int n,m;
    cin>>n>>m;

    vi a(n),b(m);
    cin>>a>>b;

    

    umii mp;
    for(auto it: a) mp[it]++;
    for(auto it: b){
        if(mp[it]) {
            mp[it]--;
            continue;
        }
        int diff = n-m;
        priority_queue<int> pq;
        pq.push((it+1)/2);
        pq.push(it/2);
        while(pq.size() and diff>0) {
            int val = pq.top();
            pq.pop();
            if(mp[val]) {
                mp[val]--;
            } else {
                pq.push(val/2);
                pq.push((val+1)/2);
            }
            diff--;
        }
        while(pq.size()){
            int val = pq.top();
            pq.pop();
            if(mp[val]) {
                mp[val]--;
            } else {
                no;
                ret;
            }
        }
    }
    
    for(auto it: mp) {
        if(it.ss) {
            no;
            ret;
        }
    }
    yes;
    return 0;
}
 
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
 
    long long t;
    cin >> t;
    while (t--)
 
        solve();
 
    return 0;
}