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