import java.io.*;
import java.security.KeyStore.Entry;
import java.util.*;

public class Forest {
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null)
                return;
            din.close();
        }
    }

    static Reader sc = new Reader();
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String args[]) throws IOException {
        @SuppressWarnings("unused")
        Reader sc = new Reader();
        long t = inputLong();
        while (t-- > 0) {
            long n = inputLong();
            long m = inputLong();
            long a[] = new long[(int) n];
            HashMap<Long,Long>map=new HashMap<>();
            long sum=0;
            for (int i = 0; i < n; i++) {
                a[i] = inputLong();
                sum+=a[i];
                map.put(a[i],map.getOrDefault(a[i], (long) 0)+1);
            }
            long sum1=0;
            // long b[] = new long[(int) n];
            ArrayList<Long>list=new ArrayList<>();
            for (int i = 0; i < m; i++) {
                list.add(inputLong());
                sum1+=list.get(i);
            }
            if(sum!=sum1){
                System.out.println("NO");continue;
            }
            boolean bool=true;
            while(!list.isEmpty()){
                long num=list.get(list.size()-1);
                if(map.containsKey(num)){
                    map.put(num,map.getOrDefault(num, (long) 0)-1);
                    list.remove(list.size()-1);
                    if(map.get(num)<=0){
                        map.remove(num);
                    }
                }else{
                    list.remove(num);
                    if(num/2 ==0){bool=false;break;}
                    else if(ceil(num,2)==0){bool=false;break;}
                    list.add(num/2);
                    list.add(ceil1(num,2));
                }
                if(!bool)break;
            }
            if(!bool)System.out.println("NO");
            else System.out.println("YES");

        }
        bw.flush();
        bw.close();
    }

    public static long ceil1(long a, long b) {
        return (a + b - 1) / b;
    }

    public static boolean checker(long a[]) {
        for (int i = 0; i < a.length-1; i++) {
            if (a[i] > a[i+1])
                return false;
        }
        return true;
    }

    public static long divisor(long n) {
        long cnt = 0;
        for (long i = 1; i * i <= n; i++) {
            if (n % i == 0)
                cnt++;
            if ((n / i) % i == 0)
                cnt++;
        }
        return cnt;
    }

    public static long powerMod(long base, long exponent, long mod) {
        long result = 1;
        base = base % mod; // Ensure base is within the mod range

        while (exponent > 0) {
            // If exponent is odd, multiply base with result
            if ((exponent & 1) == 1) { // Same as (exponent % 2 != 0)
                result = (result * base) % mod;
            }
            // Now exponent must be even
            exponent = exponent >> 1; // Equivalent to exponent = exponent / 2
            base = (base * base) % mod; // Square the base
        }
        return result;
    }

    public static long sequence(long n) {
        if (n == 1)
            return 1;
        if (n % 2 == 0)
            return 1 + sequence(n / 2);
        else
            return 1 + sequence(3 * (n) + 1);
    }

    public static void matrix(int a[][], int b[][], int i, int j) {
        if (i >= a.length)
            return;
        if (j == a[0].length - 1) {
            System.out.println(a[i][j] + b[i][j]);
            matrix(a, b, i + 1, 0);
            return;
        }
        System.out.print(a[i][j] + b[i][j] + " ");
        matrix(a, b, i, j + 1);
    }

    public static boolean checker(String s) {
        String a = "1100";
        for (int i = 0; i < s.length() - 3; i++) {
            if (a.equals(s.substring(i, i + 4)))
                return true;
        }
        return false;
    }

    public static long ceil(long a, long b) {
        return (a + b - 1) / b;
    }

    public static int inputInt() throws IOException {
        return sc.nextInt();
    }

    public static long inputLong() throws IOException {
        return sc.nextLong();
    }

    public static double inputDouble() throws IOException {
        return sc.nextDouble();
    }

    public static String inputString() throws IOException {
        return sc.readLine();
    }

    public static void print(String a) throws IOException {
        bw.write(a);
    }

    public static void printSp(String a) throws IOException {
        bw.write(a + " ");
    }

    public static void println(String a) throws IOException {
        bw.write(a + "\n");
    }
}

import java.io.*;
import java.security.KeyStore.Entry;
import java.util.*;

public class Forest {
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null)
                return;
            din.close();
        }
    }

    static Reader sc = new Reader();
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String args[]) throws IOException {
        @SuppressWarnings("unused")
        Reader sc = new Reader();
        long t = inputLong();
        while (t-- > 0) {
            long n = inputLong();
            long m = inputLong();
            long a[] = new long[(int) n];
            TreeMap<Long,Long>map=new TreeMap<>();
            long sum=0;
            for (int i = 0; i < n; i++) {
                a[i] = inputLong();
                sum+=a[i];
                map.put(a[i],map.getOrDefault(a[i], (long) 0)+1);
            }
            long sum1=0;
            // long b[] = new long[(int) n];
            ArrayList<Long>list=new ArrayList<>();
            for (int i = 0; i < m; i++) {
                list.add(inputLong());
                sum1+=list.get(i);
            }
            if(sum!=sum1){
                System.out.println("NO");continue;
            }
            boolean bool=true;
            while(!list.isEmpty()){
                long num=list.get(list.size()-1);
                if(map.containsKey(num)){
                    map.put(num,map.getOrDefault(num, (long) 0)-1);
                    list.remove(list.size()-1);
                    if(map.get(num)<=0){
                        map.remove(num);
                    }
                }else{
                    list.remove(list.size()-1);
                    if(num/2 ==0){bool=false;break;}
                    else if(ceil(num,2)==0){bool=false;break;}
                    list.add(num/2);
                    list.add(ceil1(num,2));
                }
                if(!bool)break;
            }
            if(!bool)System.out.println("NO");
            else System.out.println("YES");

        }
        bw.flush();
        bw.close();
    }

    public static long ceil1(long a, long b) {
        return (a + b - 1) / b;
    }

    public static boolean checker(long a[]) {
        for (int i = 0; i < a.length-1; i++) {
            if (a[i] > a[i+1])
                return false;
        }
        return true;
    }

    public static long divisor(long n) {
        long cnt = 0;
        for (long i = 1; i * i <= n; i++) {
            if (n % i == 0)
                cnt++;
            if ((n / i) % i == 0)
                cnt++;
        }
        return cnt;
    }

    public static long powerMod(long base, long exponent, long mod) {
        long result = 1;
        base = base % mod; // Ensure base is within the mod range

        while (exponent > 0) {
            // If exponent is odd, multiply base with result
            if ((exponent & 1) == 1) { // Same as (exponent % 2 != 0)
                result = (result * base) % mod;
            }
            // Now exponent must be even
            exponent = exponent >> 1; // Equivalent to exponent = exponent / 2
            base = (base * base) % mod; // Square the base
        }
        return result;
    }

    public static long sequence(long n) {
        if (n == 1)
            return 1;
        if (n % 2 == 0)
            return 1 + sequence(n / 2);
        else
            return 1 + sequence(3 * (n) + 1);
    }

    public static void matrix(int a[][], int b[][], int i, int j) {
        if (i >= a.length)
            return;
        if (j == a[0].length - 1) {
            System.out.println(a[i][j] + b[i][j]);
            matrix(a, b, i + 1, 0);
            return;
        }
        System.out.print(a[i][j] + b[i][j] + " ");
        matrix(a, b, i, j + 1);
    }

    public static boolean checker(String s) {
        String a = "1100";
        for (int i = 0; i < s.length() - 3; i++) {
            if (a.equals(s.substring(i, i + 4)))
                return true;
        }
        return false;
    }

    public static long ceil(long a, long b) {
        return (a + b - 1) / b;
    }

    public static int inputInt() throws IOException {
        return sc.nextInt();
    }

    public static long inputLong() throws IOException {
        return sc.nextLong();
    }

    public static double inputDouble() throws IOException {
        return sc.nextDouble();
    }

    public static String inputString() throws IOException {
        return sc.readLine();
    }

    public static void print(String a) throws IOException {
        bw.write(a);
    }

    public static void printSp(String a) throws IOException {
        bw.write(a + " ");
    }

    public static void println(String a) throws IOException {
        bw.write(a + "\n");
    }
}

Answer: 2


/*
CF2061D
30.1.25.
*/

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

#define _line << "\n"
#define _space << " " <<
#define ll long long

int main(){

    /*ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);*/

    int t; cin >> t;

    while (t--){
        priority_queue<int> a,b;
        int n,m; cin >> n >> m;
        for (int i=0; i<n; i++){
            int x; cin >> x;
            a.push(x);
        }
        for (int j=0; j<m; j++){
            int x; cin >> x;
            b.push(x);
        }

        if (m>n){
            cout << "no" _line; continue;
        }

        int z=1;
        while (!a.empty() || !b.empty()){
            if (a.empty() || b.empty() || b.size()>a.size()) {
                z=0;
                cout << "no" _line; break;
            }
            if (a.top()==b.top()){
                a.pop(); b.pop();
            }
            else {
                int x=b.top(); b.pop();
                //cout << x _space b.size() _line;
                if (x==1){
                    z=0;
                    cout << "no" _line; break;
                }
                if (x%2){
                    b.push(x/2);
                    b.push(x/2+1);
                }
                else {
                    b.push(x/2);
                    b.push(x/2);
                }
            }
        }

        if (z) cout << "yes" _line;

    }

}

/*
CF2061D
30.1.25.
*/

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

#define _line << "\n"
#define _space << " " <<
#define ll long long

int main(){

    /*ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);*/

    int t; cin >> t;

    while (t--){
        priority_queue<int> a,b;
        int n,m; cin >> n >> m;
        for (int i=0; i<n; i++){
            int x; cin >> x;
            a.push(x);
        }
        for (int j=0; j<m; j++){
            int x; cin >> x;
            b.push(x);
        }

        if (m>n){
            cout << "no" _line; continue;
        }

        int z=1;
        while (!a.empty() || !b.empty()){
            if (a.empty() || b.empty()) {
                z=0;
                cout << "no" _line; break;
            }
            if (a.top()==b.top()){
                a.pop(); b.pop();
            }
            else {
                int x=b.top(); b.pop();
                if (x==1){
                    z=0;
                    cout << "no" _line; break;
                }
                if (x%2){
                    b.push(x/2);
                    b.push(x/2+1);
                }
                else {
                    b.push(x/2);
                    b.push(x/2);
                }
            }
        }

        if (z) cout << "yes" _line;

    }

}

