Problem: 2061D - Kevin and Numbers
Language: java

Solution 1 (runtime: 718 ms):

import java.io.*;
import java.util.*;

public class ComTemp2 {

    private static final long MINA = 1;
    private static final long MAXA = 1000000000000L;
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static PrintWriter writer;

    static int MOD = (int) (1e9 + 7);
    static final int MOD1 = 998244353;

    public static Scanner scanner = new Scanner(System.in);

    public static boolean flag1 = true, flag2 = true;

    public static void main(String[] args) throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        tokenizer = new StringTokenizer("");

        int n = nextInt();
//        int n = 1;
        while (n-- > 0) {
            solve();
        }

        writer.close();
    }


    static int fun(int a1, int a2, int a3, int a4, int a5) {
        int count = 0;
        if (a3 == a1 + a2) count++;
        if (a4 == a2 + a3) count++;
        if (a5 == a3 + a4) count++;
        return count;
    }

    public static void solve() throws IOException {
        int n = nextInt(), m = nextInt();
        int[] arr = nextIntArray(n), brr = nextIntArray(m);
        PriorityQueue<Long> a = new PriorityQueue<>((x,y)->Long.compare(y,x)), b = new PriorityQueue<>((x,y)->Long.compare(y,x));
        for (int i = 0; i < n; i++) {
            a.add((long) arr[i]);
        }
        for (int i = 0; i < m; i++) {
            b.add((long) brr[i]);
        }
//        writer.println(a+" "+b);

        while (!a.isEmpty() && !b.isEmpty()) {
            if (Objects.equals(a.peek(), b.peek())){
                a.poll();
                b.poll();
            }else {
               long e = b.poll();
               if (e<a.peek() || a.size() <= b.size()){
                   writer.println("NO");
                   return;
               }
               b.add(e/2);
               if (e%2==0){
                   e/=2;
               }else {
                   e/=2;
                   e++;
               }
               b.add(e);
            }
        }

        if (!a.isEmpty() || !b.isEmpty()){
            writer.println("NO");
            return;
        }

        writer.println("YES");
    }

    public static void solv() throws IOException {
        int n = nextInt();
        for (int i = 0; i < 2; i++) {
            writer.print(1 + " ");
        }
        for (int i = 2; i < n - 1; i++) {
            writer.print(i + " ");
        }
        writer.println(1);
    }

    public static boolean isPowerLogarithmic(int x, int y) {
        if (x <= 0 || y <= 0 || y == 1) return false; // Edge cases

        double logResult = Math.log(x) / Math.log(y);
        return Math.abs(logResult - Math.round(logResult)) < 1e-10;
    }


/*
14
10100001000101
15
001110100100001
14
10100101111101

11
01100101101
14
01001111101001
 */

    private static int ask(int l, int r) throws IOException {
        if (r - l + 1 <= 1)
            return 0;
        writer.println(String.valueOf("? ") + String.valueOf(l) + " " + String.valueOf(r));
        writer.flush();
        return nextInt();
    }

    public static int leftmostBit(int n) {
        return (int) Math.pow(2, (int) (Math.log(n) / Math.log(2)));
    }

    public static int leftmostBit(long n) {
        return (int) Math.pow(2, (long) (Math.log(n) / Math.log(2)));
    }

    public static long gcdOfArray(int[] arr) {
        long result = arr[0];
        for (int i = 1; i < arr.length; i++) {
            result = gcd(result, arr[i]);
            if (result == 1)
                break; // Early exit if GCD is 1
        }
        return result;
    }

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

    public static List<Integer> findDivisors(int n) {
        List<Integer> divisors = new ArrayList<>();

        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                divisors.add(i); // Add the divisor
                if (i != n / i) { // Avoid duplicate for perfect squares
                    divisors.add(n / i);
                }
            }
        }

        // Optional: Sort the divisors if needed
        divisors.sort(Integer::compareTo);
        return divisors;
    }

    public static long[] unique(long[] arr) {
        int n = arr.length;
        ArrayList<Long> l1 = new ArrayList<>();
        l1.add(arr[0]);
        for (int i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1]) {
                continue;
            } else {
                l1.add(arr[i]);
            }
        }
        n = l1.size();
        long[] tem = new long[n];
        for (int i = 0; i < n; i++) {
            tem[i] = l1.get(i);
        }
        return tem;
    }

    public static void reverseArray(long[] array) {
        int start = 0, end = array.length - 1;
        while (start < end) {
            long temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
    }

    public static <T> void printArr(int[] arr) {
        for (int e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    public static void printArr(long[] arr) {
        for (long e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    private static int lowerBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

    // Binary search for the last index where arr[index] <= target
    private static int upperBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
//        if (arr[right]>target)return -1;
        return right;
    }

    public static class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Pair{" + "key=" + key + ", value=" + value + '}';
        }
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static String nextLine() throws IOException {
        return reader.readLine();
    }

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

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    // Function to read the next array of longs
    static long[] nextLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}


Solution 2 (runtime: 812 ms):
import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef
    {
	static FastScanner fs = null;
	public static void main(String[] args) {
		fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		int t = fs.nextInt();
        while(t-->0){
            int n = fs.nextInt();
            int m = fs.nextInt();
            long ar[] = new long[n];
            long br[] = new long[m];
            long sum = (long)0;
            PriorityQueue<Long> pq1 = new PriorityQueue<>(Collections.reverseOrder());
            PriorityQueue<Long> pq2 = new PriorityQueue<>(Collections.reverseOrder());
            for(int i = 0;i<n;i++){
                ar[i] =  fs.nextLong();
                sum+=ar[i];
                pq1.add(ar[i]);
            }
            for(int j = 0;j<m;j++){
                br[j] = fs.nextLong();
                sum-=br[j];
                pq2.add(br[j]);
            }
            //Arrays.sort(ar);
            //Arrays.sort(br);
            boolean res = true;
            if(sum==(long)0){
                while(!pq1.isEmpty() && !pq2.isEmpty()){
                    long n1 = pq1.remove();
                    long n2 = pq2.remove();
                    //out.println((n1)+" -- "+n2);
                    if((pq2.size())>pq1.size()){
                        res = false;
                        break;
                    }
                    if(n1==n2){
                        
                    }
                    else{
                        long md = n2%(long)2;
                        n2/=(long)2;
                        if(md==(long)0){
                            pq2.add(n2);
                            pq2.add(n2);
                        }
                        else{
                            pq2.add(n2);
                            pq2.add((n2+(long)1));
                            
                        }
                        pq1.add((n1));
                    }
                    //out.println(n1+" -- "+n2);
                }
            }
            else{
                res = false;
            }
            if(res)
            out.println("YES");
            else
            out.println("NO");
        }
		out.close();
	}
	static void sort(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);
	}
	static class FastScanner {
		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());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}