Problem: 2061D - Kevin and Numbers
Language: java

Solution 1 (runtime: 718 ms):

import java.io.*;
import java.util.*;

public class ComTemp2 {

    private static final long MINA = 1;
    private static final long MAXA = 1000000000000L;
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static PrintWriter writer;

    static int MOD = (int) (1e9 + 7);
    static final int MOD1 = 998244353;

    public static Scanner scanner = new Scanner(System.in);

    public static boolean flag1 = true, flag2 = true;

    public static void main(String[] args) throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        tokenizer = new StringTokenizer("");

        int n = nextInt();
//        int n = 1;
        while (n-- > 0) {
            solve();
        }

        writer.close();
    }


    static int fun(int a1, int a2, int a3, int a4, int a5) {
        int count = 0;
        if (a3 == a1 + a2) count++;
        if (a4 == a2 + a3) count++;
        if (a5 == a3 + a4) count++;
        return count;
    }

    public static void solve() throws IOException {
        int n = nextInt(), m = nextInt();
        int[] arr = nextIntArray(n), brr = nextIntArray(m);
        PriorityQueue<Long> a = new PriorityQueue<>((x,y)->Long.compare(y,x)), b = new PriorityQueue<>((x,y)->Long.compare(y,x));
        for (int i = 0; i < n; i++) {
            a.add((long) arr[i]);
        }
        for (int i = 0; i < m; i++) {
            b.add((long) brr[i]);
        }
//        writer.println(a+" "+b);

        while (!a.isEmpty() && !b.isEmpty()) {
            if (Objects.equals(a.peek(), b.peek())){
                a.poll();
                b.poll();
            }else {
               long e = b.poll();
               if (e<a.peek() || a.size() <= b.size()){
                   writer.println("NO");
                   return;
               }
               b.add(e/2);
               if (e%2==0){
                   e/=2;
               }else {
                   e/=2;
                   e++;
               }
               b.add(e);
            }
        }

        if (!a.isEmpty() || !b.isEmpty()){
            writer.println("NO");
            return;
        }

        writer.println("YES");
    }

    public static void solv() throws IOException {
        int n = nextInt();
        for (int i = 0; i < 2; i++) {
            writer.print(1 + " ");
        }
        for (int i = 2; i < n - 1; i++) {
            writer.print(i + " ");
        }
        writer.println(1);
    }

    public static boolean isPowerLogarithmic(int x, int y) {
        if (x <= 0 || y <= 0 || y == 1) return false; // Edge cases

        double logResult = Math.log(x) / Math.log(y);
        return Math.abs(logResult - Math.round(logResult)) < 1e-10;
    }


/*
14
10100001000101
15
001110100100001
14
10100101111101

11
01100101101
14
01001111101001
 */

    private static int ask(int l, int r) throws IOException {
        if (r - l + 1 <= 1)
            return 0;
        writer.println(String.valueOf("? ") + String.valueOf(l) + " " + String.valueOf(r));
        writer.flush();
        return nextInt();
    }

    public static int leftmostBit(int n) {
        return (int) Math.pow(2, (int) (Math.log(n) / Math.log(2)));
    }

    public static int leftmostBit(long n) {
        return (int) Math.pow(2, (long) (Math.log(n) / Math.log(2)));
    }

    public static long gcdOfArray(int[] arr) {
        long result = arr[0];
        for (int i = 1; i < arr.length; i++) {
            result = gcd(result, arr[i]);
            if (result == 1)
                break; // Early exit if GCD is 1
        }
        return result;
    }

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

    public static List<Integer> findDivisors(int n) {
        List<Integer> divisors = new ArrayList<>();

        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                divisors.add(i); // Add the divisor
                if (i != n / i) { // Avoid duplicate for perfect squares
                    divisors.add(n / i);
                }
            }
        }

        // Optional: Sort the divisors if needed
        divisors.sort(Integer::compareTo);
        return divisors;
    }

    public static long[] unique(long[] arr) {
        int n = arr.length;
        ArrayList<Long> l1 = new ArrayList<>();
        l1.add(arr[0]);
        for (int i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1]) {
                continue;
            } else {
                l1.add(arr[i]);
            }
        }
        n = l1.size();
        long[] tem = new long[n];
        for (int i = 0; i < n; i++) {
            tem[i] = l1.get(i);
        }
        return tem;
    }

    public static void reverseArray(long[] array) {
        int start = 0, end = array.length - 1;
        while (start < end) {
            long temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
    }

    public static <T> void printArr(int[] arr) {
        for (int e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    public static void printArr(long[] arr) {
        for (long e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    private static int lowerBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

    // Binary search for the last index where arr[index] <= target
    private static int upperBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
//        if (arr[right]>target)return -1;
        return right;
    }

    public static class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Pair{" + "key=" + key + ", value=" + value + '}';
        }
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static String nextLine() throws IOException {
        return reader.readLine();
    }

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

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

    static int[] nextIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    // Function to read the next array of longs
    static long[] nextLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

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


Solution 2 (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;
    }
  }
}