#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
using namespace std;
using vpi = vector<pair<int, int>>;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
const int inf = 9e18;
const int mod = 1e9 + 7;
const int NUM = 1000030;
const int N = 10000000;
#define DEBUG(x) cerr << #x << ": " << x << '\n'
template <typename T, typename Y>
void solve()
{
    ll n, m;
    cin >> n >> m;
    vi a(n);
    vi b(m);
    cin >> a >> b;
    map<ll, ll> fa, fb;
    set<ll, greater<ll>> pa, pb;
    for (auto &&i : a)
    {
        fa[i]++;
        pa.insert(i);
    }
    for (auto &&i : b)
    {
        fb[i]++;
        pb.insert(i);
    }
    while (true)
    {
        if (pa.empty() && pb.empty())
        {
            cout << "YES" << endl;
            return;
        }
        if (pa.empty() || pb.empty())
        {
            cout << "NO" << endl;
            return;
        }
        ll mxb = *pb.begin();
        ll mxa = *pa.begin();
        if (mxa > mxb)
        {
            cout << "NO" << endl;
            return;
        }
        if (fa[mxb])
        {
            ll mn = min(fa[mxb], fb[mxb]);
            fa[mxb] -= mn;
            fb[mxb] -= mn;
            if (fa[mxb] == 0)
            {
                pa.erase(mxb);
            }
            if (fb[mxb] == 0)
            {
                pb.erase(mxb);
            }
        }
        else
        {
            ll sb = mxb / 2;
            ll lb = (mxb + 1) / 2;
            fb[sb] += fb[mxb];
            fb[lb] += fb[mxb];
            fb[mxb] = 0;
            pb.insert(sb);
            pb.insert(lb);
            if (fb[mxb] == 0)
            {
                pb.erase(mxb);
            }
        }
    }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
using namespace std;
using vpi = vector<pair<int, int>>;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
const int inf = 9e18;
const int mod = 1e9 + 7;
const int NUM = 1000030;
const int N = 10000000;
#define DEBUG(x) cerr << #x << ": " << x << '\n'
template <typename T, typename Y>
istream &operator>>(istream &is, pair<T, Y> &p)
{
    is >> p.first >> p.second;
    return is;
}
template <typename T, typename Y>
ostream &operator<<(ostream &os, pair<T, Y> p)
{
    os << p.first << ' ' << p.second << ' ';
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    for (auto &i : v)
        is >> i;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> v)
{
    for (auto &i : v)
        os << i << ' ';
    return os;
}

void solve()
{
    ll n, m;
    cin >> n >> m;
    vi a(n);
    vi b(m);
    cin >> a >> b;
    map<ll, ll> fa, fb;
    set<ll, greater<ll>> pa, pb;
    for (auto &&i : a)
    {
        fa[i]++;
        pa.insert(i);
    }
    for (auto &&i : b)
    {
        fb[i]++;
        pb.insert(i);
    }
    while (true)
    {
        if (pa.empty() && pb.empty())
        {
            cout << "YES" << endl;
            return;
        }
        if (pa.empty() || pb.empty())
        {
            cout << "NO" << endl;
            return;
        }
        ll mxb = *pb.begin();
        ll mxa = *pa.begin();
        if (mxa > mxb)
        {
            cout << "NO" << endl;
            return;
        }
        if (fa[mxb])
        {
            ll mn = min(fa[mxb], fb[mxb]);
            fa[mxb] -= mn;
            fb[mxb] -= mn;
            if (fa[mxb] == 0)
            {
                pa.erase(mxb);
            }
            if (fb[mxb] == 0)
            {
                pb.erase(mxb);
            }
        }
        else
        {
            ll sb = mxb / 2;
            ll lb = (mxb + 1) / 2;
            fb[sb] += fb[mxb];
            fb[lb] += fb[mxb];
            fb[mxb] = 0;
            pb.insert(sb);
            pb.insert(lb);
            if (fb[mxb] == 0)
            {
                pb.erase(mxb);
            }
        }
    }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
//                                       Sk Najir
#ifndef ONLINE_JUDGE
#include "db.hpp"
#else
#define dbg(x)
#define dbg2(x, y)
#define dbg3(x, y, z)
#endif
#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

// pre_define
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

// shortcut type
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;

// Constants
const int MOD = 1e9 + 7;

void solve()
{
    int n, m;
    cin >> n >> m;
    multiset<ll> a, b;
    ll x;
    for (int i = 0; i < n; i++)
        cin >> x, a.insert(x);
    for (int i = 0; i < m; i++)
    {
        cin >> x;
        b.insert(x);
    }
    while (!a.empty() and *a.begin() <= *b.begin())
    {
        //dbg(b);
        x = *b.rbegin();
        b.erase(prev(b.end()));
        if (a.find(x) != a.end())
        {
            a.erase(a.find(x));
        }
        else
        {
            if (x % 2)
            {
                if (x == 1)
                {
                    cout << "No" << endl;
                    return;
                }
                b.insert(x / 2);
                b.insert((x / 2) + 1);
            }
            else
            {
                b.insert(x / 2);
                b.insert(x / 2);
            }
        }
    }
    if (a.empty() and b.empty())
    {
        cout << "Yes" << endl;
    }
    else
        cout << "No" << endl;
}

int main()
{
    IOS;
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++)
        solve();
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define vi vector<int>
#define mod 1000000007
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int nn;
    cin >> nn;
    while (nn--)
    {
        int n, m;
        cin >> n >> m;

        multiset<int> a;
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            a.insert(x);
        }

        priority_queue<int> pq;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            pq.push(x);
        }

        while (!pq.empty() && pq.size() <= a.size())
        {
            int x = pq.top();
            pq.pop();
            // cout << x << " ";
            auto it = a.lower_bound(x);
            if (it == a.end() || *it != x)
            {
                // cout << "breaking " << x << endl;
                if (x < 2)
                {
                    pq.push(-1);
                    break;
                }
                pq.push(x / 2);
                pq.push((x + 1) / 2);
            }
            else
            {
                // cout << "erasing " << x << endl;
                a.erase(it);
            }
        }

        cout << (a.empty() && pq.empty() ? "Yes\n" : "No\n");
    }
}

Answer: 2


#include <iostream>
#include <vector>
#include <map>
using namespace std;

// Global frequency map for the numbers of array A.
map<long long, int> freq;

// Recursive function that tries to "explain" x by matching it directly or by splitting.
bool dfs(long long x) {
    // If x is present in A, consume one occurrence and return true.
    if (freq[x] > 0) {
        freq[x]--;
        return true;
    }
    // If x is 1 and not available, we cannot split further.
    if (x == 1) return false;
    // Otherwise, split x into two parts.
    // Note: if x is even, both parts are x/2.
    //       if x is odd, the parts are x/2 and (x+1)/2.
    return dfs(x / 2) && dfs((x + 1) / 2);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;
        vector<long long> A(n), B(m);
        long long sumA = 0, sumB = 0;

        for (int i = 0; i < n; i++){
            cin >> A[i];
            sumA += A[i];
        }
        for (int i = 0; i < m; i++){
            cin >> B[i];
            sumB += B[i];
        }

        // Check the invariant: the total sum must be the same.
        if(sumA != sumB){
            cout << "No\n";
            continue;
        }

        // Build frequency map for array A.
        freq.clear();
        for(auto x : A)
            freq[x]++;

        // Try to "explain" every number in B by splitting it as needed.
        bool possible = true;
        for(auto x : B){
            if(!dfs(x)){
                possible = false;
                break;
            }
        }

        cout << (possible ? "Yes" : "No") << "\n";
    }

    return 0;
}

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while(t--){
        int n, m;
        cin >> n >> m;
        long long sumA = 0, sumB = 0;
        vector<long long> a(n), b(m);

        for (int i = 0; i < n; i++){
            cin >> a[i];
            sumA += a[i];
        }
        for (int i = 0; i < m; i++){
            cin >> b[i];
            sumB += b[i];
        }

        // The sum invariant must hold.
        if(sumA != sumB){
            cout << "No\n";
            continue;
        }

        // Build a frequency map for the original numbers (sequence a).
        unordered_map<long long,int> freq;
        freq.reserve(n * 2);
        for (auto x : a)
            freq[x]++;

        // Use a max-heap to simulate splitting the numbers from b.
        priority_queue<long long> pq;
        for (auto x : b)
            pq.push(x);

        int matched = 0; // number of elements from a that we have "matched"
        bool possible = true;

        while(!pq.empty() && matched < n){
            long long cur = pq.top();
            pq.pop();
            // If cur appears in a then we “take” it.
            if(freq.find(cur) != freq.end() && freq[cur] > 0){
                freq[cur]--;
                matched++;
                continue;
            }
            // If cur is 1 and it is not in a, then we cannot split it further.
            if(cur == 1){
                possible = false;
                break;
            }
            // Otherwise, split cur into the only two valid numbers.
            if(cur % 2 == 0){
                pq.push(cur / 2);
                pq.push(cur / 2);
            } else {
                pq.push(cur / 2);
                pq.push(cur / 2 + 1);
            }
        }

        cout << (matched == n && possible ? "Yes" : "No") << "\n";
    }
    return 0;
}

Answer: 1


// Problem: D. Kevin and Numbers
// Contest: Codeforces - IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2061/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define boost                    \
	ios::sync_with_stdio(false); \
	cin.tie(0);                  \
	cout.tie(0)

///-----new feature----///
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
///-end of new feature-///

#define scan(x)                                                                   \
	do                                                                            \
	{                                                                             \
		while ((x = getchar()) < '0')                                             \
			;                                                                     \
		int _;                                                                    \
		for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \
			;                                                                     \
	} while (0)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define dbg(x) cerr << "  - " << #x << ": " << x << endl;
#define dbgp(x) cerr << "  - " << #x << ": " << x.fi << " , " << x.se << endl;
#define dbg2(x, y) cerr << "  - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl;
#define dbgv(v, n)                                                      \
	cerr << #v << " : " << "\n";                                        \
	for (int i = 0; i < n; i++)                                         \
		cerr << "\t" << #v << "[" << i << "]" << " : " << v[i] << "\n"; \
	cerr << endl;
#define sep() cerr << "--------------------" << endl;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b) - 1; i >= (a); i--)
#define GO(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)

#define nl "\n"
#define YES cout << "YES" << nl
#define NO cout << "NO" << nl
#define yes cout << "yes" << nl
#define no cout << "no" << nl
#define Yes cout << "Yes" << nl
#define No cout << "No" << nl
#define pyprint(x) cout << x << nl
#define errprint(x) cerr << x << nl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

//---> Short Data Structures:
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // aka indexed_set

template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ordered_set<int> s;
// s.insert(1);
// s.order_of_key(3);
// s.find_by_order(1);
// s.find(1);

//---> Graph :
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

