#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1e9+7;

class TrieNode {
public:
    bool isEnd;
    TrieNode* nxt[26];
    TrieNode(bool b = false) {
        isEnd = b;
        memset(nxt, 0, sizeof(nxt));
    }
};

class Trie {
    TrieNode* root;
public:
    Trie() {
        root = new TrieNode();
    }
    void insert(string s) {
        TrieNode* cur = root;
        for (char c : s) {
            if (cur->nxt[c - 'a'] == NULL) {
                cur->nxt[c - 'a'] = new TrieNode();
            }
            cur = cur->nxt[c - 'a'];
        }
        cur->isEnd = true;
    }
    bool search(string s) {
        TrieNode* cur = root;
        for (char c : s) {
            if (!(cur->nxt[c - 'a'])) {
                return false;
            }
            cur = cur->nxt[c - 'a'];
        }
        return cur->isEnd;
    }
    ll countCombs(string s, ll k, ll sz, unordered_map<ll, ll>& dp) {
        if (k == sz) {
            return 1;
        }
        if (dp.find(k) != dp.end()) {
            return dp[k];
        }
        TrieNode* cur = root;
        ll res = 0;
        for (ll i = k; i < sz; i++) {
            if (!(cur->nxt[s[i] - 'a'])) {
                return res;
            }
            cur = cur->nxt[s[i] - 'a'];
            if (cur->isEnd) {
                res = (res + countCombs(s, i + 1, sz, dp)) % mod;
            }
        }
        return dp[k] = res;
    }
};

