Problem: 2061D - Kevin and Numbers
Language: Java 8

Previous submission (verdict: Time limit exceeded on test 1):
import java.io.*;

import java.util.*;



public class Main {

    public static void main(String[] args) throws IOException {

        FastReader in = new FastReader();

        PrintWriter out = new PrintWriter(System.out);



        int t = in.nextInt();

        while (t-- > 0) {

            int n = in.nextInt(), m = in.nextInt();

            // Read a[] into freq map

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

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

                int x = in.nextInt();

                need.put(x, need.getOrDefault(x, 0) + 1);

            }

            // Max‑heap for b[]

            PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

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

                pq.add(in.nextInt());

            }



            boolean ok = true;

            // Process until heap is empty

            while (!pq.isEmpty()) {

                int x = pq.poll();

                Integer cnt = need.get(x);

                if (cnt != null && cnt > 0) {

                    // match and consume

                    if (cnt == 1) need.remove(x);

                    else need.put(x, cnt - 1);

                } else if (x == 1) {

                    // can't split 1 any further, and it's not needed

                    ok = false;

                    break;

                } else {

                    // split x into two parts that differ by <=1

                    int lo = x / 2;

                    int hi = x - lo;

                    pq.add(lo);

                    pq.add(hi);

                }

            }



            // must have matched every a[i]

            out.println(ok && need.isEmpty() ? "Yes" : "No");

        }

        out.flush();

    }



    static class FastReader {

        BufferedReader br;

        StringTokenizer st;

        FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }

        String next() throws IOException {

            while (st == null || !st.hasMoreTokens()) {

                String line = br.readLine();

                if (line == null) return null;

                st = new StringTokenizer(line);

            }

            return st.nextToken();

        }

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

    }

}



Submission (verdict: Time limit exceeded on test 1):
import java.io.*;

import java.util.*;



public class Main {

    public static void main(String[] args) throws IOException {

        FastReader in = new FastReader();

        PrintWriter out = new PrintWriter(System.out);



        int t = in.nextInt();

        while (t-- > 0) {

            int n = in.nextInt(), m = in.nextInt();

            TreeMap<Integer, Integer> need = new TreeMap<>();

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

                int x = in.nextInt();

                need.put(x, need.getOrDefault(x, 0) + 1);

            }



            PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

            for (int i = 0; i < m; i++) pq.add(in.nextInt());



            boolean ok = true;

            while (!pq.isEmpty()) {

                int x = pq.poll();

                Integer floor = need.floorKey(x);

                if (floor != null && floor.equals(x)) {

                    // match and remove one occurrence

                    if (need.get(x) == 1) need.remove(x);

                    else need.put(x, need.get(x) - 1);

                } else if (x == 1) {

                    ok = false;

                    break;

                } else {

                    pq.add(x / 2);

                    pq.add((x + 1) / 2);

                }

            }

            out.println(ok && need.isEmpty() ? "Yes" : "No");

        }

        out.flush();

    }



    static class FastReader {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st;

        String next() throws IOException {

            while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());

            return st.nextToken();

        }

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

    }

}