Answer: 1


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

#ifdef LOCAL
#include  "../debug.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define pb push_back
#define BIG 998244353
#define MOD 1000000007
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;

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

    vi a(n), b(m);
    map<ll,ll> freq;
    ll cnt = n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        freq[a[i]]++;
    }

    priority_queue<ll, vi, greater<ll>> pq;
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        pq.push(b[i]);
    }

    while (!pq.empty()) {
        ll x = pq.top();
        pq.pop();

        if (x==0) {
            cout << "No\n";
            return;
        }

        if (freq.find(x)!=freq.end() && freq[x]) {
            freq[x]--;
            cnt--;
            continue;
        }
        else {
            ll a = x/2;
            ll b = (x+1)/2;
            pq.push(a);
            pq.push(b);
        }
    }

    if (cnt==0) cout << "Yes\n";
    else cout << "No\n";
}

int main() {
    auto start = chrono::high_resolution_clock::now();
    fast_io;
    int t;
    cin >> t;

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

    #ifdef LOCAL
        auto end = chrono::high_resolution_clock::now();
        chrono::duration<double> duration = end - start;
        cerr << "Execution time: " << duration.count() << " seconds" << endl;
    #endif
}

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

#ifdef LOCAL
#include  "../debug.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define pb push_back
#define BIG 998244353
#define MOD 1000000007
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;

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

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

    map<ll,ll> freq;
    ll mx = 0;
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        mx = max(mx,b[i]);
        freq[b[i]]++;
    }

    if (n<m) {
        cout << "No\n";
        return;
    }

    ll cnt = m;
    priority_queue<ll, vi, greater<ll>> pq;
    for (int i = 0; i < n; i++) {
        if (freq.find(a[i])!=freq.end() && freq[a[i]]) {
            freq[a[i]]--;
            cnt--;
        }
        else pq.push(a[i]);
    }

    while (sz(pq)>1) {
        ll x = pq.top();
        pq.pop();
        ll y = pq.top();
        pq.pop();

        if (y-x>1 || y+x>mx) {
            cout << "No\n";
            return;
        }

        y += x;
        if (freq.find(y)!=freq.end() && freq[y]) {
            freq[y]--;
            cnt--;
        }
        else pq.push(y);
    }

    if (cnt==0 && sz(pq)==0) cout << "Yes\n";
    else cout << "No\n";
}

int main() {
    auto start = chrono::high_resolution_clock::now();
    fast_io;
    int t;
    cin >> t;

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

    #ifdef LOCAL
        auto end = chrono::high_resolution_clock::now();
        chrono::duration<double> duration = end - start;
        cerr << "Execution time: " << duration.count() << " seconds" << endl;
    #endif
}

Answer: 1


#include <bits/stdc++.h>
#define pii pair<int, int>
#define lowbit(x) (x&-x)
#define ll long long
#define ull unsigned long long
#define endl "\n"
const int mod = 998244353;
const int inf = 1e9;
const ll llf = 1e18;
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int i, j;
	int n, m;
	int tt;
	cin >> tt;
	while (tt--)
	{
		int x;
		map<int, int>mymap;
		bool flag = 1;
		cin >> n >> m;
		while (n--)
		{
			cin >> x;
			mymap[x]--;
		}
		while (m--)
		{
			cin >> x;
			mymap[x]++;
		}
		for (auto it =mymap.rbegin();it != mymap.rend();)
		{
			if (it->first== 1)
			{
				if (it->second)
					flag = 0;
			}
			if (it->second < 0)
			{
				flag = 0;
				break;
			}
			mymap[it->first / 2] += it->second;
			mymap[it->first - it->first / 2] += it->second;
			mymap.erase(it->first);
		}
		int flag1 = 1;
		for (auto x : mymap)
		{
			if (x.second)
				flag1 = 0;
		}
		if (flag && flag1)
			cout << "YeS" << endl;
		else
			cout << "nO" << endl;
	}
}

#include <bits/stdc++.h>
#define pii pair<int, int>
#define lowbit(x) (x&-x)
#define ll long long
#define ull unsigned long long
#define endl "\n"
const int mod = 998244353;
const int inf = 1e9;
const ll llf = 1e18;
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int i, j;
	int n, m;
	int tt;
	cin >> tt;
	while (tt--)
	{
		int x;
		map<int, int>mapa;
		map<int, int>mapb;
		bool flag = 1;
		cin >> n >> m;
		while (n--)
		{
			cin >> x;
			mapa[x]++;
		}
		while (m--)
		{
			cin >> x;
			mapb[x]++;
		}
		int cnt = 0;
		for (auto it = mapb.rbegin();it != mapb.rend();it++)
		{
			//if (cnt == 2)
			//	it--;
			if (it->first == 1)
			{
				if (mapa[1] == mapb[1])
				{
					mapa.erase(1);
					mapb.erase(1);
				}
				else
					flag = 0;
				break;
			}
			if (mapa[it->first] > it->second)
			{
				flag = 0;
				break;
			}

			cnt = 0;
			if (mapb.find(it->first / 2) == mapb.end())cnt++;
			if (mapb.find(it->first / 2) == mapb.end())cnt++;

			mapb[it->first/2] += it->second - mapa[it->first];
			mapb[it->first - it->first / 2] += it->second - mapa[it->first];

			mapb[it->first] = mapa[it->first] = 0;
		}
		int flag1 = 1, flag2 = 1;
		for (auto x : mapa)
			if (x.second)
				flag1 = 0;
		for (auto x : mapb)
			if (x.second)
				flag2 = 0;
		if (flag && flag1 && flag2)
			cout << "YeS" << endl;
		else
			cout << "nO" << endl;
	}
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;typedef pair<int,int> pii;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const ll mod = 1e9+7, N = 2e6+7, M = 2e6+7, INF = INT_MAX/10;
ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}

void solve() {
	int n,m;
    cin >> n >> m;
    set<pair<ll,ll>,greater<pair<ll,ll>>> ss;
    map<ll,ll> freqa, freqb;
    for(int i=0,x;i<n;i++)
        cin >> x,freqa[x]++;
    for(int j=0,x;j<m;j++)
        cin >> x,freqb[x]++;
    for(auto &pp:freqb)
        ss.insert({pp.F, pp.S});
    while(!ss.empty() && !freqa.empty() && freqa.rbegin()->first<=ss.begin()->first) {
        auto tt = *ss.begin();
        ss.erase(ss.begin());
        if (freqa.find(tt.F) != freqa.end()) {
            int mm = min(freqa[tt.F], tt.S);
            freqa[tt.F]-=mm;
            freqb[tt.F]-=mm;
            tt.S-=mm;
            if (freqa[tt.F]>0) {
                cout << "NO\n";
                return;
            }
            freqa.erase(tt.F);
        }
        freqb.erase(tt.F);
        if (tt.S==0)
            continue;
        if (tt.F%2==0) {
            int num = tt.F/2;
            if (freqb.find(num) != freqb.end()) {
                ss.erase({num, freqb[num]});
            }
            freqb[num]+=2*tt.S;
            ss.insert({num, freqb[num]});
        } else {
            int t1 = (tt.F-1)/2;
            int t2 = t1+1;
            if (freqb.find(t1) != freqb.end()) {
                ss.erase({t1, freqb[t1]});
            }
            if (freqb.find(t2) != freqb.end()) {
                ss.erase({t2, freqb[t2]});
            }
            freqb[t1]+=tt.S;
            freqb[t2]+=tt.S;
            ss.insert({t1, freqb[t1]});
            ss.insert({t2, freqb[t2]});
        }
    }
    if (!ss.empty() || !freqa.empty())
        cout << "NO\n";
    else
        cout << "YES\n";

}

