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

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

public class Picky {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            long n=sc.nextLong();
            long m=sc.nextLong();
            List<Long>a=new ArrayList<>(), b=new ArrayList<>();

            for(int i=0; i<n; i++) a.add(sc.nextLong());
            for(int i=0; i<m; i++) b.add(sc.nextLong());

            HashMap<Long, Long> fa=new HashMap<>(), fb=new HashMap<>();
            TreeSet<Long>pa=new TreeSet<>(Collections.reverseOrder());
            TreeSet<Long>pb=new TreeSet<>(Collections.reverseOrder());

            for(long i:a){
                fa.put(i, fa.getOrDefault(i, 0L)+1);
                pa.add(i);
            }
            for(long i:b){
                fb.put(i, fb.getOrDefault(i, 0L)+1);
                pb.add(i);
            }

            while(true){
                if(pa.isEmpty() && pb.isEmpty()){
                    System.out.println("YES");
                    break;
                }
                if(pa.isEmpty() || pb.isEmpty()){
                    System.out.println("NO");
                    break;
                }

                long mxb=pb.first(), mxa=pa.first();
                if(mxa>mxb){
                    System.out.println("NO");
                    break;
                }

                if(fa.getOrDefault(mxb, 0L)>0){
                    long mn=Math.min(fa.get(mxb), fb.get(mxb));
                    fa.put(mxb, fa.get(mxb)-mn);
                    fb.put(mxb, fb.get(mxb)-mn);
                    if(fa.get(mxb)==0) pa.remove(mxb);
                    if(fb.get(mxb)==0) pb.remove(mxb);
                }
                else{
                    long sb =mxb/2, lb=(mxb+1)/2;
                    fb.put(sb, fb.getOrDefault(sb, 0L)+fb.get(mxb));
                    fb.put(lb, fb.getOrDefault(lb, 0L)+fb.get(mxb));
                    fb.put(mxb, 0L);
                    pb.add(sb);
                    pb.add(lb);
                    pb.remove(mxb);
                }
            }
        }
    }
}


Submission (verdict: Accepted):
import java.util.*;

public class Picky {
//    static boolean tcExist=true;

    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        long t=sc.nextLong();
        while(t-->0){
            long n=sc.nextLong();
            long m=sc.nextLong();
            long[] a=new long[(int) n];
            long[] b=new long[(int) m];

            for(int i=0; i<n; i++) a[i]=sc.nextLong();
            for(int i=0; i<m; i++) b[i]=sc.nextLong();
            Arrays.sort(a);
            Arrays.sort(b);

            Map<Long, Long> mpp=new HashMap<>();
            for(long num : a) mpp.put(num, mpp.getOrDefault(num, 0L)+1);

            PriorityQueue<Long> pq=new PriorityQueue<>(Collections.reverseOrder());
            for(long num : b) pq.add(num);

            long c=n-m;
            int j=0;

            while(c>=0 && !pq.isEmpty()){
                if(mpp.isEmpty()) break;
                long ele=pq.poll();

                if(mpp.containsKey(ele)){
                    long count = mpp.get(ele);
                    if(count>1) mpp.put(ele, count-1);
                    else mpp.remove(ele);
                }
                else{
                    if(ele==1){
                        System.out.println("No");
                        return;
                    }
                    long x=ele/2;
                    long y=ele/2;
                    if(ele%2!=0) y++;
                    pq.add(x);
                    pq.add(y);
                    c--;
                }
            }

            if(c>=0 && pq.isEmpty() && mpp.isEmpty()) System.out.println("Yes");
            else System.out.println("No");
        }
    }
}