Problem: 2061D - Kevin and Numbers
Language: java

Solution 1 (runtime: 890 ms):
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        int t = in.nextInt();
        while (t-- > 0) {
            int n = in.nextInt(), m = in.nextInt();
            long sumA = 0;
            HashMap<Long, Long> freqA = new HashMap<>();
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextLong();
                sumA += a[i];
                freqA.put(a[i], freqA.getOrDefault(a[i], 0L) + 1);
            }
            long sumB = 0;
            long[] b = new long[m];
            for (int i = 0; i < m; i++) {
                b[i] = in.nextLong();
                sumB += b[i];
            }
            if (sumA != sumB) {
                out.println("No");
                continue;
            }
            if (n == m) {
                HashMap<Long, Long> freqB = new HashMap<>();
                for (long x : b)
                    freqB.put(x, freqB.getOrDefault(x, 0L) + 1);
                out.println(freqA.equals(freqB) ? "Yes" : "No");
                continue;
            }
            TreeMap<Long, Long> mp = new TreeMap<>(Collections.reverseOrder());
            for (long x : b)
                mp.put(x, mp.getOrDefault(x, 0L) + 1);
            boolean ok = true;
            while (!mp.isEmpty()) {
                long cur = mp.firstKey();
                long cnt = mp.get(cur);
                mp.remove(cur);
                if (freqA.containsKey(cur)) {
                    long used = Math.min(freqA.get(cur), cnt);
                    cnt -= used;
                    long left = freqA.get(cur) - used;
                    if (left == 0)
                        freqA.remove(cur);
                    else
                        freqA.put(cur, left);
                }
                if (cnt > 0) {
                    if (cur == 1) {
                        ok = false;
                        break;
                    }
                    long left = cur / 2;
                    long right = (cur + 1) / 2;
                    mp.put(left, mp.getOrDefault(left, 0L) + cnt);
                    mp.put(right, mp.getOrDefault(right, 0L) + cnt);
                }
            }
            out.println(ok && freqA.isEmpty() ? "Yes" : "No");
        }
        out.close();
    }
    
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                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()); }
        double nextDouble() { return Double.parseDouble(next()); }
        String nextLine() {
            String str = "";
            try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
            return str;
        }
    }
}


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();}
}