signed main() {
    fast;
    int t = 1;
    cin >> t;
    while(t--){
    	solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;typedef pair<int,int> pii;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const ll mod = 1e9+7, N = 2e6+7, M = 2e6+7, INF = INT_MAX/10;
ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}

void solve() {
	int n,m;
    cin >> n >> m;
    map<int,int>a,b;
    for(int i=0,x;i<n;i++)
        cin >> x,a[x]++;
    for(int j=0,x;j<m;j++)
        cin >> x,b[x]++;
    for(auto &pp:b) {
        int times = pp.second;
        while (times--) {
            int num = pp.first;
            while(a[num]==0) {
                if (num%2==0) {
                    if (a[num/2]>0) {
                        a[num/2]--;
                        num/=2;
                    } else {
                        cout << "NO\n";
                        return;
                    }
                } else {
                    int t1 = (num-1)/2;
                    int t2 = t1+1;
                    if (a[t1]>0) {
                        a[t1]--;
                        num-=t1;
                    } else if (a[t2]>0) {
                        a[t2]--;
                        num-=t2;
                    } else {
                        cout << "NO\n";
                        return;
                    }
                }
            }
            a[num]--;
        }
    }
    int remaining = 0;
    for(auto &pp:a)
        remaining+=pp.second;
    if (remaining>0)
        cout << "NO\n";
    else
        cout << "YES\n";
}

signed main() {
    fast;
    int t = 1;
    cin >> t;
    while(t--){
    	solve();
    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>

#define ll long long int

#define pb push_back

using namespace std;

const int MAXN = 2e5 + 10;

int main(){

	ll t;

	scanf("%lld", &t);

	while(t--){

		ll n, m, x;

		scanf("%lld %lld", &n, &m);

		multiset<ll> a;

		vector<ll> b;

		ll suma_a = 0;

		ll suma_b = 0;

		for(ll i = 0; i < n; i++){

			scanf("%lld", &x);

			suma_a += x;

			a.insert(x);

		}

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

			scanf("%lld", &x);

			suma_b += x;

			b.pb(x);

		}

		if(suma_a != suma_b){

			printf("No\n");

		}

		else{

			ll k = 0;

			while(b.size() > 0 && k <= n + m){

				k++;

				if(a.count(b[b.size() - 1]) > 0){

					a.erase(a.find(b[b.size() - 1]));

					b.pop_back();

				}

				else{

					ll x1 = b[b.size() - 1]/2;

					ll x2 = b[b.size() - 1]/2;

					if(b[b.size() - 1] % 2 == 1){

						x2++;

					}

					b.pop_back();

					if(a.count(x1) > 0){

						a.erase(a.find(x1));

					}

					else{

						b.push_back(x1);

					}

					if(a.count(x2) > 0){

						a.erase(a.find(x2));

					}

					else{

						b.push_back(x2);

					}

				}

			}

			if(b.size() == 0){

				printf("Yes\n");

			}

			else{

				printf("No\n");

			}

		}

	}

	return 0;

}

#include<bits/stdc++.h>

#define ll long long int

#define pb push_back

using namespace std;

const int MAXN = 2e5 + 10;

int main(){

	ll t;

	scanf("%lld", &t);

	while(t--){

		ll n, m, x;

		scanf("%lld %lld", &n, &m);

		multiset<ll> a;

		vector<ll> b;

		ll suma_a = 0;

		ll suma_b = 0;

		for(ll i = 0; i < n; i++){

			scanf("%lld", &x);

			suma_a += x;

			a.insert(x);

		}

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

			scanf("%lld", &x);

			suma_b += x;

			b.pb(x);

		}

		if(suma_a != suma_b){

			printf("No\n");

		}

		else{

			ll k = 0;

			while(b.size() > 0 && k <= n + 2){

				k++;

				if(a.find(b[b.size() - 1]) != a.end()){

					a.erase(a.find(b[b.size() - 1]));

					b.pop_back();

				}

				else{

					ll x1 = b[b.size() - 1]/2;

					ll x2 = b[b.size() - 1]/2;

					if(b[b.size() - 1] % 2 == 1){

						x2++;

					}

					b.pop_back();

					if(a.find(x1) != a.end()){

						a.erase(a.find(x1));

					}

					else{

						b.push_back(x1);

					}

					if(a.find(x2) != a.end()){

						a.erase(a.find(x2));

					}

					else{

						b.push_back(x2);

					}

				}

			}

			if(b.size() == 0){

				printf("Yes\n");

			}

			else{

				printf("No\n");

			}

		}

	}

	return 0;

}

Answer: 2


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "DEBUG/debug.h"
#else
#define debug(x)
#endif
using namespace std;
bool Delete(long long ele, multiset<long long> &a)
{
    auto it = a.find(ele);
    if (it != a.end())
    {
        a.erase(it);
        return true;
    }
    if (ele <= 1)
    {
        return false;
    }
    long long x = ele / 2;
    long long y = (ele + 1) / 2;
    return Delete(x, a) & Delete(y, a);
}
void solve()
{
    long long n, m;
    cin >> n >> m;
    vector<long long> va(n), vb(m);
    for (auto &x : va)
        cin >> x;
    for (auto &y : vb)
        cin >> y;
    sort(va.begin(), va.end());
    sort(vb.begin(), vb.end());
    multiset<long long> a;
    for (auto &e : va)
    {
        a.insert(e);
    }
    debug(a);
    debug(va);
    debug(vb);
    for (long long i = 0; i < m; i++)
    {
        long long ele = vb[i];
        if (!Delete(ele, a))
        {
            cout << "No" << endl;
            return;
        }
    }
    if(!a.empty())
    {
        cout<<"No\n";
        return;
    }
    cout << "Yes\n";
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long long t = 1;
    cin >> t;

    while (t--)
    {
        // cout<<9-t+1<<" ";
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "DEBUG/debug.h"
#else
#define debug(x)
#endif
using namespace std;
bool Delete(long long ele, multiset<long long> &a)
{
    if(a.empty()) return false;
    auto it = a.find(ele);
    if (it != a.end())
    {
        a.erase(it);
        return true;
    }
    if (ele <= 1)
    {
        return false;
    }
    long long x = ele / 2;
    long long y = (ele + 1) / 2;
    bool check1=Delete(x, a);
    if(check1==false) return false;
    bool check2=Delete(y, a);
    if(check2==false) return false;
    return true;
}
void solve()
{
    long long n, m;
    cin >> n >> m;
    vector<long long> va(n), vb(m);
    for (auto &x : va)
        cin >> x;
    for (auto &y : vb)
        cin >> y;
    sort(va.begin(), va.end());
    sort(vb.begin(), vb.end());
    multiset<long long> a, b;
    for (auto &e : va)
    {
        a.insert(e);
    }
    for(auto &e: vb)
    {
        b.insert(e);
    }
    debug(a);
    debug(b);
    while(!a.empty() && !b.empty())
    {
        auto itb=b.begin();
        auto ita=a.find(*itb);
        if(ita!=a.end())
        {
            a.erase(ita);
            b.erase(itb);
            continue;
        }

        int ele=*itb;
        int x=ele/2;
        int y=ele-x;

        if(ele<=1)
        {
            cout<<"No\n";
            return;
        }

        b.erase(itb);
        b.insert(x);
        b.insert(y);

    }
    if(!a.empty() || !b.empty())
    {
        cout<<"No\n";
        return;
    }
    cout<<"Yes\n";
    return;
    debug(a);
    debug(va);
    debug(vb);
    for (long long i = 0; i < m; i++)
    {
        long long ele = vb[i];
        if (!Delete(ele, a))
        {
            cout << "No" << endl;
            return;
        }
    }
    if(!a.empty())
    {
        cout<<"No\n";
        return;
    }
    cout << "Yes\n";
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long long t = 1;
    cin >> t;

    while (t--)
    {
        // cout<<9-t+1<<" ";
        solve();
    }

    return 0;
}

Answer: 2


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

int main() {
	int T;
	scanf("%d", &T);

	while (T--) {
		int n, m;
		scanf("%d%d", &n, &m);

		priority_queue<int> a, b;
		for (int i = 0; i < n; i++) {
			int x;
			scanf("%d", &x);
			a.push(x);
		}
		for (int i = 0; i < m; i++) {
			int x;
			scanf("%d", &x);
			b.push(x);
		}

		bool ok = true;
		while (!a.empty()) {
			if (b.empty() || b.size() > a.size()) {
				ok = false;
				break;
			}

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

			if (x < a.top()) {
				ok = false;
				break;
			}
			if (x == a.top()) {
				a.pop();
			} else {
				b.push(x / 2);
				b.push((x + 1) / 2);
			}
		}

		if (!b.empty()) {
			ok = false;
		}

		if (ok) {
			puts("Yes");
		} else {
			puts("No");
		}
	}

    return 0;
}

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

int main() {
	int T;
	scanf("%d", &T);

	while (T--) {
		int n, m;
		scanf("%d%d", &n, &m);

		priority_queue<int> a, b;
		for (int i = 0; i < n; i++) {
			int x;
			scanf("%d", &x);
			a.push(x);
		}
		for (int i = 0; i < m; i++) {
			int x;
			scanf("%d", &x);
			b.push(x);
		}

		bool ok = true;
		while (!a.empty()) {
			if (b.empty() || b.size() > a.size()) {
				ok = false;
				break;
			}

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

			if (x < a.top()) {
				ok = false;
				break;
			}
			if (x == a.top()) {
				a.pop();
			} else {
				b.push(x / 2);
				b.push(x / 2 + 1);
			}
		}

		if (!b.empty()) {
			ok = false;
		}

		if (ok) {
			puts("Yes");
		} else {
			puts("No");
		}
	}

    return 0;
}

Answer: 1


#include <iostream>
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
#define int long long

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> freqa, freqb;
    set<int> seta, setb;

    for(auto ele: a){
        freqa[ele]++;
        seta.insert(ele);
    }

    for(auto ele: b){
        freqb[ele]++;
        setb.insert(ele);
    }

    while(true){
        if(seta.empty() && setb.empty()){
            cout << "YES" << endl;
            return;
        }

        if(seta.empty() || setb.empty()){
            cout << "NO" << endl;
            return;
        }

        int max1 = *prev(set.end());
        int max2 = *prev(set.end());

        if(max1 > max2){
            cout << "NO" << endl;
            return;
        }

        if(freqa[max2]){
            int minfreq = min(freqa[max2], freqb[max2]);
            freqa[max2] = freqa[max2] - minfreq;
            freqb[max2] = freqb[max2] - minfreq;

            if(freqa[max2] == 0) seta.erase(max2);
            if(freqb[max2] == 0) setb.erase(max2);
        }

        else{
            int cl = (max2 + 1)/2;
            int fl = max2/2;
            freqb[cl] = freqb[cl] + freqb[max2];
            freqb[fl] = freqb[fl] + freqb[max2];
            freqb[max2] = 0;
            setb.insert(cl);
            setb.insert(fl);
            setb.erase(max2);
        }
    }
}

signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

#include <iostream>
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
#define int long long

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> freqa, freqb;
    set<int> seta, setb;

    for(auto ele: a){
        freqa[ele]++;
        seta.insert(ele);
    }

    for(auto ele: b){
        freqb[ele]++;
        setb.insert(ele);
    }

    while(true){
        if(seta.empty() && setb.empty()){
            cout << "YES" << endl;
            return;
        }

        if(seta.empty() || setb.empty()){
            cout << "NO" << endl;
            return;
        }

        int max1 = *prev(seta.end());
        int max2 = *prev(setb.end());

        if(max1 > max2){
            cout << "NO" << endl;
            return;
        }

        if(freqa[max2]){
            int minfreq = min(freqa[max2], freqb[max2]);
            freqa[max2] = freqa[max2] - minfreq;
            freqb[max2] = freqb[max2] - minfreq;

            if(freqa[max2] == 0) seta.erase(max2);
            if(freqb[max2] == 0) setb.erase(max2);
        }

        else{
            int cl = (max2 + 1)/2;
            int fl = max2/2;
            freqb[cl] = freqb[cl] + freqb[max2];
            freqb[fl] = freqb[fl] + freqb[max2];
            freqb[max2] = 0;
            setb.insert(cl);
            setb.insert(fl);
            setb.erase(max2);
        }
    }
}

signed 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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree< T , null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update >;
// *st.find_by_order(i) --> element at ith position
// st.order_of_key(x) --> no. of ele less than x
//For multiset change less to less_equal and for reverse order change less to greater
#define int long long
#define endl '\n'
#define all(x) (x).begin(), (x).end()
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v){for(auto &it : v)cin >> it;return istream;}
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c){for(auto &it : c)cout << it << " ";return ostream;}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int testCase; cin >> testCase;
    while(testCase--) {
        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);
        }
        int sumA = accumulate(all(A),0LL) , sumB = accumulate(all(B),0LL);
        if(m > n or sumA != sumB) {
            cout << "No" << endl;
            continue;
        }
        while(B.size() > 0 and A.size() > 0) {
            if(*(--A.end()) > *(--B.end())) break;
            if(*(--A.end()) == *(--B.end())) {
                A.erase(--A.end());
                B.erase(--B.end());
            } else {
                int x = *(--B.end());
                B.erase(--B.end());
                B.insert(x / 2);
                B.insert((x + 1) / 2);
            }
        }
        if(A == B) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree< T , null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update >;
