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: 1031 ms):
import java.util.*;

public class D_Kevin_and_Numbers {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);

        int t=sc.nextInt();

        while(t-->0){

        int n=sc.nextInt();

        int m=sc.nextInt();

       HashMap<Integer,Integer> arr=new HashMap<>();

       int brr[]=new int[m];

        for(int i=0;i<n;i++) {

            int num=sc.nextInt();

            arr.put(num,arr.getOrDefault(num,0)+1);

            

        }

        for(int i=0;i<m;i++){

            brr[i]=sc.nextInt();

        }



        boolean flag=true;

        for(int i=0;i<m;i++){

            HashMap<Integer,Integer> map=new HashMap<>();

            int ans[]={0};

           dfs(brr[i],arr,map,ans);

           if(ans[0]==1) {

            flag=false;

            break;

           }

           for(int num:map.keySet()){

            arr.put(num,arr.get(num)-map.get(num));

            if(arr.get(num)==0) arr.remove(num);

           }

        }

        if(flag && arr.isEmpty()) System.out.println("Yes");

        else System.out.println("No");

        



        

       



        }   

    }

    public static void dfs(int target,HashMap<Integer,Integer> arr,HashMap<Integer,Integer>map,int[] ans){

        if(ans[0]==1) return;

        if(target==0) {

            ans[0]=1;

        }

        if(arr.containsKey(target) && arr.get(target)>map.getOrDefault(target, 0)){

            map.put(target,map.getOrDefault(target, 0)+1);

        }

        else{

            if(target==1){

                ans[0]=1;

                return;

            }

            dfs(target/2,arr,map,ans);

            dfs(target-target/2,arr,map,ans);



        }

    }

}