Problem: 2061D - Kevin and Numbers
Language: c++17 

Solution 1 (runtime: 359 ms):


#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 ==========================================

 
 
 
int solve()
{   
    int n,m;
    cin>>n>>m;

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

    priority_queue<int> pq;  
    mii mp;
    for(auto it: a) mp[it]++;
    for(auto it: b){
       pq.push(it);
    }
    int x = n;

     while(pq.size() and x>0) {
        int val = pq.top();
        if(val==1) break;
        pq.pop(); 
        
        if(mp[val]) {
            mp[val]--;
        } else { 
            pq.push(val/2);
            pq.push((val+1)/2);
        }
        x--;
    }
    x = n;
     while(pq.size() and x>0) {
        int val = pq.top();
        if(val==1) break;
        pq.pop();
        
        if(mp[val]) {
            mp[val]--;
        } else { 
            pq.push(val/2);
            pq.push((val+1)/2);
        }
        x--;
    }

    while(pq.size()){
        int val = pq.top();
        pq.pop();
        if(mp[val]) {
            mp[val]--;
        } else { 
            no;
            ret;
        }
    }
    
    for(auto it: mp) {
        if(it.ss>0) {
            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;
}

Solution 2 (runtime: 437 ms):
#include<bits/stdc++.h>
using namespace std;
#define LL long long

#define N 200007
int a[N];
map<int,int>cnt;

inline void solve(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i],cnt[a[i]]++;
	bool Ans=1;
	for(int i=1;i<=m;i++){
		int b,c0=1,c1=0;
		cin>>b;
		if(!Ans) continue;
		if(cnt[b]){cnt[b]--;continue;}
		if(b&1) c1=c0; else c0=2;
		b>>=1;
		while(b>=1){
			int t0=min(cnt[b],c0),t1=min(cnt[b+1],c1);
			cnt[b]-=t0,cnt[b+1]-=t1,c0-=t0,c1-=t1;
			if((!c0)&&(!c1)) break;
			if(b>1){
				if(b&1) c1=(c1<<1)+c0; else c0=(c0<<1)+c1;
				b>>=1;
			}else{
				if(b==1){
					t0=min(cnt[1],c1<<1);
					cnt[1]-=t0,c1<<=1,c1-=t0;
				}
				break;
			}
		}
		if(c0||c1) Ans=0;
	}
	for(int i=1;i<=n;i++){
		if(cnt[a[i]]) Ans=0;
		cnt[a[i]]=0;
	}
	cout<<(Ans?"Yes":"No")<<'\n';
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	
	int t;
	cin>>t;
	while(t--) solve();
	
	return 0;
}
/*
1
18 8
1 2 10 3 3 2 3 3 2 1 1 1 5 2 1 1 2 2
7 9 1 6 5 2 10 5
*/