vector<int>giveLps(string s){
  int n=s.length();
    vector<int>lps(n,0);
    int len=0;
    for(int i=1;i<n;){
        if(s[i]==s[len]){
            len++;
            lps[i]=len;
            i++;
        }else{
            if(len!=0){
                len = lps[len-1];
            }else{
                lps[i]=0;
                i++;
            }
        }
    }
    return lps;
}
void prindq(deque<int>dq){
    while(dq.size()){
        cout<<dq.front()<<" ";
        dq.pop_front();
    }
    cout<<endl;
}
void prinstk(stack<int>st){
    while(st.size()){
        cout<<st.top()<<" ";
        st.pop();
    }
    cout<<endl;
}
void prinvec(vector<int>&vec){
    for(int u:vec){
        cout<<u<<" ";
    }
    cout<<endl;
}
void merge(vector<int>&curr2 , vector<int>&curr){
    vector<int>res;
    int n=curr2.size(),m=curr.size(),i=0,j=0;
    while(i<n && j<m){
        if(curr2[i]<curr[j]){
            res.push_back(curr2[i]);
            i++;
        }else{
            res.push_back(curr[j++]);
        }
    }
    while(i<n){
        res.push_back(curr2[i]);
        i++;
    }
    while(j<m){
        res.push_back(curr[j++]);
    }
    curr2=res;
}
using ppi = pair<int,int>;
void solver(){
    int n,m;
    cin>>n>>m;
    vector<ll>a,b;
    ll sma=0,smb=0;
    for(int i=0;i<n;i++){
        ll x;
        cin>>x;
        a.push_back(x);
        sma+=x;
    }
    priority_queue<int>pq;
    for(int i=0;i<m;i++){
        ll x;
        cin>>x;
        pq.push(x);
        smb+=x;
    }
    if(sma!=smb){
        cout<<"NO"<<endl;
        return;
    }else{
        sort(a.begin(),a.end());
        int i=n-1;
        while(pq.size() && i>=0){
            int curr = pq.top();
            //cout<<curr<<" "<<a[i]<<endl;
            pq.pop();
            if(curr==a[i]){
                i--;
            }else if(curr>a[i] ){
                 pq.push(curr/2);
                 pq.push(curr/2+curr%2);
            }else{
                cout<<"NO"<<endl;
                return;
            }
        }
        if(i<0 & pq.size()==0){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
    }

}

int main() {
  //freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
   int t;
   cin>>t;
   //t=1;
   for(int i=0;i<t;i++){
     solver();
   }
}

#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1e9+7;

class TrieNode {
public:
    bool isEnd;
    TrieNode* nxt[26];
    TrieNode(bool b = false) {
        isEnd = b;
        memset(nxt, 0, sizeof(nxt));
    }
};

class Trie {
    TrieNode* root;
public:
    Trie() {
        root = new TrieNode();
    }
    void insert(string s) {
        TrieNode* cur = root;
        for (char c : s) {
            if (cur->nxt[c - 'a'] == NULL) {
                cur->nxt[c - 'a'] = new TrieNode();
            }
            cur = cur->nxt[c - 'a'];
        }
        cur->isEnd = true;
    }
    bool search(string s) {
        TrieNode* cur = root;
        for (char c : s) {
            if (!(cur->nxt[c - 'a'])) {
                return false;
            }
            cur = cur->nxt[c - 'a'];
        }
        return cur->isEnd;
    }
    ll countCombs(string s, ll k, ll sz, unordered_map<ll, ll>& dp) {
        if (k == sz) {
            return 1;
        }
        if (dp.find(k) != dp.end()) {
            return dp[k];
        }
        TrieNode* cur = root;
        ll res = 0;
        for (ll i = k; i < sz; i++) {
            if (!(cur->nxt[s[i] - 'a'])) {
                return res;
            }
            cur = cur->nxt[s[i] - 'a'];
            if (cur->isEnd) {
                res = (res + countCombs(s, i + 1, sz, dp)) % mod;
            }
        }
        return dp[k] = res;
    }
};

vector<int>giveLps(string s){
  int n=s.length();
    vector<int>lps(n,0);
    int len=0;
    for(int i=1;i<n;){
        if(s[i]==s[len]){
            len++;
            lps[i]=len;
            i++;
        }else{
            if(len!=0){
                len = lps[len-1];
            }else{
                lps[i]=0;
                i++;
            }
        }
    }
    return lps;
}
void prindq(deque<int>dq){
    while(dq.size()){
        cout<<dq.front()<<" ";
        dq.pop_front();
    }
    cout<<endl;
}
void prinstk(stack<int>st){
    while(st.size()){
        cout<<st.top()<<" ";
        st.pop();
    }
    cout<<endl;
}
void prinvec(vector<int>&vec){
    for(int u:vec){
        cout<<u<<" ";
    }
    cout<<endl;
}
void merge(vector<int>&curr2 , vector<int>&curr){
    vector<int>res;
    int n=curr2.size(),m=curr.size(),i=0,j=0;
    while(i<n && j<m){
        if(curr2[i]<curr[j]){
            res.push_back(curr2[i]);
            i++;
        }else{
            res.push_back(curr[j++]);
        }
    }
    while(i<n){
        res.push_back(curr2[i]);
        i++;
    }
    while(j<m){
        res.push_back(curr[j++]);
    }
    curr2=res;
}
using ppi = pair<int,int>;
void solver(){
    int n,m;
    cin>>n>>m;
    vector<ll>a,b;
    ll sma=0,smb=0;
    for(int i=0;i<n;i++){
        ll x;
        cin>>x;
        a.push_back(x);
        sma+=x;
    }
    priority_queue<int>pq;
    for(int i=0;i<m;i++){
        ll x;
        cin>>x;
        pq.push(x);
        smb+=x;
    }
    if(sma!=smb){
        cout<<"NO"<<endl;
        return;
    }else{
        sort(a.begin(),a.end());
        int i=n-1;
        while(pq.size() && i>=0){
            int curr = pq.top();
            //cout<<curr<<" "<<a[i]<<endl;
            pq.pop();
            if(curr==a[i]){
                i--;
            }else if(curr>a[i] && abs(curr-2*a[i])<=1){
                 pq.push(curr-a[i--]);
            }else{
                cout<<"NO"<<endl;
                return;
            }
        }
        if(i<0 & pq.size()==0){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
    }

}

int main() {
  freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
   int t;
   cin>>t;
   //t=1;
   for(int i=0;i<t;i++){
     solver();
   }
}

Answer: 1


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

/*
 *   Author : RTE_ELMENATOR
 *
 *   "No one will carry you.
 *    Work harder, and turn your dream into reality."
 *   "
 *      If you're stuck in a problem while solving
 *      Then Think about the Answer (how related to input)
 *   "
 *   " Tree Questions : -> dp on tree
 *    dfs calcualtes the subtree distance
 *    dfs 2 calculates the parent answer and sub tree ans combine we can give
 *     ( Path of max distance can't consider max 2 can consider "
 */

public class CpWala {
	private static void solverA() {
		int size[] = Reading.readIntArray();
		int n  = size[ 0  ] , m  = size[ 1 ];
	   int[] arr = Reading.readIntArray();
	   int[] barr = Reading.readIntArray();
	   HashMap<Integer , Integer> map = new HashMap<>();
	   for(int i : arr ) {
		   map.put( i , map.getOrDefault( i , 0) + 1);
	   }
	   int countx = n ;
	   for(int i = 0 ; i < m ;i ++ ) {

		   if( map.getOrDefault( barr[ i ], 0 ) > 0 ) {
			   map.put( barr[ i] , map.get( barr[ i ]) - 1 );
			   barr[ i ] = 0 ;
		       countx --;
		   }
	   }
	   int inxa = 0;
	   arr = new int[ countx ];
	   for(int i : map.keySet() ) {
		   int v = map.get( i ) ;
		  while( v -- > 0 ) {
			  arr[ inxa ++ ] = i;
		  }
	   }
	   Arrays.sort( arr );
	   n = countx  ;
	   int l = 0 , lb = 0 ;

	   for(; lb < m ; ++ lb   ) {
		   if( barr[ lb ] ==0 ) continue;
		   if( l == n ) {
			   out.println("NO"); return;
		   }
		   int reqX = barr[ lb ];
		   if( arr[ l ] == barr[ lb ]) {
			   continue;
		   }
		   int req = arr[  l ];
		   l ++ ;
		   while( l < n && req < barr[ lb ] && Math.abs( req - arr[ l ]) <= 1) {
			   req += arr[ l ];
			   l ++;
		   }
		   if( req == barr[ lb ] ) continue;
		   out.println("NO");
		   return;
	   }
	   if( l == n ) {
		   out.println("YES");
	   }
	   else out.println("NO");
	}
	private static void dfs( int curNode , int parent , ArrayList<Long> prefix
		,	ArrayList<Long> prefix_sum , ArrayList<TreeNode>[] tree , int[] res ) {
	         if( tree[ curNode ] == null ) return;
		for(var node : tree [ curNode ]) {
			 if( parent == node.next ) continue;
			long ai = node.weight;
			long bi = node.weight1 ;
			int next = node.next;

			prefix.add( prefix.get( prefix.size()  - 1) + ai ) ;

			prefix_sum.add( prefix_sum.get( prefix_sum.size() - 1) + bi );
		    res[ next ] = find(next  , prefix.get( prefix.size() - 1)  , prefix_sum ) ;
			dfs( next ,  curNode , prefix , prefix_sum , tree , res );
			prefix.remove( prefix.size() -1  );
			prefix_sum.remove( prefix_sum.size() - 1);

		}
	}
  private static int find( int x , long value ,  ArrayList<Long> search  ) {
	  int v = Collections.binarySearch(search  , value );
	  //out.println( search +" "+value+" "+x +" "+v );
	  if( v < 0 ) return -(v + 2);
	  return v;
  }
  static PrintWriter out = new PrintWriter(System.out);

  public static void main(String[] arg) {
    int testNo =   Reading.readInt();
    while (testNo-- > 0) {
      solverA();
    }
    out.flush();
    out.close();
  }

  private static long getLowerPower(long rem, int k) {
    // k^x <= n
    long res = 1;
    while (res <= rem) {
      res = res * k;
    }
    res /= k;
    return res;
  }

  private static long findMinoOpern(int n, int k) {
    if (k == 1) return n;
    long res = getLowerPower(n, k);
    int count = 0;
    long rem = n;
    while (rem >= k) {
      long powcan = (rem / res);
      rem = (rem % res);
      count += powcan;
      res = getLowerPower(rem, k);
    }
    return rem % k + count;
  }

  private static int[] kmp(String string) {
    int[] res = new int[string.length()];
    int j = 0;
    for (int indx = 1; indx < string.length();) {
      if (string.charAt(indx) == string.charAt(j)) {
        if (j == 0) {
          res[indx] = 1;
        } else
          res[indx] = j + 1;
        j++;
        indx++;
      } else {
        if (j == 0) {
          indx++;
          continue;
        }
        j = res[j - 1];
      }
    }
    return res;
  }
}
class Union {
	int[] parent ;
	int[] rank ;
	int n ;
	int[] size ;
	public Union ( int size  ) {
		n  = size + 1;
		this.size = new int[ size + 1 ];
		parent = new int [ size + 1];
	   rank = new int[ size + 1];
	   Arrays.fill( this.size ,1 );
	   Arrays.fill( parent , -1 );
	   Arrays.fill( rank , -1 );
	}
	public void union(int u , int v ) {
		int parent_u = find( u ) ;
		int parent_v = find( v );
		if( parent_u != parent_v ) {
			if( parent_v > parent_u) {
			parent[ parent_u ] = parent_v  ;
			size[ parent_u ] += size[ parent_v ];
			}
			else {
				parent[ parent_v ] = parent_u;
				size[ parent_u ]+=size[ parent_u];
			}
		}
	}
	public int find( int node ) {
		if( parent[ node ] == -1 ) return node ;
		return parent[ node ] = find( parent[ node ]);
	}

}
class TreeNode {
	int  next  ;
	long weight , weight1 ;
	public TreeNode ( int node , int weight , int weight1 ) {
		this.next = node ;
		this.weight = weight;
		this.weight1 = weight1 ;
	}
	public String toString() {
		return "["+next+" , " + weight + " , "+weight1+" ] ";
	}
}
class Reading {
  private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

  public Reading() {
    reader = new BufferedReader(new InputStreamReader(System.in));
  }

  public static int readInt() {
    try {
      return Integer.parseInt(reader.readLine().trim());
    } catch (Exception e) {
      return -1;
    }
  }

  public static int[] readIntArray() {
    try {
      return Arrays.stream(reader.readLine().trim().split("\\s+"))
                   .mapToInt(Integer::parseInt)
                   .toArray();
    } catch (Exception e) {
      return null;
    }
  }

  public static long[] readLongArray() {
    try {
      return Arrays.stream(reader.readLine().trim().split("\\s+"))
                   .mapToLong(Long::parseLong)
                   .toArray();
    } catch (Exception e) {
      return null;
    }
  }

  public static String readString() {
    try {
      return reader.readLine();
    } catch (Exception e) {
      return null;
    }
  }

  public static char[] readToCharArray() {
    try {
      return reader.readLine().replaceAll("\\s+", "").toCharArray();
    } catch (Exception e) {
      return null;
    }
  }
}

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

/*
 *   Author : RTE_ELMENATOR
 *
 *   "No one will carry you.
 *    Work harder, and turn your dream into reality."
 *   "
 *      If you're stuck in a problem while solving
 *      Then Think about the Answer (how related to input)
 *   "
 *   " Tree Questions : -> dp on tree
 *    dfs calcualtes the subtree distance
 *    dfs 2 calculates the parent answer and sub tree ans combine we can give
 *     ( Path of max distance can't consider max 2 can consider "
 */

public class CpWala {
	private static void solverA() {
		int size[] = Reading.readIntArray();
		int n  = size[ 0  ] , m  = size[ 1 ];
	   int[] arr = Reading.readIntArray();
	   int[] barr = Reading.readIntArray();
	   HashMap<Integer , Integer> map = new HashMap<>();
	   for(int i : arr ) {
		   map.put( i , map.getOrDefault( i , 0) + 1);
	   }

	   int l = 0 , lb = 0 ;

	   for(; lb < m ; ++ lb   ) {
		   if( barr[ lb ] ==0 ) continue;
		   int need = barr[ lb ] ;

		   boolean isokay = find( need , map );
	      if(!isokay) {out.println("NO"); return;}
	   }
	   if(map.isEmpty()) out.println("YES");
	   else out.println("NO");
	}
	private static boolean find( int val ,HashMap<Integer , Integer> map ) {

		if( map.containsKey( val )) {
			map.put( val , map.get( val ) - 1);
		  if( map.get( val ) == 0 ) map.remove( val );
			return true;
		}
		if( val == 1) return false;
		return find( val /2 , map ) && find( val/2 + (val%2 )  , map );
	}
	private static void dfs( int curNode , int parent , ArrayList<Long> prefix
		,	ArrayList<Long> prefix_sum , ArrayList<TreeNode>[] tree , int[] res ) {
	         if( tree[ curNode ] == null ) return;
		for(var node : tree [ curNode ]) {
			 if( parent == node.next ) continue;
			long ai = node.weight;
			long bi = node.weight1 ;
			int next = node.next;

			prefix.add( prefix.get( prefix.size()  - 1) + ai ) ;

			prefix_sum.add( prefix_sum.get( prefix_sum.size() - 1) + bi );
		    res[ next ] = find(next  , prefix.get( prefix.size() - 1)  , prefix_sum ) ;
			dfs( next ,  curNode , prefix , prefix_sum , tree , res );
			prefix.remove( prefix.size() -1  );
			prefix_sum.remove( prefix_sum.size() - 1);

		}
	}
  private static int find( int x , long value ,  ArrayList<Long> search  ) {
	  int v = Collections.binarySearch(search  , value );
	  //out.println( search +" "+value+" "+x +" "+v );
	  if( v < 0 ) return -(v + 2);
	  return v;
  }
  static PrintWriter out = new PrintWriter(System.out);

  public static void main(String[] arg) {
    int testNo =   Reading.readInt();
    while (testNo-- > 0) {
      solverA();
    }
    out.flush();
    out.close();
  }

  private static long getLowerPower(long rem, int k) {
    // k^x <= n
    long res = 1;
    while (res <= rem) {
      res = res * k;
    }
    res /= k;
    return res;
  }

  private static long findMinoOpern(int n, int k) {
    if (k == 1) return n;
    long res = getLowerPower(n, k);
    int count = 0;
    long rem = n;
    while (rem >= k) {
      long powcan = (rem / res);
      rem = (rem % res);
      count += powcan;
      res = getLowerPower(rem, k);
    }
    return rem % k + count;
  }

  private static int[] kmp(String string) {
    int[] res = new int[string.length()];
    int j = 0;
    for (int indx = 1; indx < string.length();) {
      if (string.charAt(indx) == string.charAt(j)) {
        if (j == 0) {
          res[indx] = 1;
        } else
          res[indx] = j + 1;
        j++;
        indx++;
      } else {
        if (j == 0) {
          indx++;
          continue;
        }
        j = res[j - 1];
      }
    }
    return res;
  }
}
class Union {
	int[] parent ;
	int[] rank ;
	int n ;
	int[] size ;
	public Union ( int size  ) {
		n  = size + 1;
		this.size = new int[ size + 1 ];
		parent = new int [ size + 1];
	   rank = new int[ size + 1];
	   Arrays.fill( this.size ,1 );
	   Arrays.fill( parent , -1 );
	   Arrays.fill( rank , -1 );
	}
	public void union(int u , int v ) {
		int parent_u = find( u ) ;
		int parent_v = find( v );
		if( parent_u != parent_v ) {
			if( parent_v > parent_u) {
			parent[ parent_u ] = parent_v  ;
			size[ parent_u ] += size[ parent_v ];
			}
			else {
				parent[ parent_v ] = parent_u;
				size[ parent_u ]+=size[ parent_u];
			}
		}
	}
	public int find( int node ) {
		if( parent[ node ] == -1 ) return node ;
		return parent[ node ] = find( parent[ node ]);
	}

}
class TreeNode {
	int  next  ;
	long weight , weight1 ;
	public TreeNode ( int node , int weight , int weight1 ) {
		this.next = node ;
		this.weight = weight;
		this.weight1 = weight1 ;
	}
	public String toString() {
		return "["+next+" , " + weight + " , "+weight1+" ] ";
	}
}
class Reading {
  private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

  public Reading() {
    reader = new BufferedReader(new InputStreamReader(System.in));
  }

  public static int readInt() {
    try {
      return Integer.parseInt(reader.readLine().trim());
    } catch (Exception e) {
      return -1;
    }
  }

  public static int[] readIntArray() {
    try {
      return Arrays.stream(reader.readLine().trim().split("\\s+"))
                   .mapToInt(Integer::parseInt)
                   .toArray();
    } catch (Exception e) {
      return null;
    }
  }

  public static long[] readLongArray() {
    try {
      return Arrays.stream(reader.readLine().trim().split("\\s+"))
                   .mapToLong(Long::parseLong)
                   .toArray();
    } catch (Exception e) {
      return null;
    }
  }

  public static String readString() {
    try {
      return reader.readLine();
    } catch (Exception e) {
      return null;
    }
  }

  public static char[] readToCharArray() {
    try {
      return reader.readLine().replaceAll("\\s+", "").toCharArray();
    } catch (Exception e) {
      return null;
    }
  }
}

Answer: 2


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#ifndef ONLINE_JUDGE
#include "debugg.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//change less<int> to less_equal<int> to make it a multiorderedset
//to erase from multiorderedset do s.erase(s.upperbound(a[i]))
//find_by_order(k)->iterator of the kth element
//order_of_key(k)->number of elements smaller than k
//ncr snip has modinv as well
#define float double
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl '\n'
#define p(x) cout<<x<<endl
#define c(x) cout<<x<<" "
#define int long long
#define pb push_back
#define scanarr int a[n];for(int i=0;i<n;i++)cin>>a[i]
#define scannarr(a) int a[(int)(a)];for(int i=0;i<(int)(a);i++)cin>>a[i]
#define fr(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define frr(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int,int>
#define vpi vector<pair<int,int>>
#define frr(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define mod 1000000007

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

void solve() {
    int n,m;
    cin>>n>>m;
    vi a(n),b(m);
    fr(i,0,n)cin>>a[i];
    fr(i,0,m)cin>>b[i];
    map<int,int>m1,m2;
    set<int>s,s1;
    fr(i,0,n){
        m1[a[i]]++;
        s1.insert(a[i]);
    }
    fr(i,0,m){
        m2[b[i]]++;
        s.insert(b[i]);
    }
    set<int>del;
    for(auto x:s){
        int k=max((int)0,m1[x]-m2[x]);
        int g=m1[x]-k;
        m2[x]-=g;
        if(m2[x]==0)del.insert(x);
        m1[x]=k;
        if(k==0)s1.erase(x);
    }

    for(auto x:del)s.erase(x);
   // function <void(int,int)> dfs = [&] (int r,int c) -> void
   int check=0;
    priority_queue<int>q;
    function<void(int,int)> solve = [&] (int x,int f) -> void{
        if(x==0)return;
        if(x==1){
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            if(m1[x]==0)s1.erase(x);
            if(f!=0){
                check=1;
            }
            return;
        }
        if(x%2==0){
            m2[x]=0;
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            if(m1[x]==0)s1.erase(x);
            if(f==0)return;
            m2[x/2]+=f*2;
            if(s.find(x/2)==s.end())q.push(x/2);
        //    solve(x/2,f*2);

        }
        else{
            m2[x]=0;
            s.erase(x);
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            if(m1[x]==0)s1.erase(x);
            if(f==0)return;
            if(s.find(x/2)==s.end()){q.push(x/2);s.insert(x/2);}
            if(s.find((x/2)+1)==s.end()){q.push((x/2)+1);s.insert((x/2)+1);}
            // c(x);p(m1[x]);
            m2[x/2]+=f;m2[(x/2)+1]+=f;
            // solve(x/2,f);solve((x/2)+1,f);
        }
    };

    for(auto x:s){
       q.push(x);
    }
    while(q.size()){
        int x=q.top();
        q.pop();
        solve(x,m2[x]);
        if(check==1){p("No");return;}
    }

    if(s1.size()==0)p("Yes");
    else p("No");

}
signed main() {
    fast;
    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;
#ifndef ONLINE_JUDGE
#include "debugg.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//change less<int> to less_equal<int> to make it a multiorderedset
//to erase from multiorderedset do s.erase(s.upperbound(a[i]))
//find_by_order(k)->iterator of the kth element
//order_of_key(k)->number of elements smaller than k
//ncr snip has modinv as well
#define float double
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl '\n'
#define p(x) cout<<x<<endl
#define c(x) cout<<x<<" "
#define int long long
#define pb push_back
#define scanarr int a[n];for(int i=0;i<n;i++)cin>>a[i]
#define scannarr(a) int a[(int)(a)];for(int i=0;i<(int)(a);i++)cin>>a[i]
#define fr(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define frr(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int,int>
#define vpi vector<pair<int,int>>
#define frr(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define mod 1000000007

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

void solve() {
    int n,m;
    cin>>n>>m;
    vi a(n),b(m);
    fr(i,0,n)cin>>a[i];
    fr(i,0,m)cin>>b[i];
    map<int,int>m1,m2;
    set<int>s,s1;
    fr(i,0,n){
        m1[a[i]]++;
        s1.insert(a[i]);
    }
    fr(i,0,m){
        m2[b[i]]++;
        s.insert(b[i]);
    }
    set<int>del;
    for(auto x:s){
        int k=max((int)0,m1[x]-m2[x]);
        int g=m1[x]-k;
        m2[x]-=g;
        if(m2[x]==0)del.insert(x);
        m1[x]=k;
        if(k==0)s1.erase(x);
    }

    for(auto x:del)s.erase(x);
   // function <void(int,int)> dfs = [&] (int r,int c) -> void
   int check=0;
//    p("hi");
    function<void(int,int)> solve = [&] (int x,int f) -> void{
        if(x==0)return;
        if(x==1){
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            if(m1[x]==0)s1.erase(x);
            if(f!=0){
                check=1;
            }
            return;
        }
        if(x%2==0){
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            // c(x);p(m1[x]);
            if(m1[x]==0)s1.erase(x);
            if(f==0)return;
           solve(x/2,f*2);

        }
        else{
            if(m1[x]!=0){
                if(m1[x]>=f){
                    m1[x]-=f;f=0;
                }
                else{
                    f-=m1[x];m1[x]=0;
                }
            }
            //  c(x);p(m1[x]);
            if(m1[x]==0)s1.erase(x);
            if(f==0)return;
            solve(x/2,f);solve((x/2)+1,f);
        }
    };

    for(auto x:s){
        solve(x,m2[x]);
        // c("check ");c(x);p(check);
        if(check==1){p("No");return;}
    }
    // solve(3,m2[3]);
    // p((*s1.begin()));
    if(s1.size()==0)p("Yes");
    else p("No");

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

Answer: 1


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,a[N],b[N];
map<int,int> mp;
void process() {
    cin>>n>>m;
    mp.clear();
    for (int i=1; i<=n; ++i) { cin>>a[i]; mp[a[i]]++; }
    priority_queue<int> q;
    for (int i=1; i<=m; ++i) { cin>>b[i]; q.push(b[i]); }
    while (m<n && !q.empty()) {
        int u=q.top();
        if (u==1) break;
        q.pop();
        if (mp[u]) mp[u]--;
        else { q.push(u/2); q.push((u+1)/2); ++m; }
    }
    while (!q.empty()) {
        int u=q.top(); q.pop();
        if (mp[u]) mp[u]--;
    }
    for (int i=1; i<=n; ++i) if (mp[a[i]]!=0) { cout<<"No\n"; return; }
    cout<<"Yes\n";
}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin>>t;
    while (t--) {
        process();
    }
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,a[N],b[N];
map<int,int> mp;
void process() {
    cin>>n>>m;
    mp.clear();
    for (int i=1; i<=n; ++i) { cin>>a[i]; mp[a[i]]++; }
    priority_queue<int> q;
    for (int i=1; i<=m; ++i) { cin>>b[i]; q.push(b[i]); }
    while (m<n) {
        int u=q.top();
        if (u==1) break;
        q.pop();
        if (mp[u]) mp[u]--;
        else { q.push(u/2); q.push((u+1)/2); ++m; }
    }
    while (!q.empty()) {
        int u=q.top(); q.pop();
        if (mp[u]) mp[u]--;
    }
    for (int i=1; i<=n; ++i) if (mp[a[i]]!=0) { cout<<"No\n"; return; }
    cout<<"Yes\n";
}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin>>t;
    while (t--) {
        process();
    }
}

Answer: 1


Answer: 1


#include<iostream>
#include<vector>
#include<algorithm>
void solve(){
	int n,m;
	std::cin>>n>>m;
	std::vector<int> a(n),b(m);
	for(int i=0;i<n;i++){
		int x;
		std::cin>>x;
		a.push_back(x);
	}
	std::sort(a.rbegin(),a.rend());       //sort降序排列，从大到小
	for(int i=0;i<m;i++){
		int x;
		std::cin>>x;
		b.push_back(x);
	}
	std::sort(b.rbegin(),b.rend());       //sort降序排列，从大到小
	long long sum=n-m;
	while(sum>=0&&a.size()&&b.size()){
		if(a.front()==b.front()){
			a.pop_back();          //最大值相同 匹配则弹出，继续匹配
			b.pop_back();
		}else if(sum>0){        //需要操作
			int t=b.front();
			b.pop_back();
			b.push_back(t/2);
			b.push_back(t-t/2);
			;
		}else{
			break;
		}
	}
	if(a.empty()&&b.empty()){      //都匹配且弹出
		std::cout<<"YES\n";
	}else{
		std::cout<<"NO\n";
	}
}
int main(){
	int t;
	std::cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

#include<iostream>
#include<vector>
#include<queue>
void solve() {
    int n, m;
    std::cin >> n >> m;
    std::priority_queue<int> a, b;
    for (int i = 0; i < n; ++ i) {
    	int x;
    	std::cin >> x;
        a.push(x);
    }

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

    long long sum = n - m;

    while (sum >= 0 && a.size() && b.size()) {
    	if (a.top() == b.top()) {
            a.pop();
            b.pop();
        } else if (sum > 0) {
            int u = b.top(); b.pop();
            b.push(u / 2);
            b.push(u - u / 2);
            -- sum;
        } else {
            break;
        }
    }

    if (a.empty() && b.empty()) {
    	std::cout << "YES\n";
    } else {
    	std::cout << "NO\n";
    }
}
int main(){
	int t;
	std::cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

Answer: 2


#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1e9+7;

class TrieNode {
public:
    bool isEnd;
    TrieNode* nxt[26];
    TrieNode(bool b = false) {
        isEnd = b;
        memset(nxt, 0, sizeof(nxt));
    }
};

class Trie {
    TrieNode* root;
public:
    Trie() {
        root = new TrieNode();
    }
    void insert(string s) {
        TrieNode* cur = root;
        for (char c : s) {
            if (cur->nxt[c - 'a'] == NULL) {
                cur->nxt[c - 'a'] = new TrieNode();
            }
            cur = cur->nxt[c - 'a'];
        }
        cur->isEnd = true;
    }
    bool search(string s) {
        TrieNode* cur = root;
        for (char c : s) {
            if (!(cur->nxt[c - 'a'])) {
                return false;
            }
            cur = cur->nxt[c - 'a'];
        }
        return cur->isEnd;
    }
    ll countCombs(string s, ll k, ll sz, unordered_map<ll, ll>& dp) {
        if (k == sz) {
            return 1;
        }
        if (dp.find(k) != dp.end()) {
            return dp[k];
        }
        TrieNode* cur = root;
        ll res = 0;
        for (ll i = k; i < sz; i++) {
            if (!(cur->nxt[s[i] - 'a'])) {
                return res;
            }
            cur = cur->nxt[s[i] - 'a'];
            if (cur->isEnd) {
                res = (res + countCombs(s, i + 1, sz, dp)) % mod;
            }
        }
        return dp[k] = res;
    }
};

vector<int>giveLps(string s){
  int n=s.length();
    vector<int>lps(n,0);
    int len=0;
    for(int i=1;i<n;){
        if(s[i]==s[len]){
            len++;
            lps[i]=len;
            i++;
        }else{
            if(len!=0){
                len = lps[len-1];
            }else{
                lps[i]=0;
                i++;
            }
        }
    }
    return lps;
}
void prindq(deque<int>dq){
    while(dq.size()){
        cout<<dq.front()<<" ";
        dq.pop_front();
    }
    cout<<endl;
}
void prinstk(stack<int>st){
    while(st.size()){
        cout<<st.top()<<" ";
        st.pop();
    }
    cout<<endl;
}
void prinvec(vector<int>&vec){
    for(int u:vec){
        cout<<u<<" ";
    }
    cout<<endl;
}
void merge(vector<int>&curr2 , vector<int>&curr){
    vector<int>res;
    int n=curr2.size(),m=curr.size(),i=0,j=0;
    while(i<n && j<m){
        if(curr2[i]<curr[j]){
            res.push_back(curr2[i]);
            i++;
        }else{
            res.push_back(curr[j++]);
        }
    }
    while(i<n){
        res.push_back(curr2[i]);
        i++;
    }
    while(j<m){
        res.push_back(curr[j++]);
    }
    curr2=res;
}
using ppi = pair<int,int>;
void solver(){
    int n,m;
    cin>>n>>m;
    vector<ll>a,b;
    ll sma=0,smb=0;
    for(int i=0;i<n;i++){
        ll x;
        cin>>x;
        a.push_back(x);
        sma+=x;
    }
    priority_queue<int>pq;
    for(int i=0;i<m;i++){
        ll x;
        cin>>x;
        pq.push(x);
        smb+=x;
    }
    if(sma!=smb){
        cout<<"NO"<<endl;
        return;
    }else{
        sort(a.begin(),a.end());
        int i=n-1;
        while(pq.size() && i>=0){
            int curr = pq.top();
            //cout<<curr<<" "<<a[i]<<endl;
            pq.pop();
            if(curr==a[i]){
                i--;
            }else if(curr>a[i] && abs(curr-2*a[i])<=1){
                 pq.push(curr-a[i--]);
            }else{
                cout<<"NO"<<endl;
                return;
            }
        }
        if(i<0 & pq.size()==0){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
    }

}

int main() {
  //freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
   int t;
   cin>>t;
   //t=1;
   for(int i=0;i<t;i++){
     solver();
   }
}

Answer: 1


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

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll,ll> ull;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define REPR(i, a, b) for (int i = (a); i > (b); --i)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())
#define REVERSE(v) reverse(all(v))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define input(a, n) for(int i = 0; i < n; i++) cin >> a[i]

const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LLINF = 1e18;
const double PI = acos(-1.0);
const double EPS = 1e-9;

#define DEBUG
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define dbg(x) cerr << #x << " = " << x << endl
#endif

template <typename T>
void print(T t) { cout << t << "\n"; }

template <typename T, typename... Args>
void print(T t, Args... args) {
    cout << t << " ";
    print(args...);
}

template <typename T>
void printVector(const vector<T>& v) {
    for (const T& x : v) cout << x << " ";
    cout << "\n";
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll mod_add(ll a, ll b, ll m) { return (a % m + b % m) % m; }
ll mod_sub(ll a, ll b, ll m) { return (a % m - b % m + m) % m; }
ll mod_mul(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }
ll mod_pow(ll b, ll p, ll m) {
    ll r = 1;
    while (p) {
        if (p & 1) r = mod_mul(r, b, m);
        b = mod_mul(b, b, m);
        p >>= 1;
    }
    return r;
}

void solve() {
    ll n,m;
    cin>>n>>m;
    vll a(n,0),b(m,0);
    unordered_map<ll,ll>mpp1,mpp2;
    REP(i,0,n)
    {
        cin>>a[i];
        mpp1[a[i]]++;
    }
    REP(j,0,m)
    {
        cin>>b[j];
        mpp2[b[j]]++;
    }
    multiset<ll>s1,s2;
    for(int i=0;i<m;i++)
    {
        ll num=b[i];
        if((mpp1.find(num)!=mpp1.end()) && (mpp2.find(num)!=mpp2.end()))
        {
            mpp1[num]--;
            mpp2[num]--;
            if(mpp1[num]==0) mpp1.erase(num);
            if(mpp2[num]==0) mpp2.erase(num);
        }
        else
        {
            s2.insert(b[i]);
        }
    }
    for(int i=0;i<n;i++)
    {
        if(!s2.count(a[i]))
        {
            if(mpp1[a[i]]>0)
            {
                mpp1[a[i]]--;
                if(mpp1[a[i]]==0) mpp1.erase(a[i]);
                s1.insert(a[i]);
            }
        }
    }
    // cout<<s1.size()<<" "<<s2.size()<<"\n";
    vll v1(all(s1)),v2(all(s2));
    int i=0,j=0;
    ll sum=0;
    while(j<(v2.size()))
    {
        while(i<(v1.size()) && (sum<v2[j]))
        {
            if(sum==0)
             {
                sum+=v1[i];
                i++;
             }
            else if(sum>0)
            {
                if(abs(v1[i]-sum)<=1)
                {
                    sum+=v1[i];
                    i++;
                }
                else
                {
                    cout<<"No"<<"\n";
                    return;
                }
            }
        }
        if(sum!=(v2[j]))
        {
            cout<<"No"<<"\n";
            return;
        }
        else
        {
            sum=0;
            j++;
        }
    }
    if(s2.size()==0 && s1.size()!=0)
    {
        cout<<"No"<<"\n";
        return;
    }
    cout<<"Yes"<<"\n";

}

int main() {
    fastio;

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

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

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll,ll> ull;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define REPR(i, a, b) for (int i = (a); i > (b); --i)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())
#define REVERSE(v) reverse(all(v))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define input(a, n) for(int i = 0; i < n; i++) cin >> a[i]

const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LLINF = 1e18;
const double PI = acos(-1.0);
const double EPS = 1e-9;

#define DEBUG
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define dbg(x) cerr << #x << " = " << x << endl
#endif

template <typename T>
void print(T t) { cout << t << "\n"; }

template <typename T, typename... Args>
void print(T t, Args... args) {
    cout << t << " ";
    print(args...);
}

template <typename T>
void printVector(const vector<T>& v) {
    for (const T& x : v) cout << x << " ";
    cout << "\n";
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll mod_add(ll a, ll b, ll m) { return (a % m + b % m) % m; }
ll mod_sub(ll a, ll b, ll m) { return (a % m - b % m + m) % m; }
ll mod_mul(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }
ll mod_pow(ll b, ll p, ll m) {
    ll r = 1;
    while (p) {
        if (p & 1) r = mod_mul(r, b, m);
        b = mod_mul(b, b, m);
        p >>= 1;
    }
    return r;
}

void solve() {
    ll n,m;
    cin>>n>>m;
    vll a(n,0),b(m,0);
    multiset<ll>s1,s2;
    REP(i,0,n)
    {
        cin>>a[i];
        s1.insert(a[i]);
    }
    REP(j,0,m)
    {
        cin>>b[j];
        s2.insert(b[j]);
    }
    // multiset<ll>s1,s2;
    for(int i=0;i<=(n-m);i++)
    {
        while(!(s1.empty()) && !(s2.empty()))
        {
            ll num1=*(s1.rbegin());
            ll num2=*(s2.rbegin());
            if(num1==num2)
            {
                s1.erase(s1.find(num1));
                s2.erase(s2.find(num1));
            }
            else break;
        }
        if(s1.empty() && s2.empty())
        {
            yes;
            return;
        }
        if(!s2.empty())
        {
        ll last=*(s2.rbegin());
        s2.erase(s2.find(last));
        s2.insert(last/2);
        s2.insert((last+1)/2);
        }
    }
    if(s1.empty() && s2.empty())
    {
        yes;
        return;
    }
    else no;

}

int main() {
    fastio;

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

Answer: 2


import sys

import string

import math

import array

import bisect

from heapq import heappop,heappush

from functools import lru_cache,cache

from collections import deque,defaultdict,Counter

from itertools import permutations,combinations

#sys.setrecursionlimit(10**5)

input = sys.stdin.readline

printf = lambda x:sys.stdout.write(x)

INF = 10**15

RI = lambda: int(sys.stdin.readline())

RF = lambda: float(sys.stdin.readline())

RS = lambda: sys.stdin.readline().strip()

RFF = lambda: map(float, sys.stdin.readline().split())

RII = lambda: map(int, sys.stdin.readline().split())

RSS = lambda: map(str, sys.stdin.readline().strip().split())

RIL = lambda: list(RII())

RFL = lambda: list(RFF())

RSL = lambda: list(RSS())

# from types import GeneratorType

# def bootstrap(f, stack=[]):

#     def wrappedfunc(*args, **kwargs):

#         if stack:

#             return f(*args, **kwargs)

#         else:

#             to = f(*args, **kwargs)

#             while True:

#                 if type(to) is GeneratorType:

#                     stack.append(to)

#                     to = next(to)

#                 else:

#                     stack.pop()

#                     if not stack:

#                         break

#                     to = stack[-1].send(to)

#             return to

#     return wrappedfunc

def main():

    test = RI()

    for i in range(test):

        n,m = RII()

        a = RIL()

        b = RIL()

        db = Counter([i for i in b])

        a.sort()

        q = []

        for i in a:

            #q.append(i)

            heappush(q,i)

        cnt = 0

        while q and cnt<n-m:

            #print(q)

            x = heappop(q)

            if db[x]>=1 or (not q) or abs(x-q[0])>1:

                db[x] -= 1

                continue

            y = 0

            if q:

                y = heappop(q)

            else:

                break

            if db[x+y]>=1:

                db[x+y] -= 1

                cnt += 1

                continue

            else:

                heappush(q,x+y)

                cnt += 1

        while q:

            x = heappop(q)

            db[x] -= 1

        #print(db)

        flag = False

        for i,x in db.items():

            if x!=0:

                flag = True

                break

        if flag==False:

            print('Yes')

        else:

            print('No')

if __name__ == '__main__':

    main()

import sys

import string

import math

import array

import bisect

from heapq import heappop,heappush

from functools import lru_cache,cache

from collections import deque,defaultdict,Counter

from itertools import permutations,combinations

#sys.setrecursionlimit(10**5)

input = sys.stdin.readline

printf = lambda x:sys.stdout.write(x)

INF = 10**15

RI = lambda: int(sys.stdin.readline())

RF = lambda: float(sys.stdin.readline())

RS = lambda: sys.stdin.readline().strip()

RFF = lambda: map(float, sys.stdin.readline().split())

RII = lambda: map(int, sys.stdin.readline().split())

RSS = lambda: map(str, sys.stdin.readline().strip().split())

RIL = lambda: list(RII())

RFL = lambda: list(RFF())

RSL = lambda: list(RSS())

from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):

        if stack:

            return f(*args, **kwargs)

        else:

            to = f(*args, **kwargs)

            while True:

                if type(to) is GeneratorType:

                    stack.append(to)

                    to = next(to)

                else:

                    stack.pop()

                    if not stack:

                        break

                    to = stack[-1].send(to)

            return to

    return wrappedfunc

def main():

    test = RI()

    for i in range(test):

        n,m = RII()

        a = RIL()

        b = RIL()

        #db = Counter(b)

        #a.sort()

        da = Counter([str(i) for i in a])

        # @bootstrap

        # def dp(i):

        #     if i==1:

        #         da[i] -= 1

        #         yield None

        #     if da[i]>=1:

        #         da[i] -= 1

        #         yield None

        #     x = i//2

        #     y = x+i%2

        #     yield dp(x)

        #     yield dp(y)

        #     yield None

        cnt = 0

        @bootstrap

        def dp(i):

            nonlocal cnt

            if cnt>=n:

                da[str(0)] -= 1

                yield None

            if i==1:

                da[str(i)] -= 1

                cnt += 1

                yield None

            if da[str(i)]>=1:

                da[str(i)] -= 1

                cnt += 1

                yield None

            x = i//2

            y = x+i%2

            yield dp(x)

            yield dp(y)

            yield None

        for i in b:

            dp(i)

        #print(da)

        flag = True

        for x in da.values():

            if x!=0:

                flag = False

                break

        if flag==True:

            print('Yes')

        else:

            print('No')

if __name__ == '__main__':

    main()

Answer: 2


#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define M 1000000007
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int main() {
    IOS;
    ll t;
    cin>>t;

    while(t--){
	    ll n,m;
		cin>>n>>m;

		map<ll,ll> a;

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

			a[k]++;
		}

		map<ll,ll> b;

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

			b[k]++;
		}

		ll ct = 0;

		bool k = 0;

		while(b.size() && ct<=n-m){
			auto it = b.rbegin();

			ll x = it->first;
			if(b[x]<a[x]){
				k = 1;
				cout<<"No\n";
				break;
			}
			else if(b[x]>a[x]){
				if(x == 1){
					k = 1;
					cout<<"No\n";
					break;
				}
				b[x]--;
				b[x/2]++;
				b[(x+1)/2]++;
				ct++;
			}
			else{
				b[x] = 0;
				a[x] = 0;
				a.erase(x);
				b.erase(x);
			}
		}

		if(k == 0 && (a.size() || b.size())) cout<<"No\n";
		else if(k == 0) cout<<"Yes\n";

	}
}

#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define M 1000000007
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int main() {
    IOS;
    ll t;
    cin>>t;

    while(t--){
	    ll n,m;
		cin>>n>>m;

		map<ll,ll> a;

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

			a[k]++;
		}

		map<ll,ll> b;

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

			b[k]++;
		}

		ll ct = 0;

		while(b.size() && ct<=n-m){
			auto it = b.rbegin();

			ll x = it->first;
			if(b[x]<a[x]){
				cout<<"No\n";
				break;
			}
			else if(b[x]>a[x]){
				if(x == 1){
					cout<<"No\n";
					break;
				}
				b[x]--;
				b[x/2]++;
				b[(x+1)/2]++;
				ct++;
			}
			else{
				b[x] = 0;
				a[x] = 0;
				a.erase(x);
				b.erase(x);
			}
		}

		if(a.size() || b.size()) cout<<"No\n";
		else cout<<"Yes\n";

	}
}

Answer: 1


// #define ONLINE_JUDGE 1
// clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long; using pll = pair<ll, ll>;
#define all(a) (a).begin(),(a).end()
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
const ll N = 2e6 + 10;
#ifdef LOCAL
#include <dbg.h>
#else
#define dbg(...) (__VA_ARGS__)
#endif
template<class... T> void input(T &...a) { (cin >> ... >> a); }
template<class T,class U> bool chmax(T &a, const U b) {return a<b?a=b,true:false; }
template<class T,class U> bool chmin(T &a, const U b) {return a>b?a=b,true:false; }
// clang-format on

void solve()
{
    LL(n, m);
    dbg(n, m);
    map<ll, ll> mp;
    for (ll i = 0; i < n; i++)
    {
        LL(x);
        mp[x]++;
    }
    ll cnt = 0;
    priority_queue<ll> qu;
    for (ll i = 0; i < m; i++)
    {
        LL(x);
        qu.push(x);
    }
    while (!qu.empty() && cnt < n)
    {
        if((ll)qu.size() > n-cnt)return void(cout << "No\n");
        ll x = qu.top();
        qu.pop();
        if (mp[x])
        {
            mp[x]--;
            cnt++;
        }
        else
        {
            if(x == 1)return void(cout << "No\n");
            qu.push(x / 2);
            qu.push((x + 1) / 2);
        }
    }
    cout << (cnt == n ? "Yes\n" : "No\n");
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

// #define ONLINE_JUDGE 1
// clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long; using pll = pair<ll, ll>;
#define all(a) (a).begin(),(a).end()
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
const ll N = 2e6 + 10;
#ifdef LOCAL
#include <dbg.h>
#else
#define dbg(...) (__VA_ARGS__)
#endif
template<class... T> void input(T &...a) { (cin >> ... >> a); }
template<class T,class U> bool chmax(T &a, const U b) {return a<b?a=b,true:false; }
template<class T,class U> bool chmin(T &a, const U b) {return a>b?a=b,true:false; }
// clang-format on

void solve()
{
    LL(n, m);
    map<ll, ll> mp;
    for (ll i = 0; i < n; i++)
    {
        LL(x);
        mp[x]++;
    }
    ll cnt = 0;
    function<ll(ll)> dfs = [&](ll x) -> ll
    {
        dbg(x);
        if (mp[x]) return mp[x]--, cnt++, 0;
        if (cnt >= n || x == 1) return 1;
        if (dfs(x / 2)) return 1;
        return dfs((x + 1) / 2);
    };
    for (ll i = 1; i <= m; i++)
    {
        LL(x);
        if (dfs(x)) return cout << "No\n", void();
    }
    cout << (cnt == n ? "Yes\n" : "No\n");
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=1e4+5;

int main(){
    ll T;
    cin>>T;
    while(T--){
        ll n;
        cin>>n;
        ll m;
        cin>>m;
        ll a[n],b[m];
        map<ll,ll> ma,mb;
        queue<ll> q;
        for(ll i=0;i<n;i++){
            cin>>a[i];
            ma[a[i]]=0;
            mb[a[i]]=0;
        }
        for(ll i=0;i<m;i++){
            cin>>b[i];
            ma[b[i]]=0;
        }
        sort(a,a+n);
        sort(b,b+m);
        for(ll i=0;i<m;i++){
            ma[b[i]]++;
        }
        ll count=0;
        ll mini=1e12;
        for(ll i=0;i<n;i++){
            if(ma[a[i]]>0) ma[a[i]]--;
            else{
                mb[a[i]]++;
                count++;
                mini=min(mini,a[i]);
            }
        }
        for(auto z:ma){
            if(z.second>0){
                for(ll i=0;i<z.second;i++) q.push(z.first);
            }
        }
        ll mini1=1e12;
        ll flag=0;
        while(q.empty()==0){
            ll x=q.size();
            if(x>count){
                flag=1;
                break;
            }
            for(ll i=0;i<x;i++){
                ll pp=q.front();
                q.pop();
                ll c=pp/2;
                ll d=pp-c;
                if(mb[c]>0){
                    mb[c]--;
                    count--;
                }
                else{
                    q.push(c);
                    mini1=min(mini1,c);
                }
                if(mb[d]>0){
                    mb[d]--;
                    count--;
                }
                else{
                    q.push(d);
                    mini1=min(mini1,d);
                }
            }
            if(mini1<mini){
                flag=1;
                break;
            }
        }
        if(flag==1 || count>0) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=1e4+5;

int main(){
    ll T;
    cin>>T;
    ll c=0;
    while(T--){
        ll n;
        cin>>n;
        ll m;
        cin>>m;
        ll a[n],b[m];
        map<ll,ll> ma;
        for(ll i=0;i<n;i++){
            cin>>a[i];
            ma[a[i]]=0;
        }
        for(ll i=0;i<m;i++){
            cin>>b[i];
            ma[b[i]]=0;
        }
        sort(a,a+n);
        sort(b,b+m);
        for(ll i=0;i<m;i++){
            ma[b[i]]++;
        }
        priority_queue<ll,vector<ll>,greater<ll>> pq;
        ll flag=0;
        for(ll i=0;i<n;i++){
            if(ma[a[i]]>0) ma[a[i]]--;
            else pq.push(a[i]);
        }
        ll sz=pq.size();
        while(sz>1){
            ll x=pq.top();
            pq.pop();
            sz--;
            if(ma[x]>0) ma[x]--;
            else{
                ll y=pq.top();
                pq.pop();
                if(abs(x-y)>1){
                    flag=1;
                    break;
                }
                pq.push(x+y);
            }
        }
        if(sz>0){
        ll pp=pq.top();
        if(ma[pp]>0){
            ma[pp]--;
            pq.pop();
            sz--;
        }
        }
        for(auto z:ma){
            if(z.second>0) flag=1;
        }
        if(sz>0) flag=1;
        if(flag==1) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define b back
#define cout(x) cout << x << endl
#define lb long double
#define soa(arr, n) sort(arr,arr+n)
#define inputVector(v, n) for(ll i = 0; i < n; i++) cin >> v[i]
#define sov(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define pl pair<ll, ll>
#define all(a)    a.begin(), a.end()
#define vpp vector<pair<ll, ll>>
#define fastIo ios_base::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define No cout << "NO\n";
#define Yes cout << "YES\n";
typedef long long int  ll;

const ll mod = 998244353;

void solve(ll n, ll m, vector<ll> & a, vector<ll> & b) {
    ll sa = 0;
    for(auto it: a) {
        sa += it;
    }

    for(auto it: b) {
        sa -= it;
    }

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

    multiset<ll> aa, bb;

    for(auto it: a) {
        aa.insert(it);
    }

    for(auto it: b) {
        if(aa.find(it) != aa.end()) {
            aa.erase(aa.find(it));
        } else {
            bb.insert(it);
        }
    }

    // for(auto it: aa) {
    //     cout << it << " ";
    // }
    // cout << endl;

    // for(auto it: bb) {
    //     cout << it << " ";
    // }

    // cout << endl;

    // from aa now from bb.

    while(!bb.empty() && !aa.empty()) {

        auto it = * bb.begin();
        bb.erase(bb.find(it));
        if(aa.find(it) != aa.end()) {
            aa.erase(aa.find(it));
            continue;
        }

        if(it > 1) {
            int x = it/2;
            int y = it - x;

            bb.insert(x);
            bb.insert(y);
        } else {
            break;
        }
    }

    // for(auto it: aa) {
    //     cout << it << " ";
    // }
    // cout << endl;

    // for(auto it: bb) {
    //     cout << it << " ";
    // }

    // cout << endl;

    if(aa.empty() && bb.empty()) {
        cout << "Yes\n";
        return;
    }

    if(aa.size() == 1 && bb.size() == 1 && (*aa.begin()) == (*bb.begin())) {
        cout << "Yes\n";
        return;
    }

    cout << "No\n";
}

 int main() {
    ll t=1,i=1, n, m;

    cin >> t;

    while(t --) {
        ll n, m;
        cin >> n >> m;
        vector<ll> a(n), bb(m);
        for(ll i = 0; i < n; i ++) {
            cin >> a[i];
        }

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

    }
}

// 2 2 3 3 5
// 6 9

// k = a + b +

#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define b back
#define cout(x) cout << x << endl
#define lb long double
#define soa(arr, n) sort(arr,arr+n)
#define inputVector(v, n) for(ll i = 0; i < n; i++) cin >> v[i]
#define sov(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define pl pair<ll, ll>
#define all(a)    a.begin(), a.end()
#define vpp vector<pair<ll, ll>>
#define fastIo ios_base::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define No cout << "NO\n";
#define Yes cout << "YES\n";
typedef long long int  ll;

const ll mod = 998244353;

void solve(ll n, ll m, vector<ll> & a, vector<ll> & b) {
    ll sa = 0;
    for(auto it: a) {
        sa += it;
    }

    for(auto it: b) {
        sa -= it;
    }

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

    multiset<ll> aa, bb;

    for(auto it: a) {
        aa.insert(it);
    }

    for(auto it: b) {
        if(aa.find(it) != aa.end()) {
            aa.erase(aa.find(it));
        } else {
            bb.insert(it);
        }
    }

    // from aa now from bb.

    while(aa.size() >= 2) {

        auto it = aa.begin();
        auto it1 = aa.begin();
        it1 ++;
        auto itf = * it;
        auto its = * it1;

        if(bb.find(itf) != bb.end()) {
            aa.erase(it);
            bb.erase(bb.find(itf));
            continue;
        }

        if(its - itf <= 1) {
            aa.erase(it);
            aa.erase(it1);

            if(bb.find(itf + its) != bb.end()) {
                bb.erase(bb.find(itf + its));
            } else {
                aa.insert(itf + its);
            }
        } else {
            break;
        }

    }

    if(aa.empty() && bb.empty()) {
        cout << "Yes\n";
        return;
    }

    if(aa.size() == 1 && bb.size() == 1 && (*aa.begin()) == (*bb.begin())) {
        cout << "Yes\n";
        return;
    }

    cout << "No\n";
}

 int main() {
    ll t=1,i=1, n, m;

    cin >> t;

    while(t --) {
        ll n, m;
        cin >> n >> m;
        vector<ll> a(n), bb(m);
        for(ll i = 0; i < n; i ++) {
            cin >> a[i];
        }

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

    }
}

Answer: 1


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define int long long
#define PII pair<int, int>
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
#define _per(i,a,b) for( int i=(a); i>=(b); --i)
using namespace std;
const int N = 1e6+10 ;
const int M = 1e3+10 ;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7  ;
const double eps = 1e-9 ;
int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
int lcm(int a, int b){ return a * b / gcd(a,b); }
int qmi(int a, int b){
    int res = 1 ;
    while(b){
        if(b&1) res = a * res % MOD ;
        b >>= 1 ;
        a = a * a % MOD ;
    }
    return res ;
}
int T = 1 ;
int ans = 0 ;
int n , m, k, q ;
int a[N], b[N] ;
map<int,int> mp ;
void dfs(int x){
    if(mp[x]){
        mp[x] -- ;

    }else if(x!=1){
        int a = x + 1 >> 1 ;
        int b = x >> 1 ;
        dfs(a) ;
        dfs(b) ;
    }
}
void solve(){
    mp.clear() ;
    cin >> n >> m ;
    int sum = 0 ;
    for(int i = 1; i <= n ; ++ i){
        cin >> a[i] ;
        sum += a[i] ;
        mp[a[i]] ++ ;
    }
    for(int i = 1; i <= m ; ++ i){
        cin >> b[i] ;
        sum -= b[i] ;
    }
    sort(a + 1, a + 1 + n) ;
    sort(b + 1, b + 1 + m) ;
    if(sum != 0 || a[1] > b[1] || b[m] < a[n]){
        cout << "No" << endl ;
        return ;
    }
    for(int i = 1; i <= m ; ++ i){
        dfs(b[i]) ;
    }
    for(auto e: mp){
        if(e.second != 0){
            cout << "No" << endl ;
            return ;
        }
    }
    cout << "Yes" << endl ;

}
signed main(){
    IOS ;
    cin >> T ;
    while(T --){
        solve() ;
    }
    return 0;

}

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define int long long
#define PII pair<int, int>
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
#define _per(i,a,b) for( int i=(a); i>=(b); --i)
using namespace std;
const int N = 1e6+10 ;
const int M = 1e3+10 ;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7  ;
const double eps = 1e-9 ;
int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
int lcm(int a, int b){ return a * b / gcd(a,b); }
int qmi(int a, int b){
    int res = 1 ;
    while(b){
        if(b&1) res = a * res % MOD ;
        b >>= 1 ;
        a = a * a % MOD ;
    }
    return res ;
}
int T = 1 ;
int ans = 0 ;
int n , m, k, q ;
int a[N], b[N] ;
map<int,int> mp ;
void dfs(int x){
    if(mp[x]){
        mp[x] -- ;

    }else if(x /2 >= a[1]){
        int a = x + 1 >> 1 ;
        int b = x >> 1 ;
        dfs(a) ;
        dfs(b) ;
    }
}
void solve(){
    mp.clear() ;
    cin >> n >> m ;
    int sum = 0 ;
    for(int i = 1; i <= n ; ++ i){
        cin >> a[i] ;
        sum += a[i] ;
        mp[a[i]] ++ ;
    }
    for(int i = 1; i <= m ; ++ i){
        cin >> b[i] ;
        sum -= b[i] ;
    }
    sort(a + 1, a + 1 + n) ;
    sort(b + 1, b + 1 + m) ;
    if(sum != 0 || a[1] > b[1] || b[m] < a[n]){
        cout << "No" << endl ;
        return ;
    }
    for(int i = 1; i <= m ; ++ i){
        dfs(b[i]) ;
    }
    for(auto e: mp){
        if(e.second != 0){
            cout << "No" << endl ;
            return ;
        }
    }
    cout << "Yes" << endl ;

}
signed main(){
    IOS ;
    cin >> T ;
    while(T --){
        solve() ;
    }
    return 0;

}

Answer: 2


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

#pragma GCC optimize(2)
#define debug(a) cout<<#a<<'='<<a<<endl
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repp(i,a,b) for(int i=a ;i<=b;i++)
#define per(i,b,a) for(int i=b;i>a;i--)
#define perr(i,b,a) for(int i=b;i>=a;i--)
#define pii pair<int,int>
#define mm memset
#define endl '\n'
#define ff first
#define ss second
#define pb push_back
#define INF 0x7fffffffffffffff

const double eps =1e-8;
const int mod=998244353;
const int N=2e5+2;

int a[N];
int b[N];

bool dfs(int x,unordered_map<int,int>&h)
{
    if(x==1)
    {
        if(!h[1])return false;
        h[1]--;return true;
    }
    if(h[x])
    {
        h[x]--;
        return true;
    }
    else
    {
        if(x%2)
        {
            if(!dfs(x/2+1,h))
                return false ;
            if(!dfs(x/2,h))
                return false;
        }
        else
        {
            if(!dfs(x/2,h))
                return false;
            if(!dfs(x/2,h))
                return false;
        }
    }
    return true;
}
void fun(){
    int n,m;
    cin>>n>>m;
    ll sa=0,sb=0;
    unordered_map<int,int>h1,h2;
    repp(i,1,n)cin>>a[i],sa+=a[i],h1[a[i]]++;
    repp(i,1,m)cin>>b[i],sb+=b[i],h2[b[i]]++;
    if(sa!=sb)
    {
        cout<<"No"<<endl;
        return;
    }
    repp(i,1,m)
    {
        ll zong=b[i];
        if(!dfs(zong,h1))
        {
            cout<<"No"<<endl;
            return ;
        }
    }
    cout<<"Yes"<<endl;
    return ;

}

signed main(){

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

	int t=1 ;
	cin>>t;

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

}

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

#pragma GCC optimize(2)
#define debug(a) cout<<#a<<'='<<a<<endl
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repp(i,a,b) for(int i=a ;i<=b;i++)
#define per(i,b,a) for(int i=b;i>a;i--)
#define perr(i,b,a) for(int i=b;i>=a;i--)
#define pii pair<int,int>
#define mm memset
#define endl '\n'
#define ff first
#define ss second
#define pb push_back
#define INF 0x7fffffffffffffff

const double eps =1e-8;
const int mod=998244353;
const int N=2e5+2;

int a[N];
int b[N];

bool dfs(int x,map<int,int>&h)
{
    if(x==1)
    {
        if(!h[1])return false;
        h[1]--;return true;
    }
    if(h[x])
    {
        h[x]--;
        return true;
    }
    else
    {
        if(x%2)
        {
            if(!dfs(x/2+1,h))
                return false ;
            if(!dfs(x/2,h))
                return false;
        }
        else
        {
            if(!dfs(x/2,h))
                return false;
            if(!dfs(x/2,h))
                return false;
        }
    }
    return true;
}
void fun(){
    int n,m;
    cin>>n>>m;
    ll sa=0,sb=0;
    map<int,int>h1,h2;
    repp(i,1,n)cin>>a[i],sa+=a[i],h1[a[i]]++;
    repp(i,1,m)cin>>b[i],sb+=b[i],h2[b[i]]++;
    if(sa!=sb)
    {
        cout<<"No"<<endl;
        return;
    }
    repp(i,1,m)
    {
        ll zong=b[i];
        if(!dfs(zong,h1))
        {
            cout<<"No"<<endl;
            return ;
        }
    }
    cout<<"Yes"<<endl;
    return ;

}

signed main(){

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

	int t=1 ;
	cin>>t;

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

}

Answer: 2


#include <iostream>

#include <algorithm>

#include <cstring>

#include <map>

#include <vector>

#include <queue>

using namespace std;

#define int long long

const int MAXN=1000010,inf=0x3f3f3f3f;

int n,m;

int a[MAXN],b[MAXN];

map<int,int>mp;

int check(int x){

	if (mp[x]){

		mp[x]--;

		return 1;

	}

	if (x==0)return 0;

	if (x&1)return check(x/2)&&check((x+1)/2);

	return check(x/2)&&check(x/2);

}

void solve(){

	cin>>n>>m;

	int sum=0;

	for (int i=1;i<=n;i++)cin>>a[i],mp[a[i]]++,sum+=a[i];

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

		cin>>b[i];

		sum-=b[i];

	}

	if (sum!=0){

		cout<<"No"<<endl;

		return;

	}

	sort(b+1,b+1+m);

	int flag=1;

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

		if (!check(b[i])){

			flag=0;

			break;

		}

	}

	if (flag){

		cout<<"Yes"<<endl;

	}

	else{

		cout<<"No"<<endl;

	}

}

signed main(){

	ios::sync_with_stdio(0);

	cin.tie(0);

	int t=1;

	cin>>t;

	while(t--)

	{

		solve();

	}

	return 0;

}

#include <iostream>

#include <algorithm>

#include <cstring>

#include <map>

#include <vector>

#include <queue>

using namespace std;

#define int long long

const int MAXN=1000010,inf=0x3f3f3f3f;

int n,m;

int a[MAXN],b[MAXN];

map<int,int>mp;

int check(int x){

	if (mp[x]){

		mp[x]--;

		return 1;

	}

	if (x==0)return 0;

	if (x&1)return check(x/2)&&check(x/2+1);

	return (check(x/2)&&check(x/2));

}

void solve(){

	cin>>n>>m;

	mp.clear();

	int sum=0;

	for (int i=1;i<=n;i++)cin>>a[i],mp[a[i]]++,sum+=a[i];

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

		cin>>b[i];

		sum-=b[i];

	}

	if (sum!=0){

		cout<<"No"<<endl;

		return;

	}

	sort(b+1,b+1+m);

	int flag=1;

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

		if (!check(b[i])){

			flag=0;

			break;

		}

	}

	if (flag){

		cout<<"Yes"<<endl;

	}

	else{

		cout<<"No"<<endl;

	}

}

signed main(){

	ios::sync_with_stdio(0);

	cin.tie(0);

	int t=1;

	cin>>t;

	while(t--)

	{

		solve();

	}

	return 0;

}

Answer: 2


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<bits/stdc++.h>
#include<string>
#include <numeric>
#include<map>
#include<set>
#include<iomanip>
using namespace std;
#define py cout << "YES" << endl
#define pn cout << "NO" << endl
#define ll long long
#define forw(i, j, n) for (ll i = j; i < n; i++)
#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define pb push_back
#define ppb pop_back
#define all(x) x.begin(), x.end()
bool fun(ll x,unordered_map<ll,ll>&mp1,unordered_map<ll,bool>&mp2){
    if(x<=0)return false;
    if(mp2.count(x) && mp2[x]==false)return mp2[x];
    bool ans=true;
    if(mp1[x]>0){
        mp1[x]--;
        return true;
    }
    ll l,r;
    if(x%2){
        l=x/2;
        r=l+1;
    }
    else{
        l=x/2;
        r=x/2;
    }
        if(l==1){
            if(mp1[1]>0){
                mp1[1]--;
            }
            else ans=false;

        }
        else if(l==0){ans=false;}

       else ans&=fun(l,mp1,mp2);
    if(r==1){
         if(mp1[1]>0){
                mp1[1]--;
            }
            else ans=false;
    }
    else if(r==0){ans=false;}

        else ans&=fun(r,mp1,mp2);
    mp2[x]=ans;
    return ans;
}
int main() {
	int t; cin>>t;
	while(t--){
	    ll n,m; cin>>n>>m;
	    vector<ll> a(n),b(m);
	    unordered_map<ll,ll>mp1;
	    unordered_map<ll,bool>mp2;
	    forw(i,0,n){
	        cin>>a[i];
	  mp1[a[i]]++;
	    }
	     forw(i,0,m){
	        cin>>b[i];

	    }
	   bool ans=true;
	   forw(i,0,m){
	       ans&=fun(b[i],mp1,mp2);
	       if(ans==false)break;
	   }
	for(auto it:mp1){
	    if(it.second>0){ans=false; break;}
	}

	    if(ans)py;
	    else pn;
	}
	return 0;
}
// 1 1 1 1 1 2 2 2 2 2 3 3 3 3

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<bits/stdc++.h>
#include<string>
#include <numeric>
#include<map>
#include<set>
#include<iomanip>
using namespace std;
#define py cout << "YES" << endl
#define pn cout << "NO" << endl
#define ll long long
#define forw(i, j, n) for (ll i = j; i < n; i++)
#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define pb push_back
#define ppb pop_back
#define all(x) x.begin(), x.end()

int main() {
	int t; cin>>t;
	forw(k,0,t){
	    ll n,m; cin>>n>>m;
	    vector<ll> a(n),b(m);
	    multiset<ll>mp1,mp2;
	    forw(i,0,n){
	        cin>>a[i];
	  mp1.insert(a[i]);
	    }
	     forw(i,0,m){
	        cin>>b[i];
	        mp2.insert(b[i]);
	    }

	   bool ans=true;

	   while(!mp1.empty() && !mp2.empty() && mp1.size()>=mp2.size()){
	       ll x=*--mp2.end();
	       //ll x1=*--mp2.end();
	       //cout<<x<<" ";
	       if(mp1.find(x)!=mp1.end()){
	           mp2.erase(mp2.find(x));
	           mp1.erase(mp1.find(x));

	       }
	       else{
	           mp2.erase(mp2.find(x));
	           ll l,r;
	           if(x%2){l=x/2; r=l+1;}
	           else{l=x/2; r=l;}

	            mp2.insert(l);
	            mp2.insert(r);

	       }
	       //cout<<mp1.size()<<" "<<mp2.size()<<endl;

	   }

	   while(!mp2.empty()){
	       ll it=*mp2.begin();
	       if( !mp1.empty() && mp1.find(it)!=mp1.end()){
	           mp1.erase(mp1.find(it));
	           mp2.erase(mp2.find(it));
	       }
	       else break;
	   }

	    if(mp1.empty() && mp2.empty())py;
	    else pn;
	}
	return 0;
}
// 4 5   2 3 4  1 1 1 1 1 1
// 1 1 1 1 1 2 2 2 2 2 3 3 3 3

Answer: 2


#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pb push_back
#define endl "\n"
const int mod = 1e9 + 7;
const int N = 300000 + 10;
using namespace std;
typedef pair<int, int> pi;
int n, m, k;
int a[N], b[N];
char c[4][4];
int gcd(int A, int b) {
    return b ? gcd(b, A % b) : A;
}
string s, s1, s2;
map<int, int>mp;
int qsm(int a, int b) {//a/b%mod,快速幂，费马小定理
    int ans = 1;
    while (b) {
        if (b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    } return ans;
}int x, y;
priority_queue<int,vector<int>, greater<int>>q;
bool cmp(int x, int y) {
    return x < y;
}
struct pai{
    int x, y;
}d[N];
void solve() {
    cin >> n >> m;
    int maxx = -1;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    while (!q.empty()) {
        q.pop();
    }
    for (int i = 1; i <= m; i++) {
        int u;
        cin >> u;
        q.push(u);
    }
    mp.clear();
    for (int i = 1; i <= n; i++) {
        if (mp.find(a[i]) != mp.end()) {
            mp[a[i]]++;
        }
        else {
            mp[a[i]] = 1;
        }
    }
    while (!q.empty()) {
        int u = q.top();
        q.pop();
        if (mp[u] > 0) {
            mp[u]--;
        }
        else {
            if (u == 1) {
                cout << "No" << endl;
                return;
            }
            else {
                if (u % 2 == 1) {
                    q.push(u / 2);
                    q.push(u / 2 + 1);
                }
                else {
                    q.push(u / 2);
                    q.push(u / 2);
                }
            }
        }
    }
    for (auto i : mp) {
        if (i.second > 0) {
            cout << "No" << endl;
            return;
        }
    }
    cout << "Yes" << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(12);  // 小数保留位数
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pb push_back
#define endl "\n"
const int mod = 1e9 + 7;
const int N = 300000 + 10;
using namespace std;
typedef pair<int, int> pi;
int n, m, k;
int a[N], b[N];
char c[4][4];
int gcd(int A, int b) {
    return b ? gcd(b, A % b) : A;
}
string s, s1, s2;
map<int, int>mp;
int qsm(int a, int b) {//a/b%mod,快速幂，费马小定理
    int ans = 1;
    while (b) {
        if (b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    } return ans;
}int x, y;
priority_queue<int>q;//优先队列默认，大顶堆
bool cmp(int x, int y) {
    return x < y;
}
struct pai{
    int x, y;
}d[N];
void solve() {
    cin >> n >> m;
    if (m ==1&&n==1) {
        int aaa, bbb;
        cin >> aaa >> bbb;
        if (aaa == bbb) {
            cout << "Yes" << endl;
        }
        else {
            cout << "No" << endl;
        }
        return;
    }
    int maxx = -1;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    while (!q.empty()) {
        q.pop();
    }
    for (int i = 1; i <= m; i++) {
        int u;
        cin >> u;
        q.push(u);
    }
    mp.clear();
    for (int i = 1; i <= n; i++) {
        if (mp.find(a[i]) != mp.end()) {
            mp[a[i]]++;
        }
        else {
            mp[a[i]] = 1;
        }
    }
    while (!q.empty()) {
        int u = q.top();
        q.pop();
        if (mp[u] > 0) {
            mp[u]--;
        }
        else {
            if (u == 1) {
                cout << "No" << endl;
                return;
            }
            else {
                if (u % 2 == 1) {
                    q.push(u / 2);
                    q.push(u / 2 + 1);
                }
                else {
                    q.push(u / 2);
                    q.push(u / 2);
                }
            }
        }
    }
    for (auto i : mp) {
        if (i.second > 0) {
            cout << "No" << endl;
            return;
        }
    }
    cout << "Yes" << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(12);  // 小数保留位数
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <climits>
#include <algorithm>
#include <functional>
using namespace std;
#define ll long long
#define floop(i, a, b) for (int i = (a); i < (b); i++)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
const ll inf = 1e16;
const int mod = 1e9 + 7;
const int N = 1e6;
ll i = 0, j = 0;
ll n, m;

bool solve(priority_queue<ll> &bb, map<ll, ll> &a)
{
     for (int i = 0; i < (2 * n - m); i++)
     {
          if (bb.empty())
          {
               return false;
          }
          ll num = bb.top();
          bb.pop();

          if (a[num] == 0)
          {
               bb.push(num / 2);
               bb.push((num + 1) / 2);
          }
          else
          {
               a[num]--;
          }
     }
     if (bb.empty())
          return true;
     return false;
}
void ExoticLur3e37()
{
     ll p;
     cin >> n >> m;
     map<ll, ll> a;
     for (i = 0; i < n; i++)
     {
          cin >> p;
          a[p]++;
     }
     vector<ll> b(m);
     // vector<ll> vis(m, 0);
     priority_queue<ll> bb;
     for (i = 0; i < m; i++){
          cin >> b[i];
          bb.push(b[i]);
     }

     // for (int i = 0; i < m; i++)
     // {
     //      int num = b[i];
     //      if (a[num] > 0)
     //      {
     //           a[num]--;
     //           if (a[num] == 0)
     //                a.erase(num);
     //           vis[i] = 1;
     //      }
     // }

     // for (int i = 0; i < m; i++)
     // {
     //      if (!vis[i])
     //           bb.push(b[i]);
     // }

     bool res = solve(bb, a);

     res?cout<<"YES":cout<<"NO";
}
int main()
{
     ios_base::sync_with_stdio(0);
     cin.tie(0);
     cout.tie(0);
     ll t;
     cin >> t;
     // t = 1;
     while (t--)
     {
          ExoticLur3e37();
          cout << "\n";
     }
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <climits>
#include <algorithm>
#include <functional>
using namespace std;
#define ll long long
#define floop(i, a, b) for (int i = (a); i < (b); i++)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
const ll inf = 1e16;
const int mod = 1e9 + 7;
const int N = 1e6;
ll i = 0, j = 0;
ll n, m;
void ExoticLur3e37()
{
     ll n, m, p;
     cin >> n >> m;
     map<ll, ll> a;
     for (i = 0; i < n; i++)
     {
          cin >> p;
          a[p]++;
     }
     vector<ll> b(m);
     vector<ll> vis(m, 0);
     for (i = 0; i < m; i++)
          cin >> b[i];

     for (int i = 0; i < m; i++)
     {
          int num = b[i];
          if (a[num] > 0)
          {
               a[num]--;
               vis[i] = 1;
          }
     }

     priority_queue<ll, vector<ll>, greater<ll>> aa;
     for (auto c : a)
     {
          if (c.second > 0)
          {
               int freq = c.second;
               while (freq--)
               {
                    aa.push(c.first);
               }
          }
     }
     vector<ll> bb;
     for (int i = 0; i < m; i++)
     {
          if (!vis[i])
               bb.push_back(b[i]);
     }

     if (bb.size() == 0 && aa.size() != 0)
     {
          cout << "NO";
          return;
     }

     if (bb.size() == 1 && aa.size() == 1)
     {
          bb[0] == aa.top() ? cout << "YES" : cout << "NO";
          return;
     }

     sort(all(bb));

     i = 0, j = 0;

     while (aa.size() > 1 && j < bb.size())
     {
          ll x = aa.top();
          aa.pop();
          ll y = aa.top();
          aa.pop();
          if (abs(x - y) <= 1 && (x + y) == bb[j])
               j++;
          else if (abs(x - y) <= 1)
          {
               aa.push(x + y);
          }
          else
          {
               cout << "NO";
               return;
          }
     }
     if (aa.size() == 1 && j == bb.size() - 1)
     {
          bb[j] == aa.top() ? cout << "YES" : cout << "NO";
          return;
     }
     if (j == bb.size())
          cout << "YES";
     else
          cout << "NO";
}
int main()
{
     ios_base::sync_with_stdio(0);
     cin.tie(0);
     cout.tie(0);
     ll t;
     cin >> t;
     // t = 1;
     while (t--)
     {
          ExoticLur3e37();
          cout << "\n";
     }
}

Answer: 1


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#define ll long long
#define N 200005
using namespace std;
ll i,j,k,m,n,o,p,l,s,t,sum1,sum2,ans,times,bz;
multiset<ll> f,g;
ll a[N],b[N];
int main()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    scanf("%lld",&times);
    while (times--)
    {
        scanf("%lld%lld",&n,&m);
        if (n==4&m==2)
        {
            int sdfgwse=0;
        }
        f.clear(),g.clear();
        sum1=sum2=0;
        for (i=1;i<=n;i++) scanf("%lld",&a[i]),sum1+=a[i],f.insert(a[i]);
        for (i=1;i<=m;i++) scanf("%lld",&b[i]),sum2+=b[i],g.insert(b[i]);
        if (sum1!=sum2)
        {
            puts("No");
            continue;
        }
        bz=1;
        while ((!f.empty())||(!g.empty()))
        {
            ll x=*f.rbegin(),y=*g.rbegin();
            auto posx=f.find(x),posy=g.find(y);
            if (x==y)
            {
                f.erase(posx),g.erase(posy);
            }
            else if (x>y)
            {
                bz=0;puts("No");break;
            } else {
                g.erase(posy);
                g.insert(y/2),g.insert((y+1)/2);
            }
        }
        if (bz) puts("Yes");
        else puts("No");
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#define ll long long
#define N 200005
using namespace std;
ll i,j,k,m,n,o,p,l,s,t,sum1,sum2,ans,times,bz;
multiset<ll> f,g;
ll a[N],b[N];
int main()
{
   // freopen("a.in","r",stdin);
 //   freopen("a.out","w",stdout);
    scanf("%lld",&times);
    while (times--)
    {
        scanf("%lld%lld",&n,&m);
        bz=1;
        f.clear(),g.clear();
        sum1=sum2=0;
        for (i=1;i<=n;i++) scanf("%lld",&a[i]),sum1+=a[i],f.insert(a[i]);
        for (i=1;i<=m;i++) scanf("%lld",&b[i]),sum2+=b[i],g.insert(b[i]);
        if (sum1!=sum2)
        {
            puts("No");
            continue;
        }
        while ((!f.empty())||(!g.empty()))
        {
            ll x=*f.rbegin(),y=*g.rbegin();
            auto posx=f.find(x),posy=g.find(y);
            if (x==y)
            {
                f.erase(posx),g.erase(posy);
            }
            else if (x>y)
            {
                bz=0;break;
            } else {
                g.erase(posy);
                g.insert(y/2),g.insert((y+1)/2);
            }
        }
        if (bz) puts("Yes");
        else puts("No");
    }
    return 0;
}

Answer: 2


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

#define int long long
#define nl
#define imin INT_MIN
#define imax INT_MAX
#define lmin LLONG_MIN
#define lmax LLONG_MAX
int solve(int x,map<int,int> &m){
    if(m.find(x)!=m.end()){
        m[x]--;
        if(m[x]==0){
            m.erase(x);
        }
        return 0;
    }
    auto it =m.begin();
    if(x<it->first){
        return 1;
    }
    int x1=x/2;
    int y1=(x+1)/2;
    return (solve(x1,m)|solve(y1,m));
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        vector<int> a(n);
        vector<int> b(m);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (int i = 0; i < m; i++)
        {
            cin >> b[i];
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        vector<int> v;
        vector<int> u;
        int i = 0;
        int j = 0;
        int f = 0;
        while (i < n && j < m)
        {
            if (a[i] == b[j])
            {
                i++;
                j++;
            }
            else if (a[i] > b[j])
            {
                u.push_back(b[j]);
                j++;
            }
            else
            {
                v.push_back(a[i]);
                i++;
            }
        }
        while (i < n)
        {
            v.push_back(a[i]);
            i++;
        }
        while (j < m)
        {
            u.push_back(b[j]);
            j++;
        }
        // cout<<1<<endl;
        map<int,int> m1;
        for(auto it : v){
            m1[it]++;
        }
        // cout<<1<<endl;
        for(auto it : u){
            f=(f|solve(it,m1));
            if(f==1){
                break;
            }

        }
        if(!m1.empty()){
            f=1;
        }
        if (f == 1)
        {
            cout << "No" << endl;
        }
        else
        {
            cout << "Yes" << endl;
        }
    }
    return 0;
}

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

#define int long long
#define nl
#define imin INT_MIN
#define imax INT_MAX
#define lmin LLONG_MIN
#define lmax LLONG_MAX

    signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin>>t;
while(t--){
    int n,m;
    cin>>n>>m;
    vector<int> a(n);
    vector<int> b(m);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<m;i++){
        cin>>b[i];
    }
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    vector<int> v;
    vector<int> u;
    int i=0;
    int j=0;
    int f=0;
    int sum=0;
    while(i<n && j<m){
        if(a[i]==b[j]){
            i++;
            j++;
        }
        else if(a[i]>b[j]){
            f=1;
            break;
        }
        else {
            if(abs(sum-a[i])<=1 || sum==0){
                sum+=a[i];
            }
            else{
                // cout<<sum<<" "<<i<<endl;
                f=1;
                break;
            }
            if(sum==b[j]){
                j++;
                sum=0;
            }
            else if(sum>b[j]){
                f=1;
                break;
            }
            i++;
        }
    }
    if(i!=n || j!=m){
        f=1;
    }
    if(f==1){
        cout<<"No"<<endl;
    }
    else{
        cout<<"Yes"<<endl;
    }
}
return 0;
}

Answer: 1


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <random>
#include <iomanip>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <numeric>

using namespace std;

#define LF '\n';
typedef long long lli;
typedef long double ld;

map<lli,lli> a;
map<lli, lli> b;
set<lli> s;

void solve()
{
    a.clear();
    b.clear();
    s.clear();
    lli n,m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        lli x;
        cin >> x;
        a[x]++;
    }
    for (int i = 1; i <= m; i++) {
        lli x;
        cin >> x;
        b[x]++;
        s.insert(x);
    }
    lli new_m=m;
    while (!s.empty()) {
        lli curr = *s.rbegin();
        s.erase(curr);
        if (a[curr] > b[curr]) {
            cout << "NO" << LF;
            return;
        }
        lli d = (b[curr]-a[curr]);
        if (d && curr == 1) {
            cout << "NO" << LF;
            return;
        }
        if (!d)continue;
        new_m += d;
        if (new_m > n) {
            cout << "NO" << LF;
            return;
        }
        b[curr] = a[curr];
        s.insert((curr / 2));
        s.insert(((curr+1) / 2));
        b[curr / 2]+=d;
        b[(curr+1) / 2]+=d;
    }
    for (auto to : a) {
        if (b[to.first] != to.second) {
            cout << "NO" << LF;
        }
    }
    for (auto to : b) {
        if (a[to.first] != to.second) {
            cout << "NO" << LF;
        }
    }
    cout << "YES" << LF;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    lli t;
    cin >> t;
    while (t--)solve();
}

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <random>
#include <iomanip>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <numeric>

using namespace std;

#define LF '\n';
typedef long long lli;
typedef long double ld;

map<lli,lli> a;
map<lli, lli> b;
set<lli> s;

void solve()
{
    a.clear();
    b.clear();
    s.clear();
    lli n,m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        lli x;
        cin >> x;
        a[x]++;
    }
    for (int i = 1; i <= m; i++) {
        lli x;
        cin >> x;
        b[x]++;
        s.insert(x);
    }
    lli new_m=m;
    while (!s.empty()) {
        lli curr = *s.rbegin();
        s.erase(curr);
        if (a[curr] > b[curr]) {
            cout << "NO" << LF;
            return;
        }
        lli d = (b[curr]-a[curr]);
        if (d && curr == 1) {
            cout << "NO" << LF;
            return;
        }
        if (!d)continue;
        new_m += d;
        if (new_m > n) {
            cout << "NO" << LF;
            return;
        }
        b[curr] = a[curr];
        s.insert((curr / 2));
        s.insert(((curr+1) / 2));
        b[curr / 2]+=d;
        b[(curr+1) / 2]+=d;
    }
    for (auto to : a) {
        if (b[to.first] != to.second) {
            cout << "NO" << LF;
            return;
        }
    }
    for (auto to : b) {
        if (a[to.first] != to.second) {
            cout << "NO" << LF;
            return;
        }
    }
    cout << "YES" << LF;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    lli t;
    cin >> t;
    while (t--)solve();
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
namespace A
{
    const long long N = 3e5 + 20, M = 998244353;
#define eps 1e-12
#define int long long
#define ll long long
#define all(x) x.begin(), x.end()
#define endl '\n'
#define ls (p << 1)
#define rs ((p << 1) | 1)
#define f1 first
#define pb push_back
#define f2 second
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(2);
}
using namespace A;
//__int128
int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    x *= f;
    return x;
}
void write(int x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
/*task1
{
每日错题
}*/
/*task2
{
div2 D
做到80%概率做E,10场<=20%概率降级
0 1
50%
提升一个阶段后尝试学习10个新算法
G  or 牛客博弈论专题 10题
}*/
int t, n, m;
signed main()
{
    IOS;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        int x;
        priority_queue<pair<int, int>> q1, q2;
        for (int i = 1; i <= n; i++)
        {
            cin >> x;
            q1.push({x, 1});
        }
        for (int i = 1; i <= m; i++)
        {
            cin >> x;
            q2.push({x, 1});
        }
        int ok = 1;
        while (q1.size() && q2.size())
        {
            auto [p1, t1] = q1.top();
            auto [p2, t2] = q2.top();
            q1.pop(), q2.pop();
            if (p1 == p2)
            {
                if (t1 == t2)
                    continue;
                else if (t1 > t2)
                {
                    q1.push({p1, t1 - t2});
                }
                else
                    q2.push({p2, t2 - t1});
            }
            else if (p1 < p2)
            {
                if (p2 % 2 == 1)
                {
                    q2.push({p2 / 2, t2});
                    q2.push({p2 / 2 + 1, t2});
                    q1.push({p1, t1});
                }
                else
                {
                    int u = t2;
                    while (p2 / 2 >= p1 && p2 % 2 == 0)
                    {
                        p2 /= 2;
                        u *= 2;
                    }
                    q2.push({p2, u});
                    q1.push({p1, t1});
                }
            }
            else
            {
                ok = 0;
                break;
            }
        }
        if (q1.size() || q2.size())
            ok = 0;
        cout << (ok ? "YES\n" : "NO\n");
    }
    return false;
}

#include <bits/stdc++.h>
using namespace std;
namespace A
{
    const long long N = 3e5 + 20, M = 998244353;
#define eps 1e-12
#define int long long
#define ll long long
#define all(x) x.begin(), x.end()
#define endl '\n'
#define ls (p << 1)
#define rs ((p << 1) | 1)
#define f1 first
#define pb push_back
#define f2 second
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(2);
}
using namespace A;
//__int128
int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    x *= f;
    return x;
}
void write(int x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
/*task1
{
每日错题
}*/
/*task2
{
div2 D
做到80%概率做E,10场<=20%概率降级
0 1
50%
提升一个阶段后尝试学习10个新算法
G  or 牛客博弈论专题 10题
}*/
int t, n, m;
signed main()
{
    IOS;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        int x;
        priority_queue<int> q1, q2;
        for (int i = 1; i <= n; i++)
        {
            cin >> x;
            q1.push(x);
        }
        for (int i = 1; i <= m; i++)
        {
            cin >> x;
            q2.push(x);
        }
        int ok = 1;
        while (q1.size() && q2.size())
        {
            int p1 = q1.top(), p2 = q2.top();
            q1.pop(), q2.pop();
            if(q2.size()>q1.size())
            {
                ok=0;
                break;
            }
            if (p1 == p2)
            {
                continue;
            }
            else if (p1 < p2)
            {
                int u = p2 / 2, u1 = p2 - u;
                q2.push(u), q2.push(u1), q1.push(p1);
            }
            else
            {
                ok = 0;
                break;
            }
        }
        if (q1.size() || q2.size())
            ok = 0;
        cout << (ok ? "YES\n" : "NO\n");
    }
    return false;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL

#include "/competitive_programming/debug.cpp"

#else

#define dbg(...)

#endif

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

using i64 = long long;

int main() {

  ios_base::sync_with_stdio(false), cin.tie(0);

  int T;

  cin >> T;

  while (T--) {

    int n, m;

    cin >> n >> m;

    multiset<int> S, T;

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

      int t;

      cin >> t;

      S.insert(t);

    }

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

      int t;

      cin >> t;

      if (S.count(t)) {

        S.erase(S.find(t));

      } else {

        T.insert(t);

      }

    }

    while (T.size()) {

      if (S.size() <= 1) break;

      auto b1 = S.begin();

      auto b2 = next(b1);

      int c1 = *b1, c2 = *b2;

      S.erase(S.find(c1));

      S.erase(S.find(c2));

      if (c2 - c1 <= 1) {

        int v = c1 + c2;

        if (T.count(v)) {

          T.erase(T.find(v));

        } else {

          S.insert(v);

        }

      } else {

        break;

      }

    }

    if (T.size() == 0 && S.size() == 0) {

      cout << "Yes\n";

    } else {

      cout << "No\n";

    }

  }

  return 0;

}

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL

#include "/competitive_programming/debug.cpp"

#else

#define dbg(...)

#endif

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

using i64 = long long;

int main() {

  ios_base::sync_with_stdio(false), cin.tie(0);

  int T;

  cin >> T;

  while (T--) {

    int n, m;

    cin >> n >> m;

    priority_queue<int> S, S1;

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

      int t;

      cin >> t;

      S.push(t);

    }

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

      int t;

      cin >> t;

      S1.push(t);

    }

    while (S.size() > 0 && S1.size() > 0) {

      if (S.size() < S1.size()) break;

      int st = S.top(), s1t = S1.top();

      S.pop();

      S1.pop();

      if (st == s1t) {

        continue;

      } else if (st > s1t) {

        break;

      } else {

        S.push(st);

        S1.push(s1t / 2);

        S1.push((s1t + 1) / 2);

      }

    }

    if (S.size() == 0 && S1.size() == 0) {

      cout << "Yes\n";

    } else {

      cout << "No\n";

    }

    // multiset<i64> S, T;

    // for (int i = 0; i < n; ++i) {

    //   int t;

    //   cin >> t;

    //   S.insert(t);

    // }

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

    //   int t;

    //   cin >> t;

    //   if (S.count(t)) {

    //     S.erase(S.find(t));

    //   } else {

    //     T.insert(t);

    //   }

    // }

    // while (T.size()) {

    //   if (S.size() <= 1) break;

    //   auto b1 = S.begin();

    //   auto b2 = next(b1);

    //   i64 c1 = *b1, c2 = *b2;

    //   S.erase(S.find(c1));

    //   S.erase(S.find(c2));

    //   if (c2 - c1 <= 1) {

    //     i64 v = c1 + c2;

    //     if (T.count(v)) {

    //       T.erase(T.find(v));

    //     } else {

    //       S.insert(v);

    //     }

    //   } else {

    //     break;

    //   }

    // }

    // if (T.size() == 0 && S.size() == 0) {

    //   cout << "Yes\n";

    // } else {

    //   cout << "No\n";

    // }

  }

  return 0;

}

Answer: 2


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

void sol() {
    int n, m; cin >> n >> m;
    map<int, int>mp;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        mp[x]++;
    }
    priority_queue<int> pq;
    for(int i = 1; i <= m; ++i) {
        int x; cin >> x;
        pq.push(x);
    }
    for(int i = 1; i <= 2 * n - m; ++i) {
        if(pq.empty()) return void(cout << "NO\n");
        int x = pq.top();
        pq.pop();
        if(!mp[x]) {
            pq.push(x / 2);
            pq.push((x + 1) / 2);
        } else
            --mp[x];
    }
    cout << (pq.empty() ? "YES\n" : "NO\n");

}

int32_t main() {
    ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
    int T = 1; cin >> T;
    while (T--) sol();
}

Answer: 1