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

Previous submission (verdict: Time limit exceeded on test 6):

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.*;

    public class Main {

        public static boolean isprime(long x) {
            if (x == 2 || x == 3 || x == 5) {
                return true;
            }
            for (long i = 2; i <= Math.sqrt(x); i++) {
                if (x % i == 0) {
                    return false;
                }
            }
            return true;
        }

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

        static void sort(long[] a) {
            ArrayList<Long> l = new ArrayList<>();
            for (long i : a) {
                l.add(i);
            }
            Collections.sort(l);
            for (int i = 0; i < a.length; i++) {
                a[i] = l.get(i);
            }
        }

        static void sortInt(int[] a) {
            ArrayList<Integer> l = new ArrayList<>();
            for (int i : a) {
                l.add(i);
            }
            Collections.sort(l);
            for (int i = 0; i < a.length; i++) {
                a[i] = l.get(i);
            }
        }

        public static class pair implements Comparable<pair> {

            int x;
            int y;

            public pair(int x, int y) {
                this.x = x;
                this.y = y;
            }

            @Override
            public int compareTo(pair a) {
                return this.x - a.x;
            }
        }

        public static class pp implements Comparable<pp> {

            long x;
            int y;

            public pp(long x, int y) {
                this.x = x;
                this.y = y;
            }

            @Override
            public int compareTo(pp a) {
                return this.y - a.y; // Corrected the order of comparison
            }
        }

        static Comparator<int[]> customComparator = new Comparator<int[]>() {
            @Override
            public int compare(int[] a, int[] b) {
                // First compare based on the values at index 0
                if (a[0] != b[0]) {
                    return a[0] - b[0];
                } else {
                    // If values at index 0 are equal, compare based on a[i][1] - a[i][0]
                    return a[1] - a[0] - (b[1] - b[0]);
                }
            }
        };

        static long mod = 1_000_000_000 + 7;

        static long lm(long i, long lcm) {
            return (i * (lcm)) / (gcd(i, lcm));
        }

        static long fact[] = new long[200001];

        static long bipow(long x, long y) {
            long z = 1;
            while (y != 0) {
                if ((y & 1) != 0) {
                    z = mul(z, x);
                }
                x = mul(x, x);
                y >>= 1;
            }
            return z;
        }

        // static long fact[];
        static long mul(long n, long m) {
            return (n * m) % mod;
        }

        public static long inver(long x) {
            return bipow(x, mod - 2);
        }

        public static long find_combin(long m, long n) {
            long y = mul(fact[(int) (m - n)], fact[(int) n]);
            return mul(fact[(int) m], inver(y));
        }

        public static long modularExponentiation(long a, long b, long m) {
            long result = 1;
            a = a % m; // Ensure 'a' is within the modulo range

            while (b > 0) {
                // If b is odd, multiply a with the result
                if ((b & 1) == 1) {
                    result = (result * a) % m;
                }
                // Square a and halve b
                a = (a * a) % m;
                b >>= 1;
            }
            return result;
        }

        public static long modularInverse(long a, long m) {
            return modularExponentiation(a, m - 2, m);
        }
        static HashMap<Integer, Integer> map;

        public static void main(String[] args) {
            Scanner sc = new Scanner();

            int testcase = sc.nextInt();

            for (int ttt = 0; ttt < testcase; ttt++) {

                int n = sc.nextInt();

                int m = sc.nextInt();

                int arr[] = sc.readArray(n);
                int brr[] = sc.readArray(m);

                Arrays.sort(arr);
                Arrays.sort(brr);

                map = new HashMap<>();
                for (int i = 0; i < n; i++) {
                    map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
                }

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

                int j = 0;
                t = true;

                for (int i = 0; i < m; i++) {
                    int x = brr[i];
                    find_if(x, 1);
                    if (!t) {
                        break;
                    }
                }
                if (t && map.size() == 0) {
                    System.out.println("Yes");
                } else {
                    System.out.println("No");
                }
            }

        }

        // 18 =  9 9 
        static boolean t = true;

        static void find_if(int x, int c) {
             if(c<0){
                t = false;return;
             }
            if (map.containsKey(x)) {
                int cc = map.get(x);
                if (cc > c) {
                    map.put(x, cc - c);
                    return;
                } 
                else if (cc == c) {
                    map.remove(x);
                    return;
                } 
                else {
                    c = c - cc;
                    map.remove(x);
                }
            }
            if (map.size() == 0) {
                t = false;
                return;
            }
            if (x == 1) {
                t = false;
                return;
            }
            // 10 -> 5 , 5 2 3 2 3 1 2

            int y = x / 2;

            if (x % 2 == 0) {
                find_if(y, c * 2);
            } else {
                find_if(y, c);
                find_if(y + 1, c);
            }
            return;
        }

        static class Scanner {

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer("");

            String next() {
                while (!st.hasMoreTokens()) 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());
            }

            int[] readArray(int n) {
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                    arr[i] = nextInt();
                }
                return arr;
            }

            long[] readLong(int n) {
                long[] arr = new long[n];
                for (int i = 0; i < n; i++) {
                    arr[i] = nextLong();
                }
                return arr;
            }
        }
    }


