Problem: 2061D - Kevin and Numbers
Language: java

Solution 1 (runtime: 936 ms):
import java.io.*;
import java.security.KeyStore.Entry;
import java.util.*;

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Solution 2 (runtime: 796 ms):
import java.io.*;
import java.util.*;

public class Main {
    public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
    public static void main(String args[]){
        try {
            PrintWriter o = new PrintWriter(System.out);
            boolean multiTest = true;
            // init
            if(multiTest) {
                int t = nextInt(), loop = 0;
                while (loop < t) {loop++;solve(o);}
            } else solve(o);
            o.close();
        } catch (Exception e) {e.printStackTrace();}
    }
    static void solve(PrintWriter o) {
        try {
            int n = nextInt(), m = nextInt(), k = n-m;
            int[] a = new int[n];
            int[] b = new int[m];
            Map<Integer, Integer> map = new HashMap<>();
            for(int i=0;i<n;i++) {
                a[i] = nextInt();
                map.put(a[i], map.getOrDefault(a[i], 0)+1);
            }
            PriorityQueue<Integer> pq = new PriorityQueue<>((x,y)->Integer.compare(y,x));
            for(int i=0;i<m;i++) {
                b[i] = nextInt();
                pq.offer(b[i]);
            }
            while(!pq.isEmpty()) {
                int x = pq.poll();
                if(map.get(x) != null) {
                    map.put(x, map.get(x)-1);
                    if(map.get(x) == 0) map.remove(x);
                }
                else if(k-- > 0) {
                    pq.offer(x/2);
                    pq.offer((x+1)/2);
                }
                else {
                    o.println("No");
                    return;
                }
            }
            if(k > 0) o.println("No");
            else o.println("Yes");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static int lower_bound(Integer[] a, long val){
        int l = 0, r = a.length;
        while(l < r){
            int mid = l + (r - l) / 2;
            if(a[mid] < val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int lower_bound(List<Integer> li, long val){
        int l = 0, r = li.size();
        while(l < r){
            int mid = l + (r - l) / 2;
            if(li.get(mid) < val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int upper_bound(long[] preSum, long val){
        int l = 0, r = preSum.length;
        while(l < r){
            int mid = l + (r - l) / 2;
            if(preSum[mid] <= val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int upper_bound(List<int[]> li, long val){
        int l = 0, r = li.size();
        while(l < r){
            int mid = l + (r - l) / 2;
            if(li.get(mid)[0] <= val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int upper_bound(List<Integer> li, int val){
        int l = 0, r = li.size();
        while(l < r){
            int mid = l + (r - l) / 2;
            if(li.get(mid) <= val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int upper_bound(int[] a, long val){
        int l = 0, r = a.length;
        while(l < r){
            int mid = l + (r - l) / 2;
            if(a[mid] <= val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int lower_bound(List<int[]> li, int val){
        int l = 0, r = li.size();
        while(l < r){
            int mid = l + (r - l) / 2;
            if(li.get(mid)[0] < val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int lower_bound(int[] a, int val){
        int l = 0, r = a.length;
        while(l < r){
            int mid = l + (r - l) / 2;
            if(a[mid] < val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static int lower_bound(long[][] a, long val){
        int l = 0, r = a.length;
        while(l < r){
            int mid = l + (r - l) / 2;
            if(a[mid][0] < val) l = mid + 1;
            else r = mid;
        }
        return l;
    }
    public static long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a%b);
    }
    public static long[] extgcd(long a, long b) {
        if(b == 0) return new long[]{1, 0};
        long[] it = extgcd(b, a%b);
        long x = it[1], y = it[0];
        y -= a/b*x;
        return new long[]{x, y};
    }
    public static long lcm(long a, long b){
        return a / gcd(a,b)*b;
    }
    public static long qpow(long a, long n){
        long ret = 1l;
        while(n > 0){
            if((n & 1) == 1) ret = ret * a;
            n >>= 1;
            a = a * a;
        }
        return ret;
    }
    public static long qpow(long a, long n, int md){
        a %= md;
        long ret = 1l;
        while(n > 0){
            if((n & 1) == 1) ret = ret * a % md;
            n >>= 1;
            a = a * a % md;
        }
        return ret;
    }
    public static class FenWick {
        int n;
        long[] a;
        long[] tree;
        public FenWick(int n){
            this.n = n;
            a = new long[n+1];
            tree = new long[n+1];
        }
        private void add(int x, long val){
            while(x <= n){
                tree[x] += val;
                x += x&-x;
            }
        }
        private void addMx(int x, long val) {
            a[x] += val;
            tree[x] = a[x];
            while(x <= n) {
                for(int i=1;i<(x&-x);i<<=1) tree[x] = Math.max(tree[x], tree[x-i]);
                x += x&-x;
            }
        }
        private void addMn(int x, long val) {
            a[x] += val;
            tree[x] = a[x];
            while(x <= n) {
                for(int i=1;i<(x&-x);i<<=1) tree[x] = Math.min(tree[x], tree[x-i]);
                x += x&-x;
            }
        }
        private long query(int x){
            long ret = 0l;
            while(x > 0){
                ret += tree[x];
                x -= x&-x;
            }
            return ret;
        }
        private long queryMx(int l, int r) {
            long res = 0l;
            while(l <= r) {
                if(r-(r&-r) >= l) {
                    res = Math.max(res, tree[r]);
                    r -= r&-r;
                }
                else {
                    res = Math.max(res, a[r]);
                    r--;
                }
            }
            return res;
        }
        private long queryMn(int l, int r) {
            long res = 1l<<55;
            while(l <= r) {
                if(r-(r&-r) >= l) {
                    res = Math.min(res, tree[r]);
                    r -= r&-r;
                }
                else {
                    res = Math.min(res, a[r]);
                    r--;
                }
            }
            return res;
        }
    }
    static class Pair {
        Integer p1, p2;
        public Pair(Integer p1, Integer p2) {
            this.p1 = p1;
            this.p2 = p2;
        }
        @Override
        public int hashCode() {
            int prime = 31, ret = 0;
            ret = ret*prime+p1.hashCode();
            ret = ret*prime+p2.hashCode();
            return ret;
        }
        @Override
        public boolean equals(Object o) {
            if(o instanceof Pair pair) {
                return p1.equals(pair.p1) && p2.equals(pair.p2);
            }
            return false;
        }
    }
    static class DSU {
        int[] parent;
        int[] size;
        public DSU(int n) {
            parent = new int[n];
            size = new int[n];
            for(int i=0;i<n;i++) {
                parent[i] = i;
                size[i] = 1;
            }
        }
        public int find(int x) {
            if(parent[x] != x) parent[x] = find(parent[x]);
            return parent[x];
        }
        public void union(int x, int y) {
            int root_x = find(x);
            int root_y = find(y);
            if(root_x == root_y) return;
            parent[root_x] = root_y;
            size[root_y] += size[root_x];
            size[root_x] = 0;
        }
    }
    private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static StringTokenizer tokenizer = new StringTokenizer("");
    private static String next() throws IOException{
        while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
        return tokenizer.nextToken();
    }
    public static int nextInt() throws IOException {return Integer.parseInt(next());}
    public static Long nextLong() throws IOException {return Long.parseLong(next());}
    public static double nextDouble() throws IOException {return Double.parseDouble(next());}
    public static char nextChar() throws IOException {return next().toCharArray()[0];}
    public static String nextString() throws IOException {return next();}
    public static String nextLine() throws IOException {return reader.readLine();}
}