Problem: 2061D - Kevin and Numbers
Language: java

Solution 1 (runtime: 718 ms):
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;
    }
  }
}

Solution 2 (runtime: 702 ms):
import java.util.*;
import java.io.*;
 
public class cp {
    static class Datamax implements Comparable<Datamax>{
        int idx;
        int val;
        public Datamax(int val,int idx){
            this.idx = idx;
            this.val = val;
        }
        @Override
        public int compareTo(Datamax p1){
            if(this.val<p1.val) return 1;
            return -1;
        }
    }
    static class Circle{
        int x;
        int y;
        int radius;
        public Circle(int x,int y,int radius){
            this.x = x;
            this.y = y;
            this .radius = radius;
        }
    }
    // static class Edge {
    //     int dst;
    //     int dir;
    //     public Edge(int dst,int dir){
    //         this.dir = dir;
    //         this.dst = dst;
    //     }
    // }
    public static PP.Reader cs = new PP.Reader(System.in);
    // public static Scanner cs = new Scanner(System.in);
    public static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException{
        int tests = cs.nextInt();
        while(tests>0){
            tests--;
            solve();
        }
        // writer.flush();
        writer.close();
    }
    public static long mod = (long)Math.pow(10,9) + 7;
    public static long MOD = 998244353;
    public static int netchange = 0;
    public static boolean poss;
    // public static int nodes = 0;
    public static void solve() throws IOException{
        // long it = cs.nextLong();
        // int n = (int)it;
        int n = cs.nextInt();
        int m = cs.nextInt();
        // int k = cs.nextInt();
        // long k = cs.nextLong();  
        // String str = cs.next();
        // String strb = cs.next();
        // long[] arr = new long[n];
        // long[] brr = new long[m];
        HashMap<Long,Integer> hm = new HashMap<>();
        for(int i = 0; i < n; i++){
            long temp = cs.nextLong();
            if(hm.containsKey(temp)) hm.put(temp,hm.get(temp)+1);
            else hm.put(temp,1);
        }
        boolean answer = true;
        for(int i = 0; i < m; i++){
            long temp = cs.nextLong();
            if(!check_existence(hm, temp)){
                answer = false;
            }
        } 
        if(!hm.isEmpty() || !answer) System.out.println("No");
        else System.out.println("Yes");
    }
    //Write fxn here     
    public static boolean check_existence(HashMap<Long,Integer> hm,long element){
        if(hm.containsKey(element)){
            if(hm.get(element) == 1) hm.remove(element);
            else hm.put(element,hm.get(element)-1);
            return true;
        }
        if(element == 1) return false;
        long first = element/2;
        long second = element/2;
        if(element%2 == 1) second++;
        return check_existence(hm,first) && check_existence(hm,second);
    }
    public static void dfs(ArrayList<ArrayList<Integer>> graph,int curr,int changed[]){
        for(Integer nb : graph.get(curr)){
            if(changed[nb] != 0){
                if(changed[nb] == changed[curr]) poss = false;
            }
            else{
                if(changed[curr] == 1) changed[nb] = -1;
                else changed[nb] = 1;
                netchange += changed[nb];
                dfs(graph,nb, changed);
            }
        }
    }
    public static  int binary(int[] arr,int start,int end,int key){
        int mid = (int)start;
        int st = start;
        int en = end;
        while(start<=end){
            mid = (int)(start+end)/2;
            if(arr[mid]==key) return mid;
            else if(arr[mid]>key) end = mid-1;
            else if(arr[mid]<key) start = mid+1;
        }
        //if(end<start){
            if(end<st) end++;
            // if(start>en) start--;
            // if(Math.abs(arr[end]-key)<=Math.abs(arr[start]-key-1)){
                // return start;
            // }
            return end;
        //}
        //return mid+1;
    }
    public static long invmod(long A, long M)
    {
 
        for (int X = 1; X < (int)M; X++)
            if (((A % M) * (X % M)) % M == 1)
                return X;
        return 1;
    } 
    public static long gcd(long a,long b){
        long c = Math.min(a,b);
        long d = Math.max(a,b);
        if(d%c==0) return c;
        return gcd(c,d%c);
    }
    public static long divmod(long a,long b,long m){   //claculate (a/b)%m
        // a = a%m;b = b%m  // Include if not working
        long inv =  powermod(b, m-2, m);
        return (inv*a)%m;
    }
    static long powermod(long x, long y, long p) //calculate (x^y)%p
    {
        long res = 1;
        x = x % p; 
        while (y > 0) {
            if ((y & 1) > 0)
                res = (res * x) % p;
            y = y >> 1; 
            x = (x * x) % p;
        }
        return res;
    }
}

class PP {
    static String IN = "%s";
    static String OUT = "%s";

    static class Reader {
        BufferedReader br;
        StringTokenizer st;

        Reader(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        String next() {
            try {
                while (st == null || !st.hasMoreTokens())
                    st = new StringTokenizer(br.readLine());
                return st.nextToken();
            } catch (Exception ignored) {
            }
            return null;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}