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: 1608 ms):
import java.util.*;
public class temp{
    static boolean creater(HashMap<Integer,Integer>possess,HashMap<Integer,Integer> create){
        for(int i:create.keySet()){
            if(!have(i,create.get(i),possess)){
                return false;
            }
        }
        return possess.size()==0;            
    }
    static boolean have(int num,int quan,HashMap<Integer,Integer> possess){
        if(num==0)
            return false;
        int has=possess.getOrDefault(num,0);
        if(has>quan){
            possess.put(num,has-quan);
            return true;
        }
        else if(has==quan){
            possess.remove(num);
            return true;
        }
        quan-=has;
        if(has>0){
            possess.remove(num);
        }
        if(num%2==0){
            return have(num/2,quan*2,possess);
        }
        return have(num/2,quan,possess)&&have(num/2+1,quan,possess);

    }
    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();
            int quan,x;
            HashMap<Integer,Integer> possession=new HashMap<>();
            HashMap<Integer,Integer> create=new HashMap<>();
            for(int i=0;i<n;i++){
                x=sc.nextInt();
                possession.put(x,possession.getOrDefault(x,0)+1);
            }
            for(int i=0;i<m;i++){
                x=sc.nextInt();
                quan=possession.getOrDefault(x,0);
                if(quan>1){
                    possession.put(x,quan-1);
                }
                else if(quan==1){
                    possession.remove(x);
                }
                else{
                    create.put(x,create.getOrDefault(x,0)+1);
                }
            }
            if(creater(possession,create))
                System.out.println("YES");
            else
                System.out.println("NO");
        }
    }
}