// *st.find_by_order(i) --> element at ith position
// st.order_of_key(x) --> no. of ele less than x
//For multiset change less to less_equal and for reverse order change less to greater
#define int long long
#define endl '\n'
#define all(x) (x).begin(), (x).end()
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v){for(auto &it : v)cin >> it;return istream;}
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c){for(auto &it : c)cout << it << " ";return ostream;}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int testCase; cin >> testCase;
    while(testCase--) {
        int n , m;
        cin >> n >> m;
        vector<int>arr(n) , brr(m);
        cin >> arr >> brr;
        vector<int>tmp;
        multiset<int>A(all(arr)),B(all(brr));
        if(m > n) {
            cout << "No" << endl;
            continue;
        }
        auto i = B.begin();
        while(i != B.end()) {
            auto it = A.find(*i);
            if(it != A.end()) {
                tmp.push_back(*it);
                A.erase(it);
            }
            ++i;
        }
        for(auto j : tmp) {
            auto it = B.find(j);
            if(it != B.end()) B.erase(it);
        }
        int diff = max(n - m,10000LL);
        while(diff-- and B.size() > 0 and A.size() > 0) {
            if(*(--A.end()) == *(--B.end())) {
                A.erase(--A.end());
                B.erase(--B.end());
            } else {
                int x = *(--B.end());
                B.erase(--B.end());
                B.insert(x / 2);
                B.insert((x + 1) / 2);
                if(x == 1) break;
            }
        }
        vector<int>finalA(all(A)) , finalB(all(B));
        if(finalA == finalB) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

Answer: 1


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

using ll = long long;
const ll mod = 1e9 + 7;
const ll modd = 998244353;

void solve() {
    ll n, m;
    cin>>n>>m;
    map<ll, ll>m1, m2;
    for(ll i = 0; i<n; i++) {
        ll x;
        cin>>x;
        m1[x]++;
    }
    for(ll i = 0; i<m; i++) {
        ll x;
        cin>>x;
        m2[x]++;
    }

    set<ll>s1, s2;
    for(auto&i:m1) {
        if(m2.find(i.first)!=m2.end()){
            ll k = m2[i.first];
            if(k>i.second) {
                m2[i.first]-=i.second;
                s1.insert(i.first);
            }
            else {
                i.second-=k;
                s2.insert(i.first);
                if(i.second == 0) s1.insert(i.first);
            }
        }
    }
    for(auto&i:s1) {
        m1.erase(i);
    }
    for(auto&i:s2) {
        m2.erase(i);
    }
    vector<ll>a, b;
    for(auto&i:m1) {
        ll k = i.second;
       //  cout<<i.first<<" "<<k<<"\n";
        while(k--) {
            a.push_back(i.first);
           //  cout<<i.first<<" ";
        }
    }
  //  cout<<"\n";

    for(auto&i:m2) {
        ll k = i.second;
       // cout<<i.first<<" "<<k<<"\n";
        while(k--) {
            b.push_back(i.first);
           // <F5> cout<<i.first<<" ";
        }
    }
   //  cout<<"\n";
    n = a.size(), m = b.size();
    if(n<m) {
        cout<<"NO\n";
        return;
    }

    ll i = 0, j = 0;

    while(i<n && j<m) {
        if(a[i]==b[j]) {
            i++;
            j++;
        }
        else if(b[j]<a[i]) {
            cout<<"NO\n";
            return;
        }
        else {
            ll prev = -1, s = 0;
            while(i<n && (prev == -1 ||abs(a[i]-prev<=1)) ){
                s+=a[i];
                prev = s;
                i++;
                if(b[j]==s) {
                    j++;
                    break;
                }
                else if(s>b[j]) {
                    cout<<"NO\n";
                    return;
                }
            }

        }
    }
    if(i<n ||  j<m) {
        cout<<"NO\n";
        return;
    }
    cout<<"YES\n";

}

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

    int t = 1;
    cin >> t;

    while (t--) solve();

    return 0;
}

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

using ll = long long;
const ll mod = 1e9 + 7;
const ll modd = 998244353;

void solve() {
    ll n, m;
    cin>>n>>m;
    multiset<ll>s1, s2;
    for(ll i = 0; i<n; i++) {
        ll x;
        cin>>x;
        s1.insert(x);
    }
    for(ll i = 0; i<m; i++) {
        ll x;
        cin>>x;
        s2.insert(x);
    }
    for(auto&i:s2) {
        auto it = s1.find(i);
        if(it!=s1.end()) {
            s1.erase(it);
        }
        else {
            ll a = i>>1ll, b = i>>1ll;
            if(a+b!=i) b++;
            queue<ll>q;
            q.push(a);
            q.push(b);
            while(!q.empty()) {
                ll e = q.front();
                q.pop();
                auto it1 = s1.find(e);
                if(it1!=s1.end()) {
                    s1.erase(it1);
                }
                else {
                    if(q.size()>s1.size()) {
                        cout<<"NO\n";
                        return;
                    }
                    ll c = e>>1ll, d = e>>1ll;
                    if(c==0) {
                        cout<<"NO\n";
                        return;
                    }
                    if(c+d!=e) d++;
                    q.push(c);
                    q.push(d);
                }
            }
        }
    }
    if(!s1.empty()) {
        cout<<"NO\n";
        return;
    }
    cout<<"YES\n";

}

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

    int t = 1;
    cin >> t;

    while (t--) solve();

    return 0;
}

Answer: 2


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T-- > 0) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = sc.nextInt();
            int[] b = new int[m];
            for (int i = 0; i < m; i++) b[i] = sc.nextInt();

            PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
            for (int val : b) pq.offer(val);

            Map<Integer, Integer> freqMap = new HashMap<>();
            for (int val : a) freqMap.put(val, freqMap.getOrDefault(val, 0) + 1);

            boolean isValid = true;

            for (int i = 0; i < n - m + n; i++) {
                if (pq.isEmpty()) {
                    isValid = false;
                    break;
                }

                int val = pq.poll();
                if (freqMap.getOrDefault(val, 0) == 0) {
                    pq.offer(val / 2);
                    pq.offer((val + 1) / 2);
                } else {
                    freqMap.put(val, freqMap.get(val) - 1);
                }
            }

            System.out.println(isValid && pq.isEmpty() ? "Yes" : "No");
        }
        sc.close();
    }
}

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = scanner.nextInt();
            int[] b = new int[m];
            for (int i = 0; i < m; i++) b[i] = scanner.nextInt();
            System.out.println(canDistribute(a, b) ? "Yes" : "No");
        }
        scanner.close();
    }

    private static boolean canDistribute(int[] a, int[] b) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
        for (int val : b) maxHeap.offer(val);

        Map<Integer, Integer> freqMap = new HashMap<>();
        for (int val : a) freqMap.put(val, freqMap.getOrDefault(val, 0) + 1);

        while (!maxHeap.isEmpty()) {
            int current = maxHeap.poll();
            if (freqMap.getOrDefault(current, 0) > 0) {
                freqMap.put(current, freqMap.get(current) - 1);
            } else {
                if (current == 1) return false;
                maxHeap.offer(current / 2);
                maxHeap.offer((current + 1) / 2);
            }
        }

        for (int count : freqMap.values()) {
            if (count > 0) return false;
        }
        return true;
    }
}

Answer: 1


from random import randint
class MyDict:
    def __init__(self, func=lambda: 0):
        self.random = randint(0, (1 << 32) - 1)
        self.default = func
        self.dict = {}
    def _hash(self, key):
        return self.random ^ hash(key)
    def __getitem__(self, key):
        mykey = self._hash(key)
        if mykey not in self.dict:
            self.dict[mykey] = self.default()
        return self.dict[mykey]
    def __setitem__(self, key, value):
        self.dict[self._hash(key)] = value
    def __delitem__(self, key):
        mykey = self._hash(key)
        if mykey in self.dict:
            del self.dict[mykey]
        else:
            raise KeyError(key)
    def get(self, key, default=None):
        return self.dict.get(self._hash(key), default)
    def keys(self):
        return [self._hash(k) for k in self.dict]
    def values(self):
        return self.dict.values()
    def items(self):
        return [(self._hash(k), v) for k, v in self.dict.items()]
    def __contains__(self, key):
        return self._hash(key) in self.dict
    def __iter__(self):
        return iter(self.keys())
    def __len__(self):
        return len(self.dict)
    def __str__(self):
        return str({self._hash(k): v for k, v in self.dict.items()})
    def __repr__(self):
        return f"MyDict({self.default}, {self.dict})"