//---> Number Theory :
ll gcd(ll a, ll b)
{
	while (b)
	{
		swap(a, b);
		b %= a;
	}
	return (a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll po2(ll b, ll e)
{
	if (e == 0)
		return 1;
	ll res = 1;
	while (e > 1)
	{
		if (e % 2)
			res = (res * b) % mod;
		e /= 2;
		b = (b * b) % mod;
	}
	return (res * b) % mod;
}
ll modInverse(ll a) { return po2(a, mod - 2); }
bool sortFirst(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
	if (a.first == b.first)
	{
		return a.second > b.second;
	}
	return a.first > b.first;
}

void solve() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n), b(m);
    map<ll, int> mpa, mpb;

    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        mpa[a[i]]++;
    }
    for (ll j = 0; j < m; j++) {
        cin >> b[j];
        mpb[b[j]]++;
    }

    // Remove common elements from both maps
    for (auto it : mpa) {
        if (mpb[it.first] != 0) {
            int mn = min(mpa[it.first], mpb[it.first]);
            mpa[it.first] -= mn;
            mpb[it.first] -= mn;
        }
    }

    vector<ll> aa, bb;
    for (auto it : mpa) {
        for (int i = 0; i < it.second; i++) {
            aa.pb(it.first);
        }
    }
    for (auto it : mpb) {
        for (int i = 0; i < it.second; i++) {
            bb.pb(it.first);
        }
    }

    sort(all(aa));
    sort(all(bb));

    ll ans = 0;
    bool t = false;
    vector<int> vis(bb.size(), 0);
    if(bb.size()>0){
    	for (int i = 0; i < (int)aa.size(); i++) {
    	if(t){
    		if(aa[i]-ans>1){
    			NO;
    			return;
    		}
    	}
    	ans+=aa[i];
    	//dbg(ans);
        auto it = lower_bound(all(bb), ans) - bb.begin();
        //dbg(it);
        while (it < bb.size() && vis[it]) {
            it++;
        }
        t = true;
        if (it < bb.size()&&!vis[it]&&ans==bb[it]) {
            vis[it] = 1;
            ans = 0;
            t = false;
        }

    }
    }else{
    	if(aa.size()>0){
    		cout<<"NO"<<endl;
    		return;
    	}
    }

    for (int i = 0; i < bb.size(); i++) {
        if (vis[i] == 0) {
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}

int main()
{
	boost;

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

// Problem: D. Kevin and Numbers
// Contest: Codeforces - IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2061/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define boost                    \
	ios::sync_with_stdio(false); \
	cin.tie(0);                  \
	cout.tie(0)

///-----new feature----///
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
///-end of new feature-///

#define scan(x)                                                                   \
	do                                                                            \
	{                                                                             \
		while ((x = getchar()) < '0')                                             \
			;                                                                     \
		int _;                                                                    \
		for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \
			;                                                                     \
	} while (0)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define dbg(x) cerr << "  - " << #x << ": " << x << endl;
#define dbgp(x) cerr << "  - " << #x << ": " << x.fi << " , " << x.se << endl;
#define dbg2(x, y) cerr << "  - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl;
#define dbgv(v, n)                                                      \
	cerr << #v << " : " << "\n";                                        \
	for (int i = 0; i < n; i++)                                         \
		cerr << "\t" << #v << "[" << i << "]" << " : " << v[i] << "\n"; \
	cerr << endl;
#define sep() cerr << "--------------------" << endl;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b) - 1; i >= (a); i--)
#define GO(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)

#define nl "\n"
#define YES cout << "YES" << nl
#define NO cout << "NO" << nl
#define yes cout << "yes" << nl
#define no cout << "no" << nl
#define Yes cout << "Yes" << nl
#define No cout << "No" << nl
#define pyprint(x) cout << x << nl
#define errprint(x) cerr << x << nl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

//---> Short Data Structures:
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // aka indexed_set

template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ordered_set<int> s;
// s.insert(1);
// s.order_of_key(3);
// s.find_by_order(1);
// s.find(1);

//---> Graph :
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

//---> Number Theory :
ll gcd(ll a, ll b)
{
	while (b)
	{
		swap(a, b);
		b %= a;
	}
	return (a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll po2(ll b, ll e)
{
	if (e == 0)
		return 1;
	ll res = 1;
	while (e > 1)
	{
		if (e % 2)
			res = (res * b) % mod;
		e /= 2;
		b = (b * b) % mod;
	}
	return (res * b) % mod;
}
ll modInverse(ll a) { return po2(a, mod - 2); }
bool sortFirst(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
	if (a.first == b.first)
	{
		return a.second > b.second;
	}
	return a.first > b.first;
}

void solve() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n), b(m);
    map<ll, int> mpa, mpb;

    // Read input and count occurrences
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        mpa[a[i]]++;
    }
    for (ll j = 0; j < m; j++) {
        cin >> b[j];
        mpb[b[j]]++;
    }

    // Remove common elements from both maps
    for (auto it : mpa) {
        if (mpb[it.first] != 0) {
            int mn = min(mpa[it.first], mpb[it.first]);
            mpa[it.first] -= mn;
            mpb[it.first] -= mn;
        }
    }

    vector<ll> aa, bb;
    for (auto it : mpa) {
        for (int i = 0; i < it.second; i++) {
            aa.push_back(it.first);
        }
    }
    priority_queue<ll>q;
    for (auto it : mpb) {
        for (int i = 0; i < it.second; i++) {
            q.push(it.first);
        }
    }

    sort(aa.begin(), aa.end());
    sort(bb.begin(), bb.end());

    ll ans = 0;
    bool t = false;
    vector<int> vis(bb.size(), 0);
	int op = n-m;
    while(!q.empty()&&op>0){
    	ll nb =q.top();
    	q.pop();
    	if(nb%2==0){
    		if(mpa[nb/2]>=2){
    			mpa[nb/2]-=2;
    		}else{
    			if(mpa[nb/2]>=1){
    				mpa[nb/2]-=1;
    				q.push(nb/2);
    			}else{
    				q.push(nb/2);
    				q.push(nb/2);
    			}
    		}
    	}else{
    		if(mpa[nb/2]>=1&&mpa[nb/2+1]>=1){
    			mpa[nb/2]-=1;
    			mpa[nb/2+1]-=1;
    		}else if(mpa[nb/2]>=1){
    			mpa[nb/2]--;
    			q.push(nb/2+1);
    		}else if(mpa[nb/2+1]>=1){
    			mpa[nb/2+1]--;
    			q.push(nb/2);
    		}else{
    			q.push(nb/2);
    			q.push(nb/2+1);
    		}
    	}
    	op--;
    }
    if(q.empty()){
    	for(auto it:mpa){
    		if(it.second>0){
    			cout<<"NO"<<endl;
    			return;
    		}
    	}
    	cout<<"YES"<<endl;
    	return;
    }
    cout<<"NO"<<endl;
}

int main()
{
	boost;

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

Answer: 2


#include<bits/stdc++.h>
using namespace std;

// JAY SHREE KRISHNA
// AUTHOR:- Solanki Nirmal Bhikhabhai

// ------------------------------------------------------------------------------------------------------------
#define hello ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define ld long double
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define pp pop_back
#define endl "\n"
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
// ------------------------------------------------------------------------------------------------------------

vector<ll> llToBin(ll n){
    vector<ll> ans;
    while(n!=0){
        ans.pb(n%2);
        n/=2;
    }
    reverse(all(ans));
    return ans;
}

vector<ll> isprime(1e6+1,1);
void sieve(int n){
    isprime[1]=0;
    for(ll i=2;i*i<1000000;i++){
        if(isprime[i]!=0){
            for(ll j=i*i;j<1000000;j+=i) isprime[j]=0;
        }
    }
}

void printvec(vector<ll> temp){
    for(ll i=0;i<temp.size();i++) cout<<temp[i]<<" ";
    cout<<endl;
}

void printmap(map<ll,ll> temp){
    for(auto it=temp.begin();it!=temp.end();it++) cout<<it->fi<<" "<<it->se<<endl;
}

ll ncr(ll n,ll r){
    ld sum=1;
    for(ll i=0;i<=r;i++) sum=sum*(n-r+i)/i;
    return (ll)sum;
}

// ------------------------------------------------------------------------------------------------------------

void solve(){
    ll n,m;		cin>>n>>m;
    vector<ll> a(n),b(m);
    for(ll i=0;i<n;i++) cin>>a[i];
    for(ll i=0;i<m;i++) cin>>b[i];

    priority_queue<ll,vector<ll>,greater<ll>>pqa (a.begin(),a.end());
    priority_queue<ll,vector<ll>,greater<ll>>pqb (b.begin(),b.end());
    bool flag=true;
    while(!pqa.empty() && !pqb.empty())
    {
        ll a1=pqa.top();
        ll b1=pqb.top();
        //cout<<a1<<" "<<b1<<" "<<endl;
        if(a1>b1)
        {
            flag=false;
            break;
        }
        pqa.pop();
        pqb.pop();
        if(a1==b1)
        {
            //cout<<"Hii"<<endl;
            continue;
        }
        if(pqa.empty())
        {
            flag=false;
            break;
        }
        ll a2=pqa.top();
        pqa.pop();
        //cout<<a2<<endl;
        if(a2==b1)
        {
            pqa.push(a1);
            continue;
        }
        if(a1+a2<b1)
        {
            pqa.push(a1+a2);
            pqb.push(b1);
        }
        else if( a2-a1<=1 && a1+a2==b1) continue;
        else
        {
            flag=false;
            break;
        }
    }
    if(pqa.size()>0 || pqb.size()>0 || !flag) no;
    else yes;
    //cout<<"f "<<flag<<endl;
}

int32_t main(){
    hello;
    int t;			cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

// JAY SHREE KRISHNA
// AUTHOR:- Solanki Nirmal Bhikhabhai

// ------------------------------------------------------------------------------------------------------------
#define hello ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define ld long double
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define pp pop_back
#define endl "\n"
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
// ------------------------------------------------------------------------------------------------------------

vector<ll> llToBin(ll n){
    vector<ll> ans;
    while(n!=0){
        ans.pb(n%2);
        n/=2;
    }
    reverse(all(ans));
    return ans;
}

vector<ll> isprime(1e6+1,1);
void sieve(int n){
    isprime[1]=0;
    for(ll i=2;i*i<1000000;i++){
        if(isprime[i]!=0){
            for(ll j=i*i;j<1000000;j+=i) isprime[j]=0;
        }
    }
}

void printvec(vector<ll> temp){
    for(ll i=0;i<temp.size();i++) cout<<temp[i]<<" ";
    cout<<endl;
}

void printmap(map<ll,ll> temp){
    for(auto it=temp.begin();it!=temp.end();it++) cout<<it->fi<<" "<<it->se<<endl;
}

ll ncr(ll n,ll r){
    ld sum=1;
    for(ll i=0;i<=r;i++) sum=sum*(n-r+i)/i;
    return (ll)sum;
}

// ------------------------------------------------------------------------------------------------------------
bool f(ll val, map<ll,ll>&mp)
{
    //cout<<val<<endl;
    if(mp[val]>0)
    {
        mp[val]--;
        return true;
    }
    if(val==1) return false;
    ll v1=val/2,v2=val/2;
    if(val%2!=0) v2++;
    if(!f(v1,mp)) return false;
    if(!f(v2,mp)) return false;
    return true;
}
void solve(){
    ll n,m;		cin>>n>>m;
    vector<ll> a(n),b(m);
    for(ll i=0;i<n;i++) cin>>a[i];
    for(ll i=0;i<m;i++) cin>>b[i];

    map<ll,ll>mp;
    for(auto it:a) mp[it]++;
    bool flag=true;
    for(auto it:b)
    {
        if(!f(it,mp))
        {
            flag=false;
            break;
        }
    }
    for(auto it:mp)
    {
        if(it.second>0)
        {
            flag=false;
            break;
        }
    }
    if(flag) yes;
    else no;
}

int32_t main(){
    hello;
    int t;			cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int T,n,m,a[200005],b[200005];
priority_queue<int> pq;
map<int,int> ma,mb;
int main(){
    cin>>T;
    for(int i=1;i<=T;i++){
        ma.clear(),mb.clear();
        while(!pq.empty())
        pq.pop();
        cin>>n>>m;
        int op=n-m;
        for(int j=1;j<=n;j++){
            cin>>a[j];
            ma[a[j]]++;
        }
        for(int j=1;j<=m;j++){
            cin>>b[j];
            mb[b[j]]++;
        }
        for (auto it=ma.begin();it!=ma.end();){
            int x=it->first;
            if(mb.count(x)){
                if(ma[x]<mb[x]){
                    mb[x]-=ma[x];
                    it=ma.erase(it);
                }
                else if(ma[x]==mb[x]){
                    mb.erase(x);
                    it=ma.erase(it);
                }
                else{
                    ma[x]-=mb[x];
                    mb.erase(x);
                    it++;
                }
            }
            else
            it++;
        }
        for(auto it=mb.begin();it!=mb.end();it++){
            int x=it->first;
            pq.push(x);
        }
        while((!pq.empty())&&op>0){
            int x=pq.top();
            pq.pop();
            if(x==1)
            break;
            if(x%2){
                if(ma.count(x/2+1)){
                    if(ma[x/2+1]<mb[x]){
                        ma.erase(x/2+1);
                        if(!mb.count(x/2+1))
                        pq.push(x/2+1);
                        mb[x/2+1]+=mb[x]-a[x/2+1];
                    }
                    else if(ma[x/2+1]==mb[x])
                    ma.erase(x/2+1);
                    else
                    ma[x/2+1]-=mb[x];
                }
                else{
                    if(!mb.count(x/2+1))
                    pq.push(x/2+1);
                    mb[x/2+1]+=mb[x];
                }
                if(ma.count(x/2)){
                    if(ma[x/2]<mb[x]){
                        ma.erase(x/2);
                        if(!mb.count(x/2))
                        pq.push(x/2);
                        mb[x/2]+=mb[x]-a[x/2];
                    }
                    else if(ma[x/2]==mb[x])
                    ma.erase(x/2);
                    else
                    ma[x/2]-=mb[x];
                }
                else{
                    if(!mb.count(x/2))
                    pq.push(x/2);
                    mb[x/2]+=mb[x];
                }
            }
            else{
                if(ma.count(x/2)){
                    if(ma[x/2]<mb[x]*2){
                        ma.erase(x/2);
                        if(!mb.count(x/2))
                        pq.push(x/2);
                        mb[x/2]+=mb[x]*2-a[x/2];
                    }
                    else if(ma[x/2]==mb[x]*2)
                    ma.erase(x/2);
                    else
                    ma[x/2]-=mb[x]*2;
                }
                else{
                    if(!mb.count(x/2))
                    pq.push(x/2);
                    mb[x/2]+=mb[x]*2;
                }
            }
            op-=mb[x];
            mb.erase(x);
        }
        if(ma.empty()&&mb.empty()&&!op)
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
int T,n,m,a[200005],b[200005];
priority_queue<int> pq;
map<int,int> ma,mb;
int main(){
    cin>>T;
    for(int i=1;i<=T;i++){
        ma.clear(),mb.clear();
        while(!pq.empty())
        pq.pop();
        cin>>n>>m;
        int op=n-m;
        for(int j=1;j<=n;j++){
            cin>>a[j];
            ma[a[j]]++;
        }
        for(int j=1;j<=m;j++){
            cin>>b[j];
            mb[b[j]]++;
        }
        for (auto it=ma.begin();it!=ma.end();){
            int x=it->first;
            if(mb.count(x)){
                if(ma[x]<mb[x]){
                    mb[x]-=ma[x];
                    it=ma.erase(it);
                }
                else if(ma[x]==mb[x]){
                    mb.erase(x);
                    it=ma.erase(it);
                }
                else{
                    ma[x]-=mb[x];
                    mb.erase(x);
                    it++;
                }
            }
            else
            it++;
        }
        for(auto it=mb.begin();it!=mb.end();it++){
            int x=it->first;
            pq.push(x);
        }
        while((!pq.empty())&&op>0){
            int x=pq.top();
            pq.pop();
            if(x==1)
            break;
            if(x%2){
                if(ma.count(x/2+1)){
                    if(ma[x/2+1]<mb[x]){
                        if(!mb.count(x/2+1))
                        pq.push(x/2+1);
                        mb[x/2+1]+=mb[x]-ma[x/2+1];
                        ma.erase(x/2+1);
                    }
                    else if(ma[x/2+1]==mb[x])
                    ma.erase(x/2+1);
                    else
                    ma[x/2+1]-=mb[x];
                }
                else{
                    if(!mb.count(x/2+1))
                    pq.push(x/2+1);
                    mb[x/2+1]+=mb[x];
                }
                if(ma.count(x/2)){
                    if(ma[x/2]<mb[x]){
                        if(!mb.count(x/2))
                        pq.push(x/2);
                        mb[x/2]+=mb[x]-ma[x/2];
                        ma.erase(x/2);
                    }
                    else if(ma[x/2]==mb[x])
                    ma.erase(x/2);
                    else
                    ma[x/2]-=mb[x];
                }
                else{
                    if(!mb.count(x/2))
                    pq.push(x/2);
                    mb[x/2]+=mb[x];
                }
            }
            else{
                if(ma.count(x/2)){
                    if(ma[x/2]<mb[x]*2){
                        if(!mb.count(x/2))
                        pq.push(x/2);
                        mb[x/2]+=mb[x]*2-ma[x/2];
                        ma.erase(x/2);
                    }
                    else if(ma[x/2]==mb[x]*2)
                    ma.erase(x/2);
                    else
                    ma[x/2]-=mb[x]*2;
                }
                else{
                    if(!mb.count(x/2))
                    pq.push(x/2);
                    mb[x/2]+=mb[x]*2;
                }
            }
            op-=mb[x];
            mb.erase(x);
        }
        if(ma.empty()&&mb.empty()&&!op)
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
}

Answer: 2


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
#define MOD 1000000007
#define MAX_N 200000

// vector<long long> fact(MAX_N + 1), inv_fact(MAX_N + 1);

// long long mod_inverse(long long x, long long mod) {
//     long long result = 1;
//     long long power = mod - 2;
//     while (power) {
//         if (power & 1) result = result * x % mod;
//         x = x * x % mod;
//         power >>= 1;
//     }
//     return result;
// }

// void precompute_factorials() {
//     fact[0] = inv_fact[0] = 1;
//     for (int i = 1; i <= MAX_N; ++i) {
//         fact[i] = fact[i - 1] * i % MOD;
//     }
//     inv_fact[MAX_N] = mod_inverse(fact[MAX_N], MOD);
//     for (int i = MAX_N - 1; i >= 1; --i) {
//         inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD;
//     }
//     inv_fact[0] = 1;
// }

// long long comb(int n, int k) {
//     if (k > n || k < 0) return 0;
//     return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD;
// }

void solve()
{
    int n, m;
    cin >> n >> m;
    priority_queue < ll, vector < ll > , greater < ll >> q;
    map < ll, int > mp;
    for (int i = 0; i < n; i++)
    {
        int val;
        cin >> val;
        q.push(val);
    }
    for (int i = 0; i < m; i++)
    {
        int val;
        cin >> val;
        mp[val]++;
    }

    while (!q.empty() && n >= m)
    {
        ll first = q.top();
        q.pop();
        n--;
        if (mp[first] > 0)
        {
            mp[first]--;
            m--;
        }
        else if (!q.empty() && abs(first - q.top()) <= 1)
        {
            if (mp[first + q.top()] > 0) {
                mp[first + q.top()]--;
                q.pop();
                n--;
                m--;
            }
            else
            {
                ll val = q.top();
                q.pop();
                q.push({
                    first + val
                });

            }

        }
        else
        {
            cout << "NO" << "\n";
            return;
        }
    }
    if(m==0)
    cout << "YES" << "\n";
    else
    cout<<"NO"<<"\n";

}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {

        solve();

    }
    return 0;

}

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
#define MOD 1000000007
#define MAX_N 200000

// vector<long long> fact(MAX_N + 1), inv_fact(MAX_N + 1);

// long long mod_inverse(long long x, long long mod) {
//     long long result = 1;
//     long long power = mod - 2;
//     while (power) {
//         if (power & 1) result = result * x % mod;
//         x = x * x % mod;
//         power >>= 1;
//     }
//     return result;
// }

// void precompute_factorials() {
//     fact[0] = inv_fact[0] = 1;
//     for (int i = 1; i <= MAX_N; ++i) {
//         fact[i] = fact[i - 1] * i % MOD;
//     }
//     inv_fact[MAX_N] = mod_inverse(fact[MAX_N], MOD);
//     for (int i = MAX_N - 1; i >= 1; --i) {
//         inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD;
//     }
//     inv_fact[0] = 1;
// }

// long long comb(int n, int k) {
//     if (k > n || k < 0) return 0;
//     return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD;
// }

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

    map < ll, int > mp;

    for (int i = 0; i < n; i++)
    {
        int val;
        cin >> val;
        mp[val]++;

    }
    priority_queue < int, vector < int > , greater < int >> q;

    for (int i = 0; i < m; i++)
    {
        int val;
        cin >> val;
        q.push(val);

    }
    int c = n - m;
    while (!q.empty() && c)
    {
        if (mp[q.top()] > 0)
        {
            mp[q.top()]--;
            q.pop();
        }
        else
        {
            int val = q.top();
            q.pop();
            q.push(val / 2);
            q.push(ceil(val / 2.0));
            c--;
        }

    }

    while (!q.empty())
    {
        if (mp[q.top()] == 0)
        {
            cout << "NO\n";
            return;
        }
        mp[q.top()]--;
        q.pop();
    }
    for (auto i: mp)
    {
        if (i.second)
        {
            cout << "NO\n";
            return;
        }
    }
    cout << "YES" << "\n";

}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {

        solve();

    }
    return 0;

}

Answer: 2


#include<bits/stdc++.h>
#define int long long
using namespace std;

bool solve(){
    int n,m,x;
    cin>>n>>m;
    priority_queue<int> a,b;
    for(int i=0;i<n;i++){
        cin>>x;
        a.push(x);
    }
    for(int j=0;j<m;j++){
        cin>>x;
        b.push(x);
    }
    int flag=0;
    while(a.size()&&b.size()){
        if(a.top()>b.top()||a.size()<b.size())return 0;
        if(a.top()==b.top()){a.pop();b.pop();continue;}
        int u=b.top();
        b.pop();
        b.push(u>>1);
        b.push(u+1>>1);
    }
    return a.empty()&&b.empty();
}
signed main(){
    int t;
    cin>>t;
    while(t--){
        cout<<(solve()?"Yes":"No")<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N=2e5+50;
int a[N],b[N];

signed main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        map<int,int> p2;
        int sum1=0,sum2=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
            sum1+=a[i];
        }
        for(int j=0;j<m;j++){
            cin>>b[j];
            p2[b[j]]++;
            sum2+=b[j];
        }
        if(sum1!=sum2){
            cout<<"No"<<endl;
            continue;
        }
        vector<int> v;
        sort(a,a+n);
        for(int i=0;i<n;i++){
            if(p2[a[i]]>=1){
                p2[a[i]]--;
            }else{

                if(!v.empty()){
                    if(p2[v.back()]>=1){
                        p2[v.back()]--;
                        v.pop_back();
                    }
                    if(!v.empty()&&abs(v.back()-a[i])<=1){
                        int u=v.back();
                        v.pop_back();
                        v.push_back(a[i]+u);
                     //   cout<<v.back()<<endl;
                    }else{
                        v.push_back(a[i]);
                    }
                    if(!v.empty()&&p2[v.back()]>=1){
                        p2[v.back()]--;
                        v.pop_back();
                    }
                }else{
                    v.push_back(a[i]);
                }
            }
        }
        int flag=0;
        for(auto i:p2){
            if(i.second>0){
                flag=1;
                break;
            }
        }
        if(!v.empty()||flag){
            cout<<"No"<<endl;
        }else{
            cout<<"Yes"<<endl;
        }
    }
    return 0;
}
// 1中和相等
// 2 最大 小于 下面
// 3如果长度相等 判断 是否一样
// 4

Answer: 1


#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// #pragma GCC target("avx2,sse4.2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long int
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define vi vector <int>
#define vl vector <long long int>
#define all(a) a.begin(),a.end()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rev(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.141592653589793238462643383279
#define TEMP_SIZE 1000005
using namespace std;
using namespace __gnu_pbds;

const long long INF = (long long)1e15;
long double eps = 1e-6;
const ll MOD = 998244353;

// orderedset

template<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;

// vector <int> ad[TEMP_SIZE], newgraph[TEMP_SIZE];
// ll vis[TEMP_SIZE];

vector<ll> fact;
vector<ll> inv;

long long pow(long long a, long long b)
{

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

void factinit(ll MOD)
{
    fact.assign(TEMP_SIZE, 0);
    fact[0] = 1;
    for (ll i = 1; i < TEMP_SIZE; i++)
    {
        fact[i] = (fact[i - 1] * i) % MOD;
    }
}

long long powm(long long a, long long b, long long m)
{

    a %= m;

    long long res = 1;
    while (b > 0)
    {

        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b /= 2;
    }

    return res;
}

void invinit()
{
    inv.assign(TEMP_SIZE, 0);
    inv[1] = 1;
    for (ll i = 2; i < TEMP_SIZE; i++)
    {
        inv[i] = powm(i, MOD - 2, MOD);
    }
}

ll nCr(ll n, ll r)
{
    return ((fact[n] * powm(fact[r], MOD - 2, MOD)) % MOD * powm(fact[n - r], MOD - 2, MOD)) % MOD;
}

bool compare(const pair<ll, ll> &i, const pair<ll, ll> &j)
{
    return i.second < j.second;
}

ll mex(vector<ll> &arr, ll N)
{
    sort(arr.begin(),arr.end());
    int mex = 0;
    for (int idx = 0; idx < N; idx++)
    {
        if (arr[idx] == mex)
        {
            mex += 1;
        }
    }

    return mex;
}

ll sod(ll n)
{
    ll sum = 0;
    while (n)
    {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

ll poss(ll a[], ll n, ll k, ll mid)
{
    map<ll, ll> f;
    rep(i, 0, n)
    {
        f[a[i]]++;
    }
    ll sum = 0, need = mid * k;
    for (auto i : f)
    {
        sum += min(mid, i.second);
    }
    if (sum >= need)
        return 1;
    else
        return 0;
}

double nthRoot(ll A, ll n)
{
    double val = round(exp(log(A) / n) * 1e9) / 1e9;
    return val;
}

vector <bool> vis;
vector <ll> ans;
int component_index = 0;

void dfs(vector <vector <int> > &g, int node)
{

    vis[node] = true;
    for (auto p: g[node])
    {
        if (!vis[p])
        {
            dfs(g, p);
        }
    }
    return;
}

void solve()
{
    int n, m;
    cin>>n>>m;
    vi a(n), b(m);
    rep(i,0,n) cin>>a[i];
    rep(i,0,m) cin>>b[i];

    multiset <int> m1;

    sort(all(a));

    rep(i,0,m) m1.insert(b[i]);

    int cnt = n-m;

    int i = n-1;
    while (!m1.empty() && i>=0)
    {
        auto it = next(m1.begin(), m1.size()-1);
        int val = *it;
        m1.erase(it);
        if (val == a[i])
        {
            i--;
        }
        else if (val < a[i])
        {
            cout<<"No"<<endl;
            return;
        }
        else
        {
            if (cnt == 0)
            {
                cout<<"No"<<endl;
                return;
            }
            else
            {
                cnt--;
            }
            m1.insert(val/2);
            m1.insert((val+1)/2);
        }
    }

    if (m1.size()!=0 || i>=0)
    {
        cout<<"No"<<endl;
        return;
    }

    cout<<"Yes"<<endl;
    return;
}

int main()
{
    // factinit(MOD);
    // invinit();

    #ifndef ONLINE_JUDGE
                // freopen("Input.txt", "r", stdin);
                // freopen("Output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t = 1;
    cin>>t;

    while (t--)
    {
        solve();
    }
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// #pragma GCC target("avx2,sse4.2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long int
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define vi vector <int>
#define vl vector <long long int>
#define all(a) a.begin(),a.end()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rev(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.141592653589793238462643383279
#define TEMP_SIZE 1000005
using namespace std;
using namespace __gnu_pbds;

const long long INF = (long long)1e15;
long double eps = 1e-6;
const ll MOD = 998244353;

// orderedset

template<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;

// vector <int> ad[TEMP_SIZE], newgraph[TEMP_SIZE];
// ll vis[TEMP_SIZE];

vector<ll> fact;
vector<ll> inv;

long long pow(long long a, long long b)
{

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

void factinit(ll MOD)
{
    fact.assign(TEMP_SIZE, 0);
    fact[0] = 1;
    for (ll i = 1; i < TEMP_SIZE; i++)
    {
        fact[i] = (fact[i - 1] * i) % MOD;
    }
}

long long powm(long long a, long long b, long long m)
{

    a %= m;

    long long res = 1;
    while (b > 0)
    {

        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b /= 2;
    }

    return res;
}

void invinit()
{
    inv.assign(TEMP_SIZE, 0);
    inv[1] = 1;
    for (ll i = 2; i < TEMP_SIZE; i++)
    {
        inv[i] = powm(i, MOD - 2, MOD);
    }
}

ll nCr(ll n, ll r)
{
    return ((fact[n] * powm(fact[r], MOD - 2, MOD)) % MOD * powm(fact[n - r], MOD - 2, MOD)) % MOD;
}

bool compare(const pair<ll, ll> &i, const pair<ll, ll> &j)
{
    return i.second < j.second;
}

ll mex(vector<ll> &arr, ll N)
{
    sort(arr.begin(),arr.end());
    int mex = 0;
    for (int idx = 0; idx < N; idx++)
    {
        if (arr[idx] == mex)
        {
            mex += 1;
        }
    }

    return mex;
}

ll sod(ll n)
{
    ll sum = 0;
    while (n)
    {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

ll poss(ll a[], ll n, ll k, ll mid)
{
    map<ll, ll> f;
    rep(i, 0, n)
    {
        f[a[i]]++;
    }
    ll sum = 0, need = mid * k;
    for (auto i : f)
    {
        sum += min(mid, i.second);
    }
    if (sum >= need)
        return 1;
    else
        return 0;
}

double nthRoot(ll A, ll n)
{
    double val = round(exp(log(A) / n) * 1e9) / 1e9;
    return val;
}

vector <bool> vis;
vector <ll> ans;
int component_index = 0;

void dfs(vector <vector <int> > &g, int node)
{

    vis[node] = true;
    for (auto p: g[node])
    {
        if (!vis[p])
        {
            dfs(g, p);
        }
    }
    return;
}

void solve()
{
    int n, m;
    cin>>n>>m;
    vi a(n), b(m);
    rep(i,0,n) cin>>a[i];
    rep(i,0,m) cin>>b[i];

    sort(all(a));

    priority_queue<int> pq;
    rep(i,0,m) pq.push(b[i]);

    int cnt = n-m;

    int i = n-1;
    while (!pq.empty() && i>=0)
    {

        int val = pq.top();
        pq.pop();

        if (val == a[i])
        {
            i--;
        }
        else if (val < a[i])
        {
            cout<<"No"<<endl;
            return;
        }
        else
        {
            if (cnt <= 0)
            {
                cout<<"No"<<endl;
                return;
            }
            else
            {
                cnt--;
            }
            pq.push(val/2);
            pq.push((val+1)/2);
        }
    }

    if (!pq.empty() || i>=0)
    {
        cout<<"No"<<endl;
        return;
    }

    cout<<"Yes"<<endl;
    return;
}

int main()
{
    // factinit(MOD);
    // invinit();

    #ifndef ONLINE_JUDGE
                // freopen("Input.txt", "r", stdin);
                // freopen("Output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t = 1;
    cin>>t;

    while (t--)
    {
        solve();
    }
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pp pair<int,int>
const int N = 2e5 + 9;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;

void solve() {
    int n, m;cin >> n >> m;
    map<int, int> mp;
    for (int i = 0;i < n;i++) {
        int x;cin >> x;
        mp[x]++;
    }
    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 0;i < m;i++) {
        int x;cin >> x;
        pq.push(x);
    }
    while (pq.size()) {
        int x = pq.top();pq.pop();
        if (mp[x])mp[x]--;
        else {
            if (x == 1) {
                cout << "No\n";return;
            }
            else if (x % 2) {
                pq.push(x / 2);pq.push(x / 2+1);
            }
            else {
                pq.push(x / 2);pq.push(x / 2);
            }
        }
    }
    for(auto[x,y]:mp){
        if(y){
            cout << "No\n";return;
        }
    }
    cout << "Yes\n";
}
signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pp pair<int,int>
const int N = 2e5 + 9;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;

void solve() {
    int n, m;cin >> n >> m;
    map<int, int> mp;
    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 0;i < n;i++) {
        int a;cin >> a;pq.push(a);
    }
    for (int i = 0;i < m;i++) {
        int a;cin >> a;
        mp[a]++;
    }

    while (pq.size()>=2) {
        while (pq.size()) {
            int a = pq.top();
            if (mp[a] > 0) {
                mp[a]--;pq.pop();
            }
            else break;
        }
        if (pq.size() >= 2) {
            int a = pq.top();pq.pop();
            int b = pq.top();pq.pop();
            if (b - a <= 1) {
                pq.push(b + a);
            }
            else {
                cout << "No\n";return;
            }
        }
    }
    if (pq.size() == 1) {
        if(mp[pq.top()] > 0){mp[pq.top()]--;pq.pop();}
    }
    if(pq.size() > 0){
        cout << "No\n";return;
    }
    for (auto [x, y] : mp) {
        if(y > 0){
            cout << "No\n";return;
        }
    }
    cout << "Yes\n";
}
signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)solve();
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

#define fast_cin()                    \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define ll long long
#define vi vector<ll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second

const ll MOD = 1000000007;
struct mint
{
    int v;
    explicit operator int() const { return v; }
    mint() : v(0) {}
    mint(ll _v) : v(int(_v % MOD)) { v += (v < 0) * MOD; }
};
mint &operator+=(mint &a, mint b)
{
    if ((a.v += b.v) >= MOD)
        a.v -= MOD;
    return a;
}
mint &operator-=(mint &a, mint b)
{
    if ((a.v -= b.v) < 0)
        a.v += MOD;
    return a;
}
mint operator+(mint a, mint b) { return a += b; }
mint operator-(mint a, mint b) { return a -= b; }
mint operator*(mint a, mint b) { return mint((ll)a.v * b.v); }
mint &operator*=(mint &a, mint b) { return a = a * b; }
mint pow(mint a, ll p)
{
    assert(p >= 0);
    return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);
}
mint inv(mint a)
{
    assert(a.v != 0);
    return pow(a, MOD - 2);
}
mint operator/(mint a, mint b) { return a * inv(b); }
bool operator==(mint a, mint b) { return a.v == b.v; }

void solve()
{
    int n, m;
    cin >> n >> m;
    priority_queue<int> pq1, pq2;
    while (n--)
    {
        int x;
        cin >> x;
        pq1.push(x);
    }
    while (m--)
    {
        int x;
        cin >> x;
        pq2.push(x);
    }
    while (pq1.size() && pq2.size())
    {
        if (pq1.top() == pq2.top())
        {
            pq1.pop();
            pq2.pop();
        }
        else if (pq1.top() > pq2.top() || pq1.size() < pq2.size())
        {
            cout << "no" << endl;
            return;
        }
        else
        {
            int a = pq2.top();
            pq2.pop();
            pq2.push(a / 2);
            pq2.push((a + 1) / 2);
        }
    }
    if (pq1.size() || pq2.size())
    {
        cout << "no" << endl;
        return;
    }
    cout << "yes" << endl;
}

int main()
{
    fast_cin();
    ll t;
    cin >> t;
    for (int it = 1; it <= t; it++)
    {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define fast_cin()                    \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define ll long long
#define vi vector<ll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second

const ll MOD = 1000000007;
struct mint
{
    int v;
    explicit operator int() const { return v; }
    mint() : v(0) {}
    mint(ll _v) : v(int(_v % MOD)) { v += (v < 0) * MOD; }
};
mint &operator+=(mint &a, mint b)
{
    if ((a.v += b.v) >= MOD)
        a.v -= MOD;
    return a;
}
mint &operator-=(mint &a, mint b)
{
    if ((a.v -= b.v) < 0)
        a.v += MOD;
    return a;
}
mint operator+(mint a, mint b) { return a += b; }
mint operator-(mint a, mint b) { return a -= b; }
mint operator*(mint a, mint b) { return mint((ll)a.v * b.v); }
mint &operator*=(mint &a, mint b) { return a = a * b; }
mint pow(mint a, ll p)
{
    assert(p >= 0);
    return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);
}
mint inv(mint a)
{
    assert(a.v != 0);
    return pow(a, MOD - 2);
}
mint operator/(mint a, mint b) { return a * inv(b); }
bool operator==(mint a, mint b) { return a.v == b.v; }

multiset<int> f(int target, multiset<int> &s)
{
    // cout << target << endl;
    if (s.find(target) != s.end())
    {
        s.erase(s.find(target));
        return {target};
    }
    if (s.empty() || target < *s.begin())
        return {-1};
    // cout << target / 2 << endl;
    multiset<int> temp = f(target / 2, s);
    if (temp.find(-1) != temp.end())
        return {-1};
    // cout << (target + 1) / 2 << endl;
    multiset<int> temp2 = f((target + 1) / 2, s);
    if (temp2.find(-1) != temp2.end())
        return {-1};
    temp.insert(all(temp2));
    return temp;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    multiset<int> s1, s2, s;
    for (int i = 0; i < n; i++)
    {
        ll x;
        cin >> x;
        s1.insert(x);
    }
    for (int i = 0; i < m; i++)
    {
        ll x;
        cin >> x;
        if (s1.find(x) != s1.end())
            s1.erase(s1.find(x));
        else
            s2.insert(x);
    }
    if (m > n || (s2.size() == 0 && s1.size() > 0))
    {
        cout << "NO" << endl;
        return;
    }
    if (s1.size() == 0)
    {
        cout << "YES" << endl;
        return;
    }
    for (auto rit = s2.rbegin(); rit != s2.rend(); rit++)
    {
        s = s1;
        multiset<int> ans = f(*rit, s);
        if (ans.find(-1) != ans.end())
        {
            cout << "NO" << endl;
            return;
        }
        s1.swap(s);
    }
    // cout << s1.size() << endl;
    if (s1.size() == 0)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
}

int main()
{
    fast_cin();
    ll t;
    cin >> t;
    for (int it = 1; it <= t; it++)
    {
        solve();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back

const int mxN = 2e5 + 5;

int a[mxN], b[mxN];

void solve(){

    int n, m; cin >> n >> m;

    ll sm = 0;

    for (int i = 0; i < n; i++) cin >> a[i], sm += a[i];

    for (int i = 0; i < m; i++) cin >> b[i], sm -= b[i];

    if(sm != 0) { cout << "NO\n"; return; }

    priority_queue<int> pqa, pqb; for (int i = 0; i < m; i++) pqb.push(b[i]); for(int i = 0; i < n; i++) pqa.push(a[i]);

    while(!pqa.empty() && !pqb.empty()){

        int x = pqa.top(); pqa.pop();

        int y = pqb.top(); pqb.pop();

        if (x == y) continue;

        if ( x > y || y == 1) { cout << "NO\n"; return; }

        pqb.push(y / 2); pqb.push((y + 1) / 2); pqa.push(x);

    }

    cout << (pqa.empty() && pqb.empty() ? "YES\n" : "NO\n");

}

int main(){

    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t; cin >> t;

    while(t--) solve();

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back

const int mxN = 2e5 + 5;

int a[mxN], b[mxN];

void solve(){

    int n, m; cin >> n >> m;

    ll sm = 0;

    for (int i = 0; i < n; i++) cin >> a[i], sm += a[i];

    for (int i = 0; i < m; i++) cin >> b[i], sm -= b[i];

    if(sm != 0) { cout << "NO\n"; return; }

    priority_queue<int> pqa, pqb; for (int i = 0; i < m; i++) pqb.push(b[i]); for(int i = 0; i < n; i++) pqa.push(a[i]);

    while(!pqa.empty() && !pqb.empty()){

        int x = pqa.top(); pqa.pop();

        int y = pqb.top(); pqb.pop();

        if (x == y) continue;

        if (y == 1) { cout << "NO\n"; return; }

        pqb.push(y / 2); pqb.push((y + 1) / 2); pqa.push(x);

    }

    cout << (pqa.empty() && pqb.empty() ? "YES\n" : "NO\n");

}

int main(){

    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t; cin >> t;

    while(t--) solve();

    return 0;

}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vl vector<ll>

bool rec(ll x, multiset<ll> &s) {
    auto it = s.find(x);
    if (it != s.end()) {
        s.erase(it);
        return true;
    }
    if (x == 1)
        return false;
    ll l = x / 2;
    ll r = x - l;
    return rec(l, s) && rec(r, s);
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 0;
    cin >> t;
    while(t--) {
        int n = 0, m = 0;
        cin >> n >> m;
        vl a(n), b(m);
        ll suma = 0, sumb = 0;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            suma += a[i];
        }
        for (int i = 0; i < m; i++) {
            cin >> b[i];
            sumb += b[i];
        }
        if(suma != sumb) {
            cout << "No\n";
            continue;
        }
        multiset<ll> s;
        for(auto x : a)
            s.insert(x);
        sort(b.begin(), b.end(), greater<ll>());
        bool possible = true;
        for(auto x : b) {
            if(!rec(x, s)){
                possible = false;
                break;
            }
        }
        cout << (possible && s.empty() ? "Yes" : "No") << "\n";
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vl vector<ll>
#define uli unordered_map<ll, ll>

struct Node {
  ll val, cnt;
};

struct cmp {
  bool operator() (const Node &a, const Node &b) {
    return a.val < b.val;
  }
};

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int t = 0;
  cin >> t;
  while (t--) {
    int n = 0, m = 0;
    cin >> n >> m;
    vl a(n), b(m);
    ll suma = 0, sumb = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      suma += a[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> b[i];
      sumb += b[i];
    }
    if (suma != sumb) {
      cout << "No\n";
      continue;
    }
    uli freq;
    freq.reserve(n * 2);
    for (ll x : a) {
      freq[x]++;
    }
    uli board;
    board.reserve(m * 2);
    for (ll x : b) {
      board[x]++;
    }
    priority_queue<Node, vector<Node>, cmp> pq;
    for (auto &p : board) {
      pq.push(Node{p.first, p.second});
    }
    bool possible = true;
    ll matched = 0;
    const ll total = n;
    while (!pq.empty() && matched < total) {
      Node cur = pq.top();
      pq.pop();
      if (board.find(cur.val) == board.end() || board[cur.val] != cur.cnt)
        continue;
      ll x = cur.val;
      ll cnt = cur.cnt;
      if (freq.find(x) != freq.end()) {
        ll use = min(freq[x], cnt);
        freq[x] -= use;
        matched += use;
        cnt -= use;
      }
      board.erase(x);
      if (cnt > 0) {
        if (x == 1) {
          possible = false;
          break;
        }
        ll l = x / 2;
        ll r = x - l;
        board[l] += cnt;
        board[r] += cnt;
        pq.push(Node{l, board[l]});
        pq.push(Node{r, board[r]});
      }
    }
    for (auto &p : freq) {
      if (p.second > 0) {
        possible = false;
        break;
      }
    }
    cout << (possible ? "Yes" : "No") << "\n";
  }
  return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << "\033[0m" << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr<<"\033[1;31m"<< "LINE "<<__LINE__<<": -> [" << #__VA_ARGS__ << "]:", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 19
#endif

#define int int64_t
#define sz(x) int(x.size())

bool check(multiset<int>& A, multiset<int>& B){
    if(sz(A) and sz(B)){
        int mxA = *A.rbegin();
        int mxB = *B.rbegin();
        if(mxB < mxA){
            return false;
        }
        int mnA = *A.begin();
        int mnB = *B.begin();
        if(mnB < mnA){
            return false;
        }
        return true;
    } else{
        return false;
    }
}

void solve(int tt){
    int N, M; cin >> N >> M;
    multiset<int> A, B;
    for(int i = 0; i < N; ++i){
        int x; cin >> x;
        A.insert(x);
    }
    for(int i = 0; i < M; ++i){
        int x; cin >> x;
        B.insert(x);
    }

    vector<int> toRemove;
    map<int, int> freqB;
    for(int x : B){
        freqB[x]++;
    }
    map<int, int> freqA;
    for(int x : A){
        freqA[x]++;
    }

    map<int, int> freq;
    for(auto [k, v] : freqB){
        if(freqA.count(k)){
            freq[k] = min(v, freqA[k]);
        }
    }

    for(auto [k, v] : freq){
        for(int i = 0; i < v; ++i){
            A.extract(k);
            B.extract(k);
        }
    }

    // overlap B to A
    while(true){
        dbg(A, B);
        if(sz(A) == sz(B) and sz(A) == 0){
            break;
        }
        dbg(check(A, B));
        if(!check(A, B)){
            cout << "No\n";
            return;
        } else{
            int mn = *B.begin();
            int a = mn/2;
            int b = mn - a;
            B.extract(mn);
            B.insert(a);
            B.insert(b);
            dbg(A, B);
            vector<int> toRemove;
            if(B.find(a) != B.end() and A.find(a) != A.end()){
                toRemove.push_back(a);
            }
            if(sz(toRemove)){
                B.extract(toRemove.back());
                A.extract(toRemove.back());
                toRemove.clear();
            }
            if(B.find(b) != B.end() and A.find(b) != A.end()){
                toRemove.push_back(b);
            }
            if(sz(toRemove)){
                B.extract(toRemove.back());
                A.extract(toRemove.back());
                toRemove.clear();
            }
        }
    }
    cout << "Yes\n";
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T=1; cin >> T;
    for(int tt = 1; tt <= T; ++tt){
        solve(tt);
    }
}

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << "\033[0m" << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr<<"\033[1;31m"<< "LINE "<<__LINE__<<": -> [" << #__VA_ARGS__ << "]:", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 19
#endif

#define int int64_t
#define sz(x) int(x.size())

bool check(multiset<int>& A, multiset<int>& B){
    if(sz(A) and sz(B)){
        int mxA = *A.rbegin();
        int mxB = *B.rbegin();
        if(mxB < mxA){
            return false;
        }
        int mnA = *A.begin();
        int mnB = *B.begin();
        if(mnB < mnA){
            return false;
        }
        return true;
    } else{
        return false;
    }
}

void solve(int tt){
    int N, M; cin >> N >> M;
    multiset<int> A, B;
    for(int i = 0; i < N; ++i){
        int x; cin >> x;
        A.insert(x);
    }
    for(int i = 0; i < M; ++i){
        int x; cin >> x;
        B.insert(x);
    }

    vector<int> toRemove;
    for(int x : B){
        if(A.find(x) != A.end()){
            toRemove.push_back(x);
        }
    }
    dbg(A, B, toRemove);
    for(int x : toRemove){
        A.extract(x);
        B.extract(x);
    }

    // overlap B to A
    while(true){
        dbg(A, B);
        if(sz(A) == sz(B) and sz(A) == 0){
            break;
        }
        if(!check(A, B)){
            cout << "No\n";
            return;
        } else{
            int mn = *B.begin();
            int a = mn/2;
            int b = mn - a;
            B.extract(mn);
            B.insert(a);
            B.insert(b);
            dbg(A, B);
            vector<int> toRemove;
            if(B.count(a) and A.count(a)){
                toRemove.push_back(a);
            }
            if(B.count(b) and A.count(b)){
                toRemove.push_back(b);
            }
            for(int x : toRemove){
                B.extract(x);
                A.extract(x);
                if((!sz(A) and sz(B)) or (sz(A) and !sz(B))){
                    cout << "No\n";
                    return;
                }
            }
        }
    }
    cout << "Yes\n";
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T=1; cin >> T;
    for(int tt = 1; tt <= T; ++tt){
        solve(tt);
    }
}

Answer: 1


#include <bits/stdc++.h>
#define ll long long
#define mod 998244353
using namespace std;
map<int,int>mp;
bool rec(int i){
    if(mp[i]) {
            mp[i]--;
            if(mp[i]==0)  mp.erase(i);
            return 1;
    }
    if((mp.begin()->first)<i) return 0;
    return rec((i+1)/2)&rec(i/2);

}
int main()
{
  //freopen("input.txt","r",stdin);
  //freopen("out.txt","w",stdout);
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr), cout.tie(nullptr);
   ll t,n,m; cin>>t;
   string s;
   while(t--){
     cin>>n>>m;
     ll sum1=0,sum2=0;
     vector<ll>a(n),b(m);
     for(auto&it:a) cin>>it,mp[it]++,sum1+=it;
     for(auto&it:b) cin>>it,sum2+=it;
     bool f=1;
     if(sum1!=sum2){
        cout<<"NO\n";
        continue;
     }
     sort(b.begin(),b.end());
     for(int i=0;i<m;i++){
        f&=rec(b[i]);
        if(!f) break;
     }
     if(f) cout<<"YES\n"; else cout<<"NO\n";
     mp.clear();
   }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define mod 998244353
using namespace std;
map<int,int>mp;
ll ct,n,m;
bool rec(int i){
    if(mp.begin()->first>i)
        return 0;
    if(mp[i]) {
        mp[i]--;
        if(mp[i]==0)
            mp.erase(i);
        return 1;
    }
    ct++;
    if(ct>n-m) return 0;
    return rec((i+1)/2)&rec(i/2);

}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    ll t; cin>>t;
    string s;
    while(t--){
        ct=0;
        cin>>n>>m;
        ll sum1=0,sum2=0;
        vector<int>a(n),b(m);
        for(auto&it:a) cin>>it,mp[it]++,sum1+=it;
        for(auto&it:b) cin>>it,sum2+=it;
        bool f=1;
        if(sum1!=sum2){
            mp.clear();
            cout<<"NO\n";
            continue;
        }
        sort(b.begin(),b.end());
        for(int i=0;i<m;i++){
            f&=rec(b[i]);
            if(ct>n-m) f=0;
            if(!f) break;
        }
        if(f) cout<<"YES\n"; else cout<<"NO\n";
        mp.clear();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define pb push_back
#define MOD 1000000007
#define vll vector<ll>
#define pll pair<ll, ll>
#define endl "\n"
#define all(v) v.begin(), v.end()
#define mem(a,b) memset(a, b, sizeof(a))
#define co(n) cout << n << endl
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define fr(x, n) for (int i = x; i < n; ++i)
#define fraction(x) cout << fixed << setprecision(x)
#define Baba_Yaga ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);

const double eps = 1e-9;
const int N = 2e5+123;

ll gcd(ll a,ll b) { return __gcd(a,b); }
ll lcm(ll a,ll b) { return (a*b)/__gcd(a,b); }

int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};

void solve()
{
    ll n, m; cin >> n >> m;
    map<ll, ll> m1, m2;
    set<ll> s1, s2;
    vll v1(n); fr(0, n) cin >> v1[i], m1[v1[i]]++, s1.insert(v1[i]);
    vll v2(m); fr(0, m) cin >> v2[i], m2[v2[i]]++, s2.insert(v2[i]);

    while(1)
    {
        if(s1.empty() && s2.empty())
        {
            yes; return;
        }
        if( (s1.empty() && s2.size() >= 1) || (s2.empty() && s1.size() >= 1))
        {
            no; return;
        }

        ll mxs1 = *(--s1.end());
        ll mxs2 = *(--s2.end());

        if(mxs1 > mxs2)
        {
            no; return;
        }

        if(m1[mxs2] >= 1)
        {
            ll mini = min(m1[mxs1], m2[mxs1]);
            m1[mxs1] -= mini;
            m2[mxs1] -= mini;
            if(m1[mxs1] == 0)
            {
                s1.erase(mxs1);
            }
            if(m2[mxs1] == 0)
            {
                s2.erase(mxs1);
            }
        }
        else
        {
            ll val_1 = mxs2/2;
            ll val_2 = (mxs2 + 1)/2;

            m2[val_1] += m2[mxs2];
            m2[val_2] += m2[mxs2];
            m2[mxs2] = 0;

            s2.insert(val_1);
            s2.insert(val_2);

            if(m2[mxs2] == 0)
            {
                s2.erase(mxs2);
            }

        }
    }
}

// --- Think the problem backwards ---

int main()
{
    Baba_Yaga;
    ll tc = 1; cin >> tc;
    while(tc--) solve();
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define pb push_back
#define MOD 1000000007
#define vll vector<ll>
#define pll pair<ll, ll>
#define endl "\n"
#define all(v) v.begin(), v.end()
#define mem(a,b) memset(a, b, sizeof(a))
#define co(n) cout << n << endl
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define fr(x, n) for (int i = x; i < n; ++i)
#define fraction(x) cout << fixed << setprecision(x)
#define Baba_Yaga ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);

const double eps = 1e-9;
const int N = 2e5+123;

ll gcd(ll a,ll b) { return __gcd(a,b); }
ll lcm(ll a,ll b) { return (a*b)/__gcd(a,b); }

int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};

void solve()
{
    ll n, m; cin >> n >> m;
    map<ll, ll> m1, m2;
    set<ll> s1, s2;
    vll v1(n); fr(0, n) cin >> v1[i], m1[v1[i]]++, s1.insert(v1[i]);
    vll v2(m); fr(0, m) cin >> v2[i], m2[v2[i]]++, s2.insert(v2[i]);

    while(1)
    {
        if(s1.empty() && s2.empty())
        {
            yes; return;
        }
        if( (s1.empty() && s2.size() >= 1) || (s2.empty() && s1.size() >= 1))
        {
            no; return;
        }

        ll mxs1 = *(--s1.end());
        ll mxs2 = *(--s2.end());

        if(mxs1 > mxs2)
        {
            no; return;
        }

        if(mxs1 == mxs2)
        {
            ll mini = min(m1[mxs1], m2[mxs1]);
            m1[mxs1] -= mini;
            m2[mxs1] -= mini;
            if(m1[mxs1] == 0)
            {
                s1.erase(mxs1);
            }
            if(m2[mxs1] == 0)
            {
                s2.erase(mxs1);
            }
        }
        else if(mxs1 < mxs2)
        {
            ll val_1 = mxs2/2;
            ll val_2 = (mxs2 + 1)/2;

            ll koto_bar = m2[mxs2];

            if( (m1[val_1] >= koto_bar) && (m1[val_2] >= koto_bar) )
            {
                m1[val_1] -= koto_bar;
                m1[val_2] -= koto_bar;

                if(m1[val_1] == 0)
                {
                    s1.erase(val_1);
                }
                if(m1[val_2] == 0)
                {
                    s1.erase(val_2);
                }

                s2.erase(val_1 + val_2);
            }
            else
            {
                s2.insert(val_1);
                s2.insert(val_2);
                m2[val_1]++;
                m2[val_2]++;
                m2[mxs2]--;
                s2.erase(mxs2);

            }
        }
    }
}

// --- Think the problem backwards ---

int main()
{
    Baba_Yaga;
    ll tc = 1; cin >> tc;
    while(tc--) solve();
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        multiset<ll> a, b;
        for (int i = 0; i < n; i++) {
            ll x;
            cin >> x;
            a.insert(x);
        }
        for (int i = 0; i < m; i++) {
            ll x;
            cin >> x;
            b.insert(x);
        }
        while (a.size() && b.size()) {
            if (*b.begin() == *a.begin()) {
                a.erase(a.begin());
                b.erase(b.begin());
                continue;
            }
            if (*b.begin() < *a.begin() || a.size() == 1) {
                break;
            }
            if (*next(a.begin()) - 1 > *a.begin()) {
                break;
            }
            a.insert(*a.begin() + *next(a.begin()));
            a.erase(next(a.begin()));
            a.erase(a.begin());
        }
        cout << (a.size() || b.size() ? "NO" : "YES") << "\n";
    }

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        multiset<ll> a, b;
        for (int i = 0; i < n; i++) {
            ll x;
            cin >> x;
            a.insert(x);
        }
        for (int i = 0; i < m; i++) {
            ll x;
            cin >> x;
            b.insert(x);
        }
        while (a.size() && b.size()) {
            if (a.lower_bound(*b.begin()) != a.end() && *a.lower_bound(*b.begin()) == *b.begin()) {
                a.erase(a.lower_bound(*b.begin()));
                b.erase(b.begin());
                continue;
            }
            if (*b.begin() < *a.begin() || a.size() == 1) {
                break;
            }
            ll x = *b.begin();
            b.erase(b.begin());
            b.insert(x / 2);
            b.insert(x / 2 + (x % 2));
        }
        cout << (a.size() || b.size() ? "NO" : "YES") << "\n";
    }

	return 0;
}

Answer: 2


#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)

#define debug0(x) std::cout << #x << " = " << x << '\n'
#define debug1(f, a, b) std::cout << #f << ": "; for (int i = (a); i <= (b); i++) std::cout << (f)[i] << " \n"[i == (b)]
#define debug2(f, a, b, c, d) std::cout << #f << ":\n"; for (int i = (a); i <= (b); i++) for (int j = (c); j <= (d); j++) std::cout << (f)[i][j] << " \n"[j == (d)]
#define debug3(q) std::cout << #q << ": "; for (auto it : (q)) std::cout << it << ' '; std::cout << '\n'

#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
const int N = 1e5 + 10;

typedef long long ll;

void solve () {
    ll n, m;
    std::cin >> n >> m;

    std::priority_queue<ll> a, b;
    for (int i = 1; i <= n; i++) {
        ll x;
        std::cin >> x;
        a.push(x);
    }
    for (int i = 1; i <= m; i++) {
        ll x;
        std::cin >> x;
        b.push(x);
    }

    while (!a.empty()) {
        if (b.empty() || b.size() > a.size()) {
            std::cout << "No\n";
            return;
        }

        ll x = b.top();
        b.pop();

        if (x < a.top()) {
            std::cout << "No\n";
            return;
        }
        else if (x == a.top()) {
            a.pop();
        }
        else {
            b.push(x >> 1);
            b.push(x + 1 >> 1);
        }
    }

    if (!b.empty()) {
        std::cout << "No\n";
        return;
    }
    std::cout << "Yes\n";
}

int main () {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0), std::cout.tie(0);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)

#define debug0(x) std::cout << #x << " = " << x << '\n'
#define debug1(f, a, b) std::cout << #f << ": "; for (int i = (a); i <= (b); i++) std::cout << (f)[i] << " \n"[i == (b)]
#define debug2(f, a, b, c, d) std::cout << #f << ":\n"; for (int i = (a); i <= (b); i++) for (int j = (c); j <= (d); j++) std::cout << (f)[i][j] << " \n"[j == (d)]
#define debug3(q) std::cout << #q << ": "; for (auto it : (q)) std::cout << it << ' '; std::cout << '\n'

#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
const int N = 1e5 + 10;

typedef long long ll;

void solve () {
    ll n, m;
    std::cin >> n >> m;

    std::deque<ll> a;
    std::deque<ll> b;

    // std::unordered_map<int, int> count_of_a;
    // std::unordered_map<int, int> count_of_b;

    for (int i = 1; i <= n; i++) {
        int x;
        std::cin >> x;

        a.push_back(x);
        // count_of_a[x]++;
    }
    for (int i = 1; i <= m; i++) {
        int x;
        std::cin >> x;

        b.push_back(x);
        // count_of_b[x]++;
    }
    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());

    int cnt = 0;
    while (a.size() > b.size()) {
        if (cnt >= 10000) break;

        if (a.front() == b.front()) {
            a.push_back(a.front());
            b.push_back(b.front());

            a.pop_front();
            b.pop_front();
        }
        else {
            if (std::abs(a[0] - a[1]) <= 1) {
                a.push_back(a[0] + a[1]);
                a.pop_front(); a.pop_front();
            }
        }

        cnt++;
    }
    // debug3(a), debug3(b);

    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());

    if (a == b) std::cout << "Yes\n";
    else std::cout << "No\n";
}

int main () {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0), std::cout.tie(0);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Answer: 1


#include <bits/stdc++.h>
#define endl "\n"
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

void solve() {
    int n, m; cin >> n >> m;
    vll a(n), b(m);
    for (auto& i: a) cin >> i;
    for (auto& i: b) cin >> i;

    sort(all(a));
    sort(all(b));

    priority_queue<ll, vll> pq(all(b));

    while (pq.size() != a.size()) {
        if (pq.top() == a.back()) {
            pq.pop();
            a.pop_back();
        } else if (pq.top() > a.back()) {
            ll temp = pq.top();
            pq.pop();
            pq.push(temp / 2);
            pq.push((temp / 2) + (temp % 2));
        } else if (pq.top() < a.back()) {
            cout << "No" << endl;
            return;
        }
    }

    while (pq.top() == a.back()) {
        a.pop_back();
        pq.pop();
    }

    if (pq.size() || a.size()) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
}

int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   #ifndef ONLINE_JUDGE
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
   #endif
   ll t;
   cin >> t;
   while(t--) {
      solve();
   }
   return 0;
}

#include <bits/stdc++.h>
#define endl "\n"
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

void solve() {
    int n, m; cin >> n >> m;
    vll a(n), b(m);
    for (auto& i: a) cin >> i;
    for (auto& i: b) cin >> i;

    sort(all(a));
    sort(all(b));

    priority_queue<ll, vll> pq(all(b));

    while (pq.size() < a.size() && pq.size()) {
        if (pq.top() == a.back()) {
            pq.pop();
            a.pop_back();
        } else if (pq.top() > a.back()) {
            ll temp = pq.top();
            pq.pop();
            pq.push(temp / 2);
            pq.push((temp / 2) + (temp % 2));
        } else if (pq.top() < a.back()) {
            cout << "No" << endl;
            return;
        }
    }

    if (!pq.size()) {
        cout << "No" << endl;
        return;
    }

    while (pq.top() == a.back()) {
        a.pop_back();
        pq.pop();
    }

    if (pq.size() || a.size()) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
}

int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   #ifndef ONLINE_JUDGE
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
   #endif
   ll t;
   cin >> t;
   while(t--) {
      solve();
   }
   return 0;
}

Answer: 2


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <cstdint>
#include <set>
#include <algorithm>
#include <random>
#include <iomanip>
#include <cassert>
#include <string>
#include <chrono>
#include <ctime>
#include <complex>
#include <unordered_set>

#define ll unsigned long long
#define ld long double
#define nl "\n"
#define pb push_back
#define ff first
#define ss second
#define cinn(a) for (int i = 0; i < (int) a.size(); i++) cin >> a[i];
#define coutt(a) for (int i = 0; i < (int) a.size(); i++) cout << a[i] << (i == a.size() - 1 ? nl : " ");
#define sortt(a) sort(a.begin(), a.end());
#define rev(a) reverse(a.begin(), a.end());
#define int long long

using namespace std;

void solve() {
    int n,m;

    cin >> n >> m;

    priority_queue<int> q1;
    priority_queue<int> q2;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        q1.push(x);
    }

    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        q2.push(x);
    }

    for (int i = 0; i < n-m; i++) {
        if (q1.empty() || q2.empty()) break;
        int a = q1.top();
        int b = q2.top();
        if (a < b) {
            q2.pop();
            q2.push(b/2);
            q2.push(b % 2 == 1 ? b / 2 + 1 : b / 2);
        } else if (a == b) {
            q1.pop();
            q2.pop();
            i--;
        } else {
            break;
        }
    }

    if (q1.size() != q2.size()) {
        cout << "No\n";
    } else {
        while (!q1.empty()) {
            if (q1.top() != q2.top()) {
                cout << "No\n";
                return;
            }
            q1.pop();
            q2.pop();
        }
        cout << "Yes\n";
    }

}

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ll start = chrono::high_resolution_clock::now().time_since_epoch().count();
#endif
    //freopen("segmentupdate.in", "r", stdin);
    //freopen("segmentupdate.out", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout << fixed;
    //cout.precision(10);

    int tests = 1;
    cin >> tests;
    while (tests--) solve();

#ifdef _DEBUG
    cout << nl << "TIME ms: ";
    cout << (chrono::high_resolution_clock::now().time_since_epoch().count() - start) / 1e6 << nl;
#endif
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <cstdint>
#include <set>
#include <algorithm>
#include <random>
#include <iomanip>
#include <cassert>
#include <string>
#include <chrono>
#include <ctime>
#include <complex>
#include <unordered_set>

#define ll unsigned long long
#define ld long double
#define nl "\n"
#define pb push_back
#define ff first
#define ss second
#define cinn(a) for (int i = 0; i < (int) a.size(); i++) cin >> a[i];
#define coutt(a) for (int i = 0; i < (int) a.size(); i++) cout << a[i] << (i == a.size() - 1 ? nl : " ");
#define sortt(a) sort(a.begin(), a.end());
#define rev(a) reverse(a.begin(), a.end());
#define int long long

using namespace std;

void solve() {
    int n,m;

    cin >> n >> m;

    priority_queue<int> q1;
    priority_queue<int> q2;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        q1.push(x);
    }

    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        q2.push(x);
    }

    for (int i = 0; i < n; i++) {
        int a = q1.top();
        int b = q2.top();
        if (a < b) {
            q2.pop();
            q2.push(b/2);
            q2.push(b % 2 == 1 ? b / 2 + 1 : b / 2);
        } else if (a == b) {
            q1.pop();
            q2.pop();
        } else {
            break;
        }
    }

    if (q1.size() != q2.size()) {
        cout << "No\n";
    } else {
        while (!q1.empty()) {
            if (q1.top() != q2.top()) {
                cout << "No\n";
                return;
            }
            q1.pop();
            q2.pop();
        }
        cout << "Yes\n";
    }

}

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ll start = chrono::high_resolution_clock::now().time_since_epoch().count();
#endif
    //freopen("segmentupdate.in", "r", stdin);
    //freopen("segmentupdate.out", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout << fixed;
    //cout.precision(10);

    int tests = 1;
    cin >> tests;
    while (tests--) solve();

#ifdef _DEBUG
    cout << nl << "TIME ms: ";
    cout << (chrono::high_resolution_clock::now().time_since_epoch().count() - start) / 1e6 << nl;
#endif
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("inp.in", "r", stdin), freopen("oup.out", "w", stdout);
    #endif
    int t; cin >> t;
    while (t--) {
        int n, m; cin >> n >> m;
        map<int, int> a, b;
        int atot = 0, btot = 0;
        for (int i = 0, j; i < n; ++i) cin >> j, a[j]++, atot += j;
        for (int i = 0, j; i < m; ++i) cin >> j, b[j]++, btot += j;
        if (atot != btot) goto fail;
        atot = n, btot = m;
        while (b.size()) {
            auto [key, val] = *b.begin();
            btot -= val;
            if (a.count(key)) {
                if (a[key] >= val) a[key] -= val, atot -= val, val = 0;
                else btot -= a[key], val -= a[key], a.erase(key);
                if (a[key] == 0) a.erase(key);
            }
            if (val != 0) b[key / 2] += val, b[key - key / 2] += val, btot += 2 * val;
            b.erase(key);
            if (btot > atot) goto fail;
        }
        if (a.size() || b.size()) goto fail;
        cout << "YES" << endl; continue;
        fail: cout << "NO" << endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("inp.in", "r", stdin), freopen("oup.out", "w", stdout);
    #endif
    #define int long long
    int t; cin >> t;
    while (t--) {
        int n, m; cin >> n >> m;
        string ans = "YES";
        map<int, int> a, b;
        int atot = 0, btot = 0;
        for (int i = 0, j; i < n; ++i) cin >> j, a[j]++, atot += j;
        for (int i = 0, j; i < m; ++i) cin >> j, b[j]++, btot += j;
        if (atot != btot) goto fail;
        atot = n, btot = m;
        while (b.size()) {
            auto [key, val] = *b.begin();
            btot -= val;
            if (a.count(key)) {
                if (a[key] >= val) a[key] -= val, atot -= val, val = 0;
                else btot -= a[key], val -= a[key];
                if (a[key] == 0) a.erase(key);
            }
            if (val != 0) b[key / 2] += val, b[key - key / 2] += val, btot += 2 * val;
            b.erase(key);
            if (btot > atot) goto fail;
        }
        cout << "YES" << endl; continue;
        fail: cout << "NO" << endl;
    }
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define lowbit(x) x & (-x)
#define fix(x) fixed << setprecision(x)
ll n, m;
map<ll, ll> ma;
bool dfs(ll x) {
    if (x == 0)
        return 0;
    if (ma[x] > 0) {
        ma[x]--;
        return 1;
    }
    return dfs(x / 2) && dfs(x - x / 2);
}
void sp() {
    cin >> n >> m;
    vector<ll> a(n);
    ma.clear();
    ll f=0;
    for (ll i = 0; i < n; i++)
        cin >> a[i], ma[a[i]]++;
    for (ll i = 0; i < m; i++) {
        ll x;
        cin >> x;
        if (!dfs(x)) {
            f=1;
        }
    }
    for (auto i : ma) {
        if (i.second > 0) {
            f=1;
            break;
        }
    }
    if(f){
        cout<<"No"<<endl;
        return ;
    }
    cout << "Yes" << endl;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll __ = 1;
    cin >> __;
    while (__--)
        sp();
}

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define lowbit(x) x & (-x)
#define fix(x) fixed << setprecision(x)
ll n, m;
map<ll, ll> ma;
bool dfs(ll x){
    if(x==0) return 0;
        if(ma[x]>0){
            ma[x]--;
            return 1;
        }
    return dfs(x/2)&&dfs(x-x/2);
}
void sp() {
    cin >> n >> m;
    vector<ll> a(n);
    ma.clear();
    for (ll i = 0; i < n; i++)
        cin >> a[i], ma[a[i]]++;
    for (ll i = 0; i < m; i++) {
        ll x;
        cin >> x;
        if(!dfs(x)){
            cout << "No" << endl;
            return;
        }
    }
    for(auto i:ma){
        if(i.second>0){
            cout<<"No"<<endl;
            return ;
        }
    }
    cout << "Yes" << endl;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll __ = 1;
    cin >> __;
    while (__--)
        sp();
}

Answer: 1


#include<bits/stdc++.h>

#define int long long

#define pp pair<int,int>

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

#define rall(a) a.rbegin(),a.rend()

using namespace std;

void remove(int val,map <int,int> &mp) {

    mp[val]--;

    if(mp[val]==0)

        mp.erase(val);

}

void form(int val,map <int,int> &mp) {

    if(mp.size()==0)return;

    if(val<(mp.begin()->first))return;

    if(mp.find(val)!=mp.end())return;

    if(val&1) {

        int x=(val-1)/2,y=x+1;

        form(x,mp);

        if(mp.find(x)!=mp.end()) {

            remove(x,mp);

            form(y,mp);

            if(mp.find(y)!=mp.end()) {

                remove(y,mp);

                mp[val]++;

            }

            else

                mp[x]++;

        }

    }

    else {

        int x=val/2;

        form(x,mp);

        if(mp.find(x)!=mp.end()) {

            remove(x,mp);

            form(x,mp);

            if(mp.find(x)!=mp.end()) {

                remove(x,mp);

                mp[val]++;

            }

            else

                mp[x]++;

        }

    }

}

void solve()

{

    int n,m;cin>>n>>m;

    vector <int> a(n),b(m);

    for(int i=0;i<n;i++)cin>>a[i];

    for(int i=0;i<m;i++)cin>>b[i];

    map <int,int> mp;

    for(int i:a)

        mp[i]++;

    for(int i=0;i<m;i++) {

        form(b[i],mp);

        if(mp.find(b[i])!=mp.end()) {

            remove(b[i],mp);

        }

        else {

            cout<<"No"<<endl;

            return;

        }

    }

    if(mp.size()==0)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

}

signed main()

{

    cin.tie()->sync_with_stdio(false);

    int t;cin>>t;

    while(t--)solve();

}

#include<bits/stdc++.h>

#define int long long

#define pp pair<int,int>

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

#define rall(a) a.rbegin(),a.rend()

using namespace std;

void remove(int val,map <int,int> &mp) {

    mp[val]--;

    if(mp[val]==0)

        mp.erase(val);

}

void form(int val,map <int,int> &mp) {

    if(mp.size()==0)return;

    if(val<mp.begin()->first)return;

    if(val&1) {

        int x=(val-1)/2,y=x+1;

        if(mp.find(x)!=mp.end() && mp.find(y)!=mp.end()) {

            remove(x,mp);

            remove(y,mp);

            mp[val]++;

        }

        else if(mp.find(x)!=mp.end()) {

            form(y,mp);

            if(mp.find(y)!=mp.end()) {

                remove(y,mp);

                remove(x,mp);

                mp[val]++;

            }

        }

        else if(mp.find(y)!=mp.end()) {

            form(x,mp);

            if(mp.find(x)!=mp.end()) {

                remove(x,mp);

                remove(y,mp);

                mp[val]++;

            }

        }

        else {

            form(x,mp);

            form(y,mp);

            if(mp.find(x)!=mp.end() && mp.find(y)!=mp.end()) {

                remove(x,mp);

                remove(y,mp);

                mp[val]++;

            }

        }

    }

    else {

        int x=val/2;

        if(mp[x]>=2) {

            remove(x,mp);

            remove(x,mp);

            mp[val]++;

        }

        else if(mp.find(x)!=mp.end()) {

            form(x,mp);

            if(mp[x]>=2) {

                remove(x,mp);

                remove(x,mp);

                mp[val]++;

            }

        }

        else {

            form(x,mp);

            form(x,mp);

            if(mp[x]>=2) {

                remove(x,mp);

                remove(x,mp);

                mp[val]++;

            }

        }

    }

}

void solve()

{

    int n,m;cin>>n>>m;

    vector <int> a(n),b(m);

    for(int i=0;i<n;i++)cin>>a[i];

    for(int i=0;i<m;i++)cin>>b[i];

    map <int,int> mp;

    for(int i:a)

        mp[i]++;

    for(int i=0;i<m;i++) {

        if(mp.find(b[i])!=mp.end()) {

            remove(b[i],mp);

        }

        else {

            form(b[i],mp);

            if(mp.find(b[i])!=mp.end()) {

                remove(b[i],mp);

            }

            else {

                cout<<"No"<<endl;

                return;

            }

        }

    }

    if(mp.size()==0)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

}

signed main()

{

    cin.tie()->sync_with_stdio(false);

    int t;cin>>t;

    while(t--)solve();

}

Answer: 1