Submission (verdict: Accepted):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static boolean isprime(long x) {
        if (x == 2 || x == 3 || x == 5) {
            return true;
        }
        for (long i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }

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

    static void sort(long[] a) {
        ArrayList<Long> l = new ArrayList<>();
        for (long i : a) {
            l.add(i);
        }
        Collections.sort(l);
        for (int i = 0; i < a.length; i++) {
            a[i] = l.get(i);
        }
    }

    static void sortInt(int[] a) {
        ArrayList<Integer> l = new ArrayList<>();
        for (int i : a) {
            l.add(i);
        }
        Collections.sort(l);
        for (int i = 0; i < a.length; i++) {
            a[i] = l.get(i);
        }
    }

    public static class pair implements Comparable<pair> {

        int x;
        int y;

        public pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(pair a) {
            return this.x - a.x;
        }
    }

    public static class pp implements Comparable<pp> {

        long x;
        int y;

        public pp(long x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(pp a) {
            return this.y - a.y; // Corrected the order of comparison
        }
    }

    static Comparator<int[]> customComparator = new Comparator<int[]>() {
        @Override
        public int compare(int[] a, int[] b) {
            // First compare based on the values at index 0
            if (a[0] != b[0]) {
                return a[0] - b[0];
            } else {
                // If values at index 0 are equal, compare based on a[i][1] - a[i][0]
                return a[1] - a[0] - (b[1] - b[0]);
            }
        }
    };

    static long mod = 1_000_000_000 + 7;

    static long lm(long i, long lcm) {
        return (i * (lcm)) / (gcd(i, lcm));
    }

    static long fact[] = new long[200001];

    static long bipow(long x, long y) {
        long z = 1;
        while (y != 0) {
            if ((y & 1) != 0) {
                z = mul(z, x);
            }
            x = mul(x, x);
            y >>= 1;
        }
        return z;
    }

    // static long fact[];
    static long mul(long n, long m) {
        return (n * m) % mod;
    }

    public static long inver(long x) {
        return bipow(x, mod - 2);
    }

    public static long find_combin(long m, long n) {
        long y = mul(fact[(int) (m - n)], fact[(int) n]);
        return mul(fact[(int) m], inver(y));
    }

    public static long modularExponentiation(long a, long b, long m) {
        long result = 1;
        a = a % m; // Ensure 'a' is within the modulo range

        while (b > 0) {
            // If b is odd, multiply a with the result
            if ((b & 1) == 1) {
                result = (result * a) % m;
            }
            // Square a and halve b
            a = (a * a) % m;
            b >>= 1;
        }
        return result;
    }

    public static long modularInverse(long a, long m) {
        return modularExponentiation(a, m - 2, m);
    }
    static HashMap<Integer, Integer> map;

    public static void main(String[] args) {
        Scanner sc = new Scanner();

        int testcase = sc.nextInt();

        for (int ttt = 0; ttt < testcase; ttt++) {

            int n = sc.nextInt();

            int m = sc.nextInt();

            int arr[] = sc.readArray(n);
            int brr[] = sc.readArray(m);


            map = new HashMap<>();


            for (int i = 0; i < n; i++) {
                map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
            }

            int op = n - m;

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


            for (int i = 0; i < m; i++) {
                pq.add(brr[i]);
            }

            boolean tt = true;

            while (op > 0 && pq.size() > 0 && map.size()>0) {
                int r = pq.remove();
                if (map.containsKey(r)) {
                    map.put(r, map.get(r) - 1);
                    if (map.get(r) == 0) {
                        map.remove(r);
                    }
                    continue;
                }
                else{
                    if(r==1){
                        tt = false;
                        break;
                    }
                    if(r%2==0){
                        pq.add(r/2);
                        pq.add(r/2);
                    }
                    else{
                        pq.add(r/2);
                        pq.add(r/2 + 1);
                    }
                }
                op--;
            }
            if(!tt){
                System.out.println("NO");continue;
            }

            while(!pq.isEmpty()){
                int r = pq.remove();
                if (map.containsKey(r)) {
                    map.put(r, map.get(r) - 1);
                    if (map.get(r) == 0) {
                        map.remove(r);
                    }
                }
                else {
                    tt = false;
                    break;
                }
            }

            if(map.size()>0){
                tt = false;
            }

            if(!tt){
                System.out.println("NO");continue;
            }

            System.out.println("YES");

        }

    }

    // 18 =  9 9 
    static class Scanner {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next() {
            while (!st.hasMoreTokens()) 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());
        }

        int[] readArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        long[] readLong(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }
    }
}