import heapq
def solve():
    n,m = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    st = []
    for i in b :
        st.append(-i)

    heapq.heapify(st)

    freq = MyDict()
    for i in a:
        freq[i] += 1

    ii = -1
    for i in range(2*n-m):
        if not st:
            print("NO")
            return
        val = - heapq.heappop(st)
        if freq[val] != 0:
            freq[val] -= 1

        else:
            heapq.heappush(st,-(val // 2))
            heapq.heappush(st,-(val+2-1)//2)
    if st :
        print("NO")
    else:
        print("YES")

for __ in range(int(input())):
    solve()

from random import randint
class MyDict:
    def __init__(self, func=lambda: 0):
        self.random = randint(0, (1 << 32) - 1)
        self.default = func
        self.dict = {}
    def _hash(self, key):
        return self.random ^ hash(key)
    def __getitem__(self, key):
        mykey = self._hash(key)
        if mykey not in self.dict:
            self.dict[mykey] = self.default()
        return self.dict[mykey]
    def __setitem__(self, key, value):
        self.dict[self._hash(key)] = value
    def __delitem__(self, key):
        mykey = self._hash(key)
        if mykey in self.dict:
            del self.dict[mykey]
        else:
            raise KeyError(key)
    def get(self, key, default=None):
        return self.dict.get(self._hash(key), default)
    def keys(self):
        return [self._hash(k) for k in self.dict]
    def values(self):
        return self.dict.values()
    def items(self):
        return [(self._hash(k), v) for k, v in self.dict.items()]
    def __contains__(self, key):
        return self._hash(key) in self.dict
    def __iter__(self):
        return iter(self.keys())
    def __len__(self):
        return len(self.dict)
    def __str__(self):
        return str({self._hash(k): v for k, v in self.dict.items()})
    def __repr__(self):
        return f"MyDict({self.default}, {self.dict})"

import heapq
def solve():
    n,m = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    st = []
    for i in b :
        st.append(-i)

    heapq.heapify(st)

    freq = MyDict()
    for i in a:
        freq[i] += 1

    for i in range(2*n-m):
        if not st:
            print("NO")
            return
        val = - heapq.heappop(st)
        if freq[val] != 0:
            freq[val] -= 1

        else:
            heapq.heappush(st,-(val // 2))
            heapq.heappush(st,-((val+2-1)//2))
    if st :
        print("NO")
    else:
        print("YES")

for __ in range(int(input())):
    solve()

Answer: 2


#include<bits/stdc++.h>
#define int long long
using namespace std;
priority_queue<int,vector<int>,greater<int> >a,b;
map<int,int> ma;
int n,m;
bool cal(int x)
{
if(x==0)return 0;
if(ma[x]>0)
{
	ma[x]--;
	n--;
	return 1;
}
return cal(x/2)&&cal(x/2+x%2);
}
void solve()
{

	cin>>n>>m;
	ma.clear();
	for(int i=1;i<=n;i++)
	{
		int temp;cin>>temp;
		ma[temp]++;
	}
	for(int i=1;i<=m;i++)
	{
		int temp;cin>>temp;
		if(cal(temp)==0)
		{
			cout<<"NO\n";
			return ;
		}
	}
	if(n==0)
	cout<<"YES\n";
else cout<<"NO\n";
	return;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;
	cin>>T;
	while(T--)
	solve();
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int,int> ma;
int n,m;
bool cal(int x)
{
if(ma[x]>0)
{
	ma[x]--;
	n--;
	return 1;
}
if(x==1)
	return 0;
return cal(x/2)&&cal(x/2+x%2);
}
void solve()
{

	cin>>n>>m;
	ma.clear();
	for(int i=1;i<=n;i++)
	{
		int temp;cin>>temp;
		ma[temp]++;
	}
	for(int i=1;i<=m;i++)
	{
		int temp;cin>>temp;
		if(cal(temp)==0)
		{
			cout<<"NO\n";
			for(int j=i+1;j<=m;j++)
				cin>>temp;
			return ;
		}
	}
	if(n==0)
	cout<<"YES\n";
else cout<<"NO\n";
	return;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;
	cin>>T;
	while(T--)
	solve();
}

Answer: 2


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
template<typename T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
typedef unsigned long long int ull;
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mpi map<int,int>
#define  si set<int>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define reP(i,a,n) for(int i=a;i<=n;i++)
#define YES cout<<"YES\n";
#define NO cout<<"NO\n";
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define case(i,n) Case<<" "<<i<<": "<<n<<endl;
#define endl "\n"
#define w(t) int t;cin>>t;while(t--)
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)(a).size()
#define MOD 1000000007

void solve(){

ll n,m;
cin>>n>>m;
set<ll>a,b;
map<ll,ll>mp1,mp2;
ll x;
rep(i,0,n)
{
	cin>>x;
	mp1[x]++;
	a.insert(x);
}rep(i,0,m)
{
	cin>>x;
	mp2[x]++;
	b.insert(x);
}
while(true){
	if(a.empty() && b.empty()){
		YES
		return;
	}
	if(a.empty() || b.empty()){
		NO
		return;
	}
	ll mxa= *a.rbegin();
	ll mxb= *b.rbegin();
	if(mxa>mxb){
		NO
		return;
	}
	if(mp1[mxb]){
		ll mn = min(mp1[mxb],mp2[mxb]);
		mp1[mxb]-=mn;
		mp2[mxb]-=mn;
		if(mp1[mxb]==0){
			a.erase(mxb);
		}if(mp2[mxb]==0){
			b.erase(mxb);
		}
	}
	else
	{
		ll l = mxb/2;
		ll r = (mxb+1)/2;
		mp2[l]+=mp2[mxb];
		mp2[r]+=mp2[mxb];
		mp2[mxb]=0;
		b.insert(l);
		b.insert(r);
		b.erase(mxb);
	}

}

}
int main()
{
     #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
clock_t z =  clock();
t=1;
cin>>t;
while(t--)
{
solve();
}

cerr<<"Run Time : "<<((double)(clock()-z)/CLOCKS_PER_SEC);
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
template<typename T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
typedef unsigned long long int ull;
#define vi vector<int>
#define vpi vector<pair<int,int> >
#define mpi map<int,int>
#define  si set<int>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define reP(i,a,n) for(int i=a;i<=n;i++)
#define YES cout<<"YES\n";
#define NO cout<<"NO\n";
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define case(i,n) Case<<" "<<i<<": "<<n<<endl;
#define endl "\n"
#define w(t) int t;cin>>t;while(t--)
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)(a).size()
#define MOD 1000000007

void solve(){

ll n,m;
cin>>n>>m;
vector<ll>ar(n);
vector<ll>br(m);
map<ll,ll>mp1,mp2;
rep(i,0,n)
{
	cin>>ar[i];
	mp1[ar[i]]++;
}
ll sum=0;

rep(i,0,m)
{
	cin>>br[i];
	if(mp1[br[i]]>0){
		mp1[br[i]]--;
		//cout<<"hi "<<br[i]<<" "<<mp1[br[i]]<<endl;
	}
	else
	{
		mp2[br[i]]++;
	}
	sum+=br[i];
	// cout<<sum<<endl;
}

if(sum<n){
	//cout<<"hi"<<endl;
	NO
	return;
}
vector<ll>a,b;
for(auto it:mp1){
	rep(i,0,it.se){
		a.pb(it.fi);
	}
}
for(auto it:mp2){
	rep(i,0,it.se){
		b.pb(it.fi);
	}
}
int cnt = sz(a)-sz(b);
ll l;
if(sz(a)>0)
l=a[0];
for(int i=1;i<sz(a);i++){
		if(abs(l-a[i])<=1){
				l+=a[i];

				if(mp2[l]>0){

					mp2[l]--;

					if(i+1<sz(a)){
						l=a[i+1];
						i++;
					}
				}
		}
		else
		{
			NO
			return;
		}
}

for(auto it:mp2){
	if(it.se!=0){
		NO
		return;
	}
}
YES

}
int main()
{
     #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
// ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
int t;
clock_t z =  clock();
t=1;
cin>>t;
while(t--)
{
solve();
}

cerr<<"Run Time : "<<((double)(clock()-z)/CLOCKS_PER_SEC);
}

Answer: 1


#ifndef ONLINE_JUDGE
#include "debugger.h"
#else
#define dbg(...)
#endif
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pii pair<ll, ll>
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define minus cout << -1 << endl
#define fastread()                \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;
void solve()
{
    int n, m;
    cin >> n >> m;
    multiset<int> m1, m2;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        m1.insert(x);
    }

    for (int i = 0; i < m; i++)
    {
        int x;
        cin >> x;
        m2.insert(x);
    }
    // multiset<int> m3;
    int times = 2*(n+m);
    while (times--)
    {
        if (m1.size() == 0 || m2.size() == 0)
            break;
        int num = *m2.rbegin();
        if (m1.find(num) != m1.end())
        {
            m2.erase(m2.find(num));
            m1.erase(m1.find(num));
            continue;
        }
        int num1 = num / 2;
        int num2 = (num / 2) + (num % 2);
        m2.erase(m2.find(num));
        m2.insert(num1);
        m2.insert(num2);
    }

    if (m1 == m2)
        cyes;
    else
        cno;
}

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

    return 0;
}

#ifndef ONLINE_JUDGE
#include "debugger.h"
#else
#define dbg(...)
#endif
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pii pair<ll, ll>
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define minus cout << -1 << endl
#define fastread()                \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;
void solve()
{
    int n, m;
    cin >> n >> m;
    multiset<int> m1, m2;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        m1.insert(x);
    }

    for (int i = 0; i < m; i++)
    {
        int x;
        cin >> x;
        m2.insert(x);
    }
    // multiset<int> m3;
    int times = n + m;
    while (times--)
    {
        if (m1.size() == 0 || m2.size() == 0)
            break;
        int num = *m2.rbegin();
        if (m1.find(num) != m1.end())
        {
            m2.erase(m2.find(num));
            m1.erase(m1.find(num));
            continue;
        }
        int num1 = num / 2;
        int num2 = (num / 2) + (num % 2);
        m2.erase(m2.find(num));
        m2.insert(num1);
        m2.insert(num2);
    }

    if (m1 == m2)
        cyes;
    else
        cno;
}

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

    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define ll int
#define lld long double
#define pll pair <ll, ll>
#define f(i, a, b) for(ll i = a; i < b; i++)
#define br cout<<"\n";
const long long int N =  2e5+20;
const long long int M =  998244353;

ll binpow(ll x, ll y, ll MOD){
    ll res = 1; x %= MOD;
    while(y > 0){
        if(y&1ll){res = (res * x) % MOD;}
        x = (x * x) % MOD;
        y >>= 1LL;
    }
    res %= MOD;
    return res;
}

void solve(){
    ll n, m;
    cin>>n>>m;
    vector <ll> a(n), b(m);
    multiset <ll> s, t;
    f(i, 0, n){
        cin>>a[i];
        s.insert(a[i]);
    }
    f(j, 0, m){
        cin>>b[j];
        t.insert(b[j]);
    }
    ll cnt = 0;
    while(cnt < n-m){
        vector <ll> v;
        for(auto x: t){
            if(s.find(x) != s.end()){
                s.erase(s.find(x));
            }
            else{
                cnt++;
                v.push_back(x/2);
                v.push_back((x+1)/2);
            }

        }
        t.clear();
        assert(t.size() == 0);
        for(auto x: v){
            t.insert(x);
        }
        if(t.empty() || s.empty()){break;}
    }
    if(t.size() != s.size()){
        cout<<"NO\n";
        return;
    }
    for(auto x: t){
        if(s.find(x) == s.end()){
            cout<<"NO\n";
            return;
        }
        if(s.count(x) != t.count(x)){
            cout<<"NO\n";
            return;
        }
    }
    for(auto x: s){
        if(t.find(x) == t.end()){
            cout<<"NO\n";
            return;
        }
        if(s.count(x) != t.count(x)){
            cout<<"NO\n";
            return;
        }
    }
    cout<<"YES\n";
    return;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int TT = 1;
    cin >> TT;
    f(tt, 1, TT+1){
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll int
#define lld long double
#define pll pair <ll, ll>
#define f(i, a, b) for(ll i = a; i < b; i++)
#define br cout<<"\n";
const long long int N =  2e5+20;
const long long int M =  998244353;

ll binpow(ll x, ll y, ll MOD){
    ll res = 1; x %= MOD;
    while(y > 0){
        if(y&1ll){res = (res * x) % MOD;}
        x = (x * x) % MOD;
        y >>= 1LL;
    }
    res %= MOD;
    return res;
}

void solve(){
    ll n, m;
    cin>>n>>m;
    vector <ll> a(n), b(m);
    multiset <ll> s, t;
    f(i, 0, n){
        cin>>a[i];
        s.insert(a[i]);
    }
    f(j, 0, m){
        cin>>b[j];
        t.insert(b[j]);
    }
    ll cnt = 0;
    while(cnt < n-m){
        vector <ll> v;
        for(auto x: t){
            if(s.find(x) != s.end()){
                s.erase(s.find(x));
            }
            else{
                cnt++;
                v.push_back(x/2);
                v.push_back((x+1)/2);
            }

        }
        t.clear();
        assert(t.size() == 0);
        for(auto x: v){
            t.insert(x);
        }
        if(t.empty() || s.empty()){break;}
    }
    if(t.size() != s.size()){
        cout<<"NO\n";
        return;
    }
    vector <ll> x, y;
    for(auto z: t){
        x.push_back(z);
    }
    for(auto z: s){
        y.push_back(z);
    }
    f(i, 0, x.size()){
        if(x[i] != y[i]){
            cout<<"NO\n";
            return;
        }
    }

    cout<<"YES\n";
    return;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int TT = 1;
    cin >> TT;
    f(tt, 1, TT+1){
        solve();
    }
    return 0;
}

Answer: 2


#include<iostream>
#include<queue>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		priority_queue<int> q;
		priority_queue<int> Q;
		int n,m;
		cin>>n>>m;
		for(int i=0;i<n;i++){
			int a;
			cin>>a;
			q.push(a);
		}
		for(int i=0;i<m;i++){
			int b;
			cin>>b;
			Q.push(b);
		}
		while(1){
			if(m>n){
				cout<<"No"<<'\n';
				break;
			}
			if(Q.empty() and !q.empty()){
				cout<<"No"<<'\n';
				break;
			}
			if(q.empty() and Q.empty()){
				cout<<"Yes"<<'\n';
				break;
			}
			int a = q.top();
			int b = Q.top();
			cout<<a<<' '<<b<<'\n';
			if(a==b){
				q.pop();
				Q.pop();
				n--;
				m--;
			}
			if(b>a){
				Q.pop();
				Q.push(b/2);
				Q.push(b-b/2);
				m--;
				m+=2;
			}
			if(b<a){
				cout<<"No"<<'\n';
				break;
			}
		}
	}
}

#include<iostream>
#include<queue>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		priority_queue<int> q;
		priority_queue<int> Q;
		int n,m;
		cin>>n>>m;
		for(int i=0;i<n;i++){
			int a;
			cin>>a;
			q.push(a);
		}
		for(int i=0;i<m;i++){
			int b;
			cin>>b;
			Q.push(b);
		}
		while(1){
			if(m>n){
				cout<<"No"<<'\n';
				break;
			}
			if(Q.empty() and !q.empty()){
				cout<<"No"<<'\n';
				break;
			}
			if(q.empty() and Q.empty()){
				cout<<"Yes"<<'\n';
				break;
			}
			int a = q.top();
			int b = Q.top();
			//cout<<a<<' '<<b<<'\n';
			if(a==b){
				q.pop();
				Q.pop();
				n--;
				m--;
			}
			if(b>a){
				Q.pop();
				Q.push(b/2);
				Q.push(b-b/2);
				m--;
				m+=2;
			}
			if(b<a){
				cout<<"No"<<'\n';
				break;
			}
		}
	}
}

Answer: 2


#include <iostream>

#include <vector>

#include <map>

using namespace std;

typedef long long ll;

int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int t;

    cin >> t;

    while(t--){

        int n, m;

        cin >> n >> m;

        vector<ll> 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];

        }

        // Invariant check: the sums must be equal.

        if(sumA != sumB){

            cout << "No\n";

            continue;

        }

        // Build frequency map for a.

        map<ll, ll> freq;

        for(auto x : a)

            freq[x]++;

        // Build the need map from b.

        // need[x] means: we need x to appear this many times.

        map<ll, ll> need;

        for(auto x : b)

            need[x]++;

        bool possible = true;

        // Process the need map in descending order.

        while(!need.empty() && possible){

            // Get the largest key.

            auto it = need.end();

            it--;

            ll x = it->first;

            ll cnt = it->second;

            need.erase(it);

            // Use available copies from freq.

            ll use = 0;

            if(freq.count(x))

                use = min(freq[x], cnt);

            cnt -= use;

            if(freq.count(x))

                freq[x] -= use;

            // If we still need more copies of x but cannot split further, fail.

            if(cnt > 0){

                if(x == 1){

                    possible = false;

                    break;

                }

                // Split each copy of x.

                if(x % 2 == 0){

                    // x even => must come from two copies of x/2.

                    need[x/2] += cnt * 2;

                } else {

                    // x odd => must come from one copy of floor(x/2) and one copy of floor(x/2)+1.

                    need[x/2]     += cnt;

                    need[x/2 + 1] += cnt;

                }

            }

        }

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

    }

    return 0;

}

