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

public class sub {

	//can i get expert by summer please
	
	static int mod = 1000000007;

	public static void main(String args[]) throws IOException {
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			int m = sc.nextInt();
			int a[] = new int[n];
			for(int i = 0;i<n;i++) {
				a[i]=sc.nextInt();
			}
			int b[] = new int[m];
			PriorityQueue<Integer> pq =new PriorityQueue(Collections.reverseOrder());
			for(int i = 0;i<m;i++) {
				b[i]=sc.nextInt();
				pq.offer(b[i]);
			}
			int moves = n-m;
			int pointer = n-1;
			Arrays.sort(a);
			while(moves>=0 && pointer!=-1 && pq.size()!=0) {
				if(a[pointer]==pq.peek()) {
					pq.poll();
					pointer--;
				}
				else {
					int cur = pq.poll();
					pq.offer(cur/2);
					pq.offer((cur+1)/2);
					moves--;
				}
			}
			if(moves==-1 || pointer!=-1) {
				out.println("NO");
			}
			else {
				out.println("YES");
			}
		}
		out.close();
	}

//	static long bincoef(int n, int k) {
//	    if (n < k) return 0L;
//	    return (p[n] * pow((p[n - k] * p[k]) % mod, mod - 2, mod)) % mod;
//	}

	static long pow(long a, long b, long mod) {
		if (b == 0) {
			return 1L;
		}
		long val = pow(a, b / 2, mod);
		if (b % 2 == 0) {
			return val * val % mod;
		}
		return val * val % mod * a % mod;
	}

	static long gcd(long a, long b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	static long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}

	static Kattio sc = new Kattio();
	static PrintWriter out = new PrintWriter(System.out);

	static class Kattio {

		static BufferedReader r;
		static StringTokenizer st;

		public Kattio() {
			r = new BufferedReader(new InputStreamReader(System.in));
		}

		public String next() {
			try {
				while (st == null || !st.hasMoreTokens()) {
					st = new StringTokenizer(r.readLine());
				}
				return st.nextToken();
			} catch (Exception e) {
				return null;
			}
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}