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: 765 ms):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static boolean isprime(long x) {
        if (x == 2 || x == 3 || x == 5) {
            return true;
        }
        for (long i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static long gcd(long a, long b) {
        if (a == 0) {
            return b;
        }
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    static void sort(long[] a) {
        ArrayList<Long> l = new ArrayList<>();
        for (long i : a) {
            l.add(i);
        }
        Collections.sort(l);
        for (int i = 0; i < a.length; i++) {
            a[i] = l.get(i);
        }
    }

    static void sortInt(int[] a) {
        ArrayList<Integer> l = new ArrayList<>();
        for (int i : a) {
            l.add(i);
        }
        Collections.sort(l);
        for (int i = 0; i < a.length; i++) {
            a[i] = l.get(i);
        }
    }

    public static class pair implements Comparable<pair> {

        int x;
        int y;

        public pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(pair a) {
            return this.x - a.x;
        }
    }

    public static class pp implements Comparable<pp> {

        long x;
        int y;

        public pp(long x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(pp a) {
            return this.y - a.y; // Corrected the order of comparison
        }
    }

    static Comparator<int[]> customComparator = new Comparator<int[]>() {
        @Override
        public int compare(int[] a, int[] b) {
            // First compare based on the values at index 0
            if (a[0] != b[0]) {
                return a[0] - b[0];
            } else {
                // If values at index 0 are equal, compare based on a[i][1] - a[i][0]
                return a[1] - a[0] - (b[1] - b[0]);
            }
        }
    };

    static long mod = 1_000_000_000 + 7;

    static long lm(long i, long lcm) {
        return (i * (lcm)) / (gcd(i, lcm));
    }

    static long fact[] = new long[200001];

    static long bipow(long x, long y) {
        long z = 1;
        while (y != 0) {
            if ((y & 1) != 0) {
                z = mul(z, x);
            }
            x = mul(x, x);
            y >>= 1;
        }
        return z;
    }

    // static long fact[];
    static long mul(long n, long m) {
        return (n * m) % mod;
    }

    public static long inver(long x) {
        return bipow(x, mod - 2);
    }

    public static long find_combin(long m, long n) {
        long y = mul(fact[(int) (m - n)], fact[(int) n]);
        return mul(fact[(int) m], inver(y));
    }

    public static long modularExponentiation(long a, long b, long m) {
        long result = 1;
        a = a % m; // Ensure 'a' is within the modulo range

        while (b > 0) {
            // If b is odd, multiply a with the result
            if ((b & 1) == 1) {
                result = (result * a) % m;
            }
            // Square a and halve b
            a = (a * a) % m;
            b >>= 1;
        }
        return result;
    }

    public static long modularInverse(long a, long m) {
        return modularExponentiation(a, m - 2, m);
    }
    static HashMap<Integer, Integer> map;

    public static void main(String[] args) {
        Scanner sc = new Scanner();

        int testcase = sc.nextInt();

        for (int ttt = 0; ttt < testcase; ttt++) {

            int n = sc.nextInt();

            int m = sc.nextInt();

            int arr[] = sc.readArray(n);
            int brr[] = sc.readArray(m);


            map = new HashMap<>();


            for (int i = 0; i < n; i++) {
                map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
            }

            int op = n - m;

            PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());


            for (int i = 0; i < m; i++) {
                pq.add(brr[i]);
            }

            boolean tt = true;

            while (op > 0 && pq.size() > 0 && map.size()>0) {
                int r = pq.remove();
                if (map.containsKey(r)) {
                    map.put(r, map.get(r) - 1);
                    if (map.get(r) == 0) {
                        map.remove(r);
                    }
                    continue;
                }
                else{
                    if(r==1){
                        tt = false;
                        break;
                    }
                    if(r%2==0){
                        pq.add(r/2);
                        pq.add(r/2);
                    }
                    else{
                        pq.add(r/2);
                        pq.add(r/2 + 1);
                    }
                }
                op--;
            }
            if(!tt){
                System.out.println("NO");continue;
            }

            while(!pq.isEmpty()){
                int r = pq.remove();
                if (map.containsKey(r)) {
                    map.put(r, map.get(r) - 1);
                    if (map.get(r) == 0) {
                        map.remove(r);
                    }
                }
                else {
                    tt = false;
                    break;
                }
            }

            if(map.size()>0){
                tt = false;
            }

            if(!tt){
                System.out.println("NO");continue;
            }

            System.out.println("YES");

        }

    }

    // 18 =  9 9 
    static class Scanner {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next() {
            while (!st.hasMoreTokens()) try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

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

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

        int[] readArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        long[] readLong(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }
    }
}