#include <iostream>

#include <vector>

#include <unordered_map>

using namespace std;

typedef long long ll;

// Global frequency map to store counts of numbers in array a.

unordered_map<ll, ll> freq;

// Recursive function to check if we can split x into numbers that exactly match entries in freq.

bool solve(ll x) {

    // If x is present in freq, use one occurrence.

    if(freq[x] > 0) {

        freq[x]--;

        return true;

    }

    // If x cannot be split further, return false.

    if(x == 1)

        return false;

    // Otherwise, try splitting x into two parts:

    // One part is floor(x/2) and the other is ceil(x/2).

    return solve(x / 2) && solve((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<ll> 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];

        }

        // The sum invariant must hold: otherwise it's impossible.

        if(sumA != sumB){

            cout << "No\n";

            continue;

        }

        // Build the frequency map for array a.

        freq.clear();

        for(auto x : a)

            freq[x]++;

        // Try to decompose the total sum (which equals sumB) into elements from a.

        if(solve(sumB))

            cout << "Yes\n";

        else

            cout << "No\n";

    }

    return 0;

}

Answer: 1


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

bool canTransform(vector<int>& a, vector<int>& b) {
    // Calculate the sum of both sequences
    long long sumA = accumulate(a.begin(), a.end(), 0LL);
    long long sumB = accumulate(b.begin(), b.end(), 0LL);

    if (sumA != sumB) {
        return false;
    }

    // Count frequencies in a and b
    map<int, int> freqA, freqB;
    for (int num : a) freqA[num]++;
    for (int num : b) freqB[num]++;

    // Iterate through the numbers in a
    for (auto& [num, count] : freqA) {
        if (freqB.count(num)) {
            int minCount = min(count, freqB[num]);
            count -= minCount;
            freqB[num] -= minCount;
        }

        // Try to combine numbers to match frequencies in b
        if (count > 0) {
            if (freqB.count(num + 1)) {
                int minCount = min(count, freqB[num + 1]);
                count -= minCount;
                freqB[num + 1] -= minCount;
            }
            if (count > 0) {
                return false;
            }
        }
    }

    // Check if all frequencies in b are matched
    for (auto& [num, count] : freqB) {
        if (count > 0) {
            return false;
        }
    }

    return true;
}

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

    int t;
    cin >> t;
    while (t--) {
        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];

        if (canTransform(a, b)) {
            cout << "Yes\n";
        } else {
            cout << "No\n";
        }
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cerr.rdbuf(cout.rdbuf());
#define f(i,a,b) for (ll (i)=(a); (i)<(b); (i)++)
#define rf(i,a,b) for (ll (i)=(a); (i)>=(b); (i)--)
#define vll vector<ll>
#define vvll vector<vll>
#define in(n) ll n; cin>>n;
#define sin(s) string (s); cin>>(s);
#define vin(v,n) vll (v)(n); f(i,0,n)cin>>(v)[i];
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sort(v) sort(all(v));
#define rsort(v) sort(v) reverse(all(v));
#define pYES cout<<"YES\n";
#define pNO cout<<"NO\n";
const ll mod = 1000000007;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " = "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

template <typename T>
void _print(T x) { cerr << x; }
template <typename T, typename V>
void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template <typename T>
void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T>
void _print(set<T> s) { cerr << "{ "; for (T i : s) { _print(i); cerr << " "; } cerr << "}"; }
template <typename T, typename V>
void _print(map<T, V> m) { cerr << "{ "; for (auto i : m) { _print(i); cerr << " "; } cerr << "}"; }
/* *********************Template ends here************** */

void solve() {
    in(n) in(m)
    vin(a,n)
    vin(b,m)
    multiset<ll> p(all(a)),q(all(b));
    ll diff=n-m;
    for(auto itt=q.begin();itt!=q.end(); itt++){
        auto it=p.find(*itt);
        if (it!=p.end()){
            p.erase(it);
            q.erase(itt);
        }else{
            if (diff==0 || *itt==1){
                pNO
                return;
            }else{
                ll x=*itt;
                q.erase(itt);
                ll xx=x/2, yy=x/2+x%2;
                if (xx!=0) q.insert(xx);
                if (yy!=0) q.insert(yy);
                if (q.size()>0) itt=q.begin();
                else break;
            }
        }
    }
    if (p.size()==0 && q.size()==0) pYES
    else pNO
}

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

Answer: 1


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("O3,unroll-loops")

using ll = long long;
using ld = long double;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;

constexpr ll inf=2e18;
constexpr ld eps=1e-9;
constexpr ll mod=1e9+7;

#define F first
#define S second
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()

template<typename T>
istream& operator>>(istream &istream, vector<T> &v)
{
	for(auto &it : v)
		cin>>it;
	return istream;
}

template<typename T>
ostream& operator<<(ostream &ostream, const vector<T> &c)
{
	for(auto &it : c)
		cout<<it<<" ";
	return ostream;
}

vector<int> fact_dp(2e5+2,-1);

int binpow(int a, int b, int m=mod)
{
	if(b==0)
		return 1;

	int x = binpow(a,b/2);
	if(b&1)
		return ((x*x)%m*a)%m;
	return (x*x)%m;
}

int modinv(int x, int m=mod)
{
	return binpow(x,m-2,m);
}

int fact(int x){
	if(x<=1) return 1;
	if(fact_dp[x] != -1) return fact_dp[x];

	return fact_dp[x] = (x%mod * fact(x-1)%mod)%mod;
}

int nCr(int n, int r){
	int x = (modinv(fact(r)) * modinv(fact(n-r)))%mod;
	int y = fact(n);
	return (x*y)%mod;
}

void solve()
{
	int n, m;
	cin >> n >> m;
	vector<int> a(n), b(m);
	cin >> a >> b;
	sort(all(a));
	sort(all(b));

	priority_queue<int, vector<int>, greater<int>> q;
	for(auto it : a){
		q.push(it);
	}

	int j = 0;
	while(!q.empty() && j<m){
		int cur = q.top();
		q.pop();

		if(cur==b[j]) j++;
		else{
			if(q.empty()){
				cout << "NO\n";
				return;
			}else{
				int cur2=q.top();

				if(abs(cur2-cur)<=1){
					q.pop();
					q.push(cur+cur2);
				}else{
					cout << "NO\n";
					return;
				}
			}
		}
	}

	if(q.empty() && j==m){
		cout << "YES\n";
	}else{
		cout << "NO\n";
	}
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(12) << fixed;

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

	return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("O3,unroll-loops")

using ll = long long;
using ld = long double;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;

constexpr ll inf=2e18;
constexpr ld eps=1e-9;
constexpr ll mod=1e9+7;

#define F first
#define S second
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()

template<typename T>
istream& operator>>(istream &istream, vector<T> &v)
{
	for(auto &it : v)
		cin>>it;
	return istream;
}

template<typename T>
ostream& operator<<(ostream &ostream, const vector<T> &c)
{
	for(auto &it : c)
		cout<<it<<" ";
	return ostream;
}

vector<int> fact_dp(2e5+2,-1);

int binpow(int a, int b, int m=mod)
{
	if(b==0)
		return 1;

	int x = binpow(a,b/2);
	if(b&1)
		return ((x*x)%m*a)%m;
	return (x*x)%m;
}

int modinv(int x, int m=mod)
{
	return binpow(x,m-2,m);
}

int fact(int x){
	if(x<=1) return 1;
	if(fact_dp[x] != -1) return fact_dp[x];

	return fact_dp[x] = (x%mod * fact(x-1)%mod)%mod;
}

int nCr(int n, int r){
	int x = (modinv(fact(r)) * modinv(fact(n-r)))%mod;
	int y = fact(n);
	return (x*y)%mod;
}

void solve()
{
	int n, m;
	cin >> n >> m;
	priority_queue<int> a, b;
	for(int i = 0 ; i < n ; i++){
		int x ; cin >> x;
		a.push(x);
	}
	for(int i = 0 ; i < m ; i++){
		int x ; cin >> x;
		b.push(x);
	}

	while(!b.empty() && !a.empty()){

		if(a.size()<b.size()){
			cout << "NO\n";
			return;
		}

		int btop = b.top(), atop = a.top();
		b.pop();
		if(btop==atop){
			a.pop();
		}else{
			b.push(btop/2);
			b.push((btop+1)/2);
		}
	}

	if(a.empty() && b.empty())cout << "YES\n";
	else cout << "NO\n";
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout << setprecision(12) << fixed;

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

	return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 998244353;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	while(t--){
		int n, m;
		cin >> n >> m;
		multiset<ll, greater<ll>> a, b;
		for(int i{0}; i<n; i++){
			ll k;
			cin >> k;
			a.insert(k);
		}
		for(int i{0}; i<m; i++){
			ll k;
			cin >> k;
			b.insert(k);
		}
        int c=0;
        while(!b.empty() && !a.empty()){
            auto eb=*b.begin();
            if(a.find(eb)!=a.end()){
                b.erase(b.begin());
                a.erase(a.find(eb));
            }else{
                if(++c>n-m)
                break;
                b.erase(b.begin());
                b.insert(eb/2);
                b.insert((eb+1)/2);
            }
        }
        if(a.empty() && b.empty())
        cout << "YES\n";
        else
        cout << "NO\n";
	}
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 998244353;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	while(t--){
		int n, m;
		cin >> n >> m;
		multiset<ll> a, b;
		for(int i{0}; i<n; i++){
			ll k;
			cin >> k;
			a.insert(k);
		}
		for(int i{0}; i<m; i++){
			ll k;
			cin >> k;
			b.insert(k);
		}
		int c=0;
		while(!a.empty() && !b.empty()){
			auto it2=a.begin(), it1=a.begin();
			it2++;
			auto bit1=b.find(*it1);
			if(bit1!=b.end()){
				b.erase(bit1);
				a.erase(it1);
			}else if(it2!=a.end()){
				if(*it2-*it1<=1){
					ll el=*it2+*it1;
					a.erase(it1);
					a.erase(it2);
					a.insert(el);
				}else{
					a.erase(it1);
					c++;
				}
			}else{
				a.erase(it1);
				c++;
			}
		}
		if(b.empty() && a.empty() && c==0)
		cout << "YES\n";
		else
		cout << "NO\n";
	}
}

Answer: 1


#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <map>
#include <vector>
using namespace std;
int tci = 0;

bool rec(int x, multiset<int> &values) {
	int h1 = x / 2, h2 = x - x / 2;
	int c1 = values.count(h1);
	int c2 = values.count(h2);

	// if (tci == 6)
	// 	cout << x << " " << h1 << " " << h2 << " " << c1 << " " << c2 << " " << values.count(x) << "\n";
	if (values.count(x)) {
		values.erase(values.find(x));
		return true;
	} else if (x == 1) {
		return false;
	} else if (c1 && c2) {
		return (h1 == h2 ? c1 >= 2: true) && rec(h1, values) && rec(h2, values);
	} else {
		return rec(h1, values) && rec(h2, values);
	}

	return false;
}

void solve()
{
	int n, m, x;
	cin >> n >> m;
	multiset<int> values;
	for (int i = 0; i < n; i++) {
		cin >> x;
		values.insert(x);
	}

	bool ans = true;
	for (int i = 0; i < m; i++) {
		cin >> x;
		if (!rec(x, values)) {
			ans = false;
		}
	}

	cout << (ans && !values.size() ? "Yes\n": "No\n");
}

int main()
{
	int tc;
	cin >> tc;

	for(tci = 0; tci < tc; tci++) {
		// cout << "Case #" << tci + 1 << ": ";
		solve();
	}

	return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <map>
#include <vector>
using namespace std;
int tci = 1;

bool rec(int x, multiset<int> &values) {
	if (values.count(x)) {
		values.erase(values.find(x));
		return true;
	} else if (x == 1) {
		return false;
	} else {
		return rec(x / 2, values) && rec(x - x / 2, values);
	}

	return false;
}

void solve()
{
	int n, m, x;
	cin >> n >> m;
	multiset<int> values;
	for (int i = 0; i < n; i++) {
		cin >> x;
		values.insert(x);
	}

	bool ans = true;
	for (int i = 0; i < m; i++) {
		cin >> x;
		if (!rec(x, values)) {
			ans = false;
		}
	}

	cout << (ans && !values.size() ? "Yes\n": "No\n");
}

int main()
{
	int tc;
	cin >> tc;

	for(tci = 1; tci <= tc; tci++) {
		// cout << "Case #" << tci + 1 << ": ";
		solve();
	}

	return 0;
}

Answer: 2


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

#define int long long
#define aa(z) int z; cin >> z
#define vi vector<int>
#define f(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define in(arr, n) for (int i = 0; i < n; i++) cin >> arr[i]
#define out(arr, n) for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl
#define srt(v) sort(v.begin(), v.end())
#define srtr(v) sort(v.begin(), v.end(), greater<int>())
#define err cout << fixed << setprecision(10)
#define lb(arr, d) lower_bound(arr, arr + n, d) - arr
const int mod = 998244353;
/// heyyyy pravesshhhhhh from mirzaaapurrrr  how areee youuuuuuuuuu thanks for giving me hint
void mohammaad_rafi() {
    aa(n);
    aa(m);
    vi a(n), b(m);
    in(a, n);
    in(b, m);
    map<int,int> mpa,mpb;
    f(i,n){
        mpa[a[i]]++;
    }
   f(i,m){
        mpb[b[i]]++;
    }

while(mpa.size()>0&&mpb.size()>0){
auto it=mpb.begin();
int key=it->first;
mpb[key]--;
if(mpb[key]==0){
    mpb.erase(key);
}
while(mpa.find(key)==mpa.end()){
    auto kt=mpa.begin();
    if(key<kt->first){
        cout<<"No"<<endl;return;
    }
    if(key%2==0){
mpb[key/2]++;
key=key/2;
    }
    else{
mpb[key/2+1]++;
key=key/2;

    }

}

if(mpa.find(key)!=mpa.end()){
    mpa[key]--;

    if(mpa[key]==0){
    mpa.erase(key);
}

}
else{
    break;
}

}

if(mpa.size()==0&&mpb.size()==0){
    cout<<"Yes"<<endl;
}
else{
       cout<<"No"<<endl;
}

}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    aa(t); // Number of test cases
    while (t--) {
        mohammaad_rafi();
    }
    return 0;
}

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

#define int long long
#define aa(z) int z; cin >> z
#define vi vector<int>
#define f(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define in(arr, n) for (int i = 0; i < n; i++) cin >> arr[i]
#define out(arr, n) for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl
#define srt(v) sort(v.begin(), v.end())
#define srtr(v) sort(v.begin(), v.end(), greater<int>())
#define err cout << fixed << setprecision(10)
#define lb(arr, d) lower_bound(arr, arr + n, d) - arr
const int mod = 998244353;

void mohammaad_rafi() {
    aa(n);
    aa(m);
    vi a(n), b(m);
    in(a, n);
    in(b, m);

    map<int, int> mp;
    srt(a);
    srt(b);

    // Populate the map with elements of `a`
    for (int x : a) {
        mp[x]++;
    }

    int i = 0;
    while (i < m) {
        if (mp.find(b[i]) != mp.end() && mp[b[i]] >= 1) {
            // Exact match found
            mp[b[i]]--;
            if (mp[b[i]] == 0) {
                mp.erase(b[i]);
            }
            i++;
        } else if (mp.find(b[i] / 2) != mp.end() && b[i] % 2 == 0 && mp[b[i] / 2] >= 2) {
            // Combine two smaller elements to form `b[i]` (even case)
            mp[b[i] / 2] -= 2;
            if (mp[b[i] / 2] == 0) {
                mp.erase(b[i] / 2);
            }
            i++;
        } else if (mp.find(b[i] / 2) != mp.end() && mp.find(b[i] / 2 + 1) != mp.end() &&
                   b[i] % 2 != 0 && mp[b[i] / 2] >= 1 && mp[b[i] / 2 + 1] >= 1) {
            // Combine two smaller elements to form `b[i]` (odd case)
            mp[b[i] / 2]--;
            mp[b[i] / 2 + 1]--;
            if (mp[b[i] / 2] == 0) {
                mp.erase(b[i] / 2);
            }
            if (mp[b[i] / 2 + 1] == 0) {
                mp.erase(b[i] / 2 + 1);
            }
            i++;
        } else {
            // Try to create `b[i]` by combining smaller elements
            auto it = mp.begin();
            while (!mp.empty() && it->first < b[i]) {
                int key = it->first;
                if (mp.find(key ) != mp.end() && mp[key ] >= 2) {
                    // Combine `key` and `key + 1` to form `key + key + 1`
                    mp[key]--;
                    mp[key ]--;
                     mp[key + key]++;
                    if (mp[key] == 0) {
                        mp.erase(key);
                    }
                    // if (mp[key ] == 0) {
                    //     mp.erase(key 1);
                    // }

                    // Add the combined element

                }

               else if (mp.find(key + 1) != mp.end() && mp[key + 1] >= 1) {
                    // Combine `key` and `key + 1` to form `key + key + 1`
                    mp[key]--;
                    mp[key + 1]--;
                    if (mp[key] == 0) {
                        mp.erase(key);
                    }
                    if (mp[key + 1] == 0) {
                        mp.erase(key + 1);
                    }

                    // Add the combined element
                    mp[key + key + 1]++;
                } else {
                    // No valid combination can be made
                    break;
                }

                // Update iterator after potential erasures
                it = mp.begin();
            }

            // After trying, check if `b[i]` can be satisfied
            it = mp.begin();
            if (mp.empty() || it->first != b[i]) {
                cout << "NO" << endl;
                return;
            }
        }
    }
    if(mp.size()!=0){
        cout<<"No"<<endl;return;
    }

    cout << "YES" << endl;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    aa(t); // Number of test cases
    while (t--) {
        mohammaad_rafi();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
#define int long long
#define fr(i,a,b) for(int i=a;i<b;i++)

int power(int a, int b) {
    int result = 1;
    while (b) {
        if (b & 1)
            result = result * a;
        a = a * a;
        b >>= 1;
    }
    return result;
}
int log_a_base_b(int a , int b){
    int cnt=0;
    while(a!=0){
        a/=b;
        cnt++;
    }
    return cnt;
}
void solve(){
    int n,m;
    cin >> n>>m ;
    multiset<int> s1;
    multiset<int> s2;
    fr(i,0,n){
        int x;
        cin>>x;
        s1.insert(x);
    }
    fr(i,0,m){
        int x;
        cin>>x;
        s2.insert(x);
    }
    // int f=1;
    while(!s1.empty() && !s2.empty()){
        auto it1=--s1.end(),it2=--s2.end();
        if(*it2>*it1){
            int x=floor((*it2)*1.0/2);
            int y=ceil((*it2)*1.0/2);
            s2.erase(it2);
            s2.insert(x);
            s2.insert(y);
            // cout<<x<<" "<<y;
        }
        else{
            if(*it2==*it1){
                s1.erase(it1);
                s2.erase(it2);
                // cout<<" = "  << *it1<<" = ";
            }
            else{
                // f=0;
                break;
            }
        }
        if(s2.size()>s1.size()){
            // f=0;
            break;
        }
    }
    // if(!f) cout<<"No"<<endl;
    // else{
        if(s1.empty() && s2.empty()){
            cout<<"Yes"<<endl;
        }
        else{
            cout<<"No"<<endl;
        }
    // }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif
    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
}

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
#define int long long
#define fr(i,a,b) for(int i=a;i<b;i++)

int power(int a, int b) {
    int result = 1;
    while (b) {
        if (b & 1)
            result = result * a;
        a = a * a;
        b >>= 1;
    }
    return result;
}
int log_a_base_b(int a , int b){
    int cnt=0;
    while(a!=0){
        a/=b;
        cnt++;
    }
    return cnt;
}
void solve(){
    int n,m;
    cin >> n>>m ;
    multiset<int> s1;
    multiset<int> s2;
    fr(i,0,n){
        int x;
        cin>>x;
        s1.insert(x);
    }
    fr(i,0,m){
        int x;
        cin>>x;
        s2.insert(x);
    }
    // int f=1;
    while(!s1.empty() && !s2.empty()){
        auto it1=--s1.end(),it2=--s2.end();
        if(*it2>*it1){
            s2.erase(it2);
            int x=floor((*it2)*1.0/2);
            int y=ceil((*it2)*1.0/2);
            s2.insert(x);
            s2.insert(y);
            // cout<<x<<" "<<y;
        }
        else{
            if(*it2==*it1){
                s1.erase(it1);
                s2.erase(it2);
                // cout<<" = "  << *it1<<" = ";
            }
            else{
                // f=0;
                break;
            }
        }
        if(s2.size()>s1.size()){
            // f=0;
            break;
        }
    }
    // if(!f) cout<<"No"<<endl;
    // else{
        if(s1.empty() && s2.empty()){
            cout<<"Yes"<<endl;
        }
        else{
            cout<<"No"<<endl;
        }
    // }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif
    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
}

Answer: 1


// author: лелутик :3
//#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <cassert>
#include <map>
#include <cmath>
#include <iomanip>
#include <random>
#include <functional>
#include <string>
#include <queue>
#include <stack>

using namespace std;
#define int               long long
#define pii               pair<int, int>
#define line              cout << "\n"
#define pb                push_back
#define ppb               pop_back
#define sz(a)             (int) a.size()
#define all(a)            a.begin(), a.end()
#define rall(a)           a.rbegin(), a.rend()
#define fast              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef long double ld;
typedef string str;
mt19937 rnd(time(0));
const int MOD = 998244353;

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    multiset<int> st1, st2;
    int s1 = 0, s2 = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        st1.insert(a[i]);
        s1 += a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        st2.insert(b[i]);
        s2 += b[i];
    }
    if (s1 != s2) {
        cout << "No"; line;
        return;
    }
    while (sz(st1) && sz(st2) && sz(st2) <= sz(st1)) {
        int x = *(--st2.end());
        if (x < *st1.begin()) {
            cout << "No"; line;
            return;
        }
        st2.erase(--st2.end());
        if (st1.find(x) != st1.end()) {
            st1.erase(st1.find(x));
            continue;
        }
        st2.insert(x / 2);
        st2.insert((x + 1) / 2);
    }
    if (sz(st1) || sz(st2)) {
        cout << "No"; line;
        return;
    }
    cout << "Yes"; line;
}

signed main() {
    fast;

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

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

// author: лелутик :3
//#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <cassert>
#include <map>
#include <cmath>
#include <iomanip>
#include <random>
#include <functional>
#include <string>
#include <queue>
#include <stack>

using namespace std;
#define int               long long
#define pii               pair<int, int>
#define line              cout << "\n"
#define pb                push_back
#define ppb               pop_back
#define sz(a)             (int) a.size()
#define all(a)            a.begin(), a.end()
#define rall(a)           a.rbegin(), a.rend()
#define fast              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef long double ld;
typedef string str;
mt19937 rnd(time(0));
const int MOD = 998244353;

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    multiset<int> st;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        st.insert(a[i]);
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }
    sort(all(b));
    int i = 0;
    while (sz(st) && i < sz(b)) {
        int x = *st.begin();
        st.erase(st.begin());
        if (x == b[i]) {
            i++;
            continue;
        }
        if (!sz(st)) {
            cout << "No"; line;
            return;
        }
        int y = *st.begin();
        st.erase(st.begin());
        if (y - x > 1) {
            cout << "No"; line;
            return;
        }
        st.insert(x + y);
    }
    if (sz(st) || i != sz(b)) {
        cout << "No"; line;
        return;
    }
    cout << "Yes"; line;
}

signed main() {
    fast;

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

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

Answer: 1