Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 874 ms):
import sys

input = sys.stdin.readline

from collections import Counter



from random import getrandbits



RANDOM = getrandbits(32)



class Int(int):

    def __init__(self, x):

        int.__init__(x)

    def __hash__(self):

        return super(Int, self).__hash__() ^ RANDOM



def find(x):

    if cnt[Int(x)]:

        cnt[Int(x)] -= 1

        return True

    if x == 0 or x == 1: return False

    

    return find((x + 1) // 2) and find(x // 2)



def solve():

    global cnt

    n, m = map(int, input().split())

    a = list(map(Int, input().split()))

    b = list(map(int, input().split()))

    b.sort(reverse=True)



    if sum(a) != sum(b):

        return False

    

    cnt = Counter(a)

    for i in range(m):

        if cnt[Int(b[i])]:

            cnt[Int(b[i])] -= 1

            continue

        if not find(b[i]):

            return False



    return max(cnt.values()) == 0



for _ in range(int(input())):

    print("Yes" if solve() else "No")

Solution 2 (runtime: 702 ms):
import heapq

import sys



# Use fast I/O

readline = sys.stdin.readline



def solve():

    """

    Solves a single test case for the Kevin and Numbers problem.

    Reads the sequences A and B, checks if A can be transformed into B

    using the specified operation, and prints "Yes" or "No".



    The core idea is to simulate the process in reverse. We start with sequence B

    and apply the reverse operation: split an integer `z` into two integers `x, y`

    such that `z = x+y` and `|x-y| <= 1`. This splitting rule is deterministic:

    - If `z` is even and `z >= 2`, it splits into `z/2, z/2`.

    - If `z` is odd and `z >= 3`, it splits into `(z-1)/2, (z+1)/2`.

    - `z=1` cannot be split into two positive integers satisfying the condition.

    

    We want to know if we can reach sequence A after exactly N-M splits starting from B.

    We use max heaps to manage the elements and greedily match or split the largest elements.

    Max heaps are implemented using Python's `heapq` module (which provides min heaps)

    by storing negative values.

    """

    N, M = map(int, readline().split())

    A = list(map(int, readline().split()))

    B = list(map(int, readline().split()))



    # A necessary condition for the transformation is that the sum of elements must be invariant.

    # The operation replaces x, y with x+y, changing the total sum by (x+y) - (x+y) = 0.

    # This appears wrong, the operation removes two numbers x,y and adds one number x+y.

    # The sum changes from S to S - x - y + (x+y) = S. The sum IS invariant.

    if sum(A) != sum(B):

        print("No")

        return



    # Initialize max heap for the target sequence A. Stores -x for each x in A.

    pq_a = [-x for x in A]

    heapq.heapify(pq_a)

    

    # Initialize max heap for the current sequence, starting with B. Stores -x for each x in B.

    # Elements in pq_b will be split progressively.

    pq_b = [-x for x in B]

    heapq.heapify(pq_b)



    # Process elements using the heaps. Continue as long as there are elements in B's heap.

    while pq_b:

        # Pop the largest element from B's current representation (-ve value from min-heap).

        top_b_neg = heapq.heappop(pq_b)

        top_b = -top_b_neg # Convert back to positive value



        # Check if A's heap is empty. If so, all elements of A have been matched/accounted for.

        # If B's heap still has elements (like top_b we just popped), it indicates a mismatch.

        # The sums matched initially, so this state means B's structure requires more elements

        # or different elements than A provides.

        if not pq_a:

             print("No")

             return



        # Peek the largest element currently required by A (target sequence).

        top_a_neg = pq_a[0]

        top_a = -top_a_neg



        # If the largest element available from B's transformation (top_b) is smaller than

        # the largest element required by A (top_a), then it's impossible to form A.

        # The reverse splitting process only generates numbers less than or equal to the parent.

        if top_b < top_a:

            print("No")

            return



        # If the largest elements match (top_b == top_a), it signifies that this element `top_a`

        # from A corresponds to `top_b` derived from B (potentially after some splits).

        # We consume `top_a` by popping it from A's heap.

        if top_b == top_a:

            heapq.heappop(pq_a)

            # Continue to the next largest element in B's heap.

            continue 



        # Case: top_b > top_a

        # The element `top_b` is larger than the current target `top_a`.

        # This implies `top_b` must have been formed by a sum operation in the forward process.

        # We reverse this by splitting `top_b` into its unique constituents (x, y) based on the rules.

        

        # An element of 1 cannot be split into two positive integers satisfying the rules.

        # If top_b is 1, it must match top_a == 1. This would be handled by the `top_b == top_a` case.

        # Reaching this point (`top_b > top_a`) with top_b=1 is logically impossible

        # because the smallest possible value for top_a is 1.

        if top_b == 1:

             # Include this check for logical consistency, though it should not be reached.

             print("No") 

             return



        # Perform the split based on the parity of top_b.

        # The rules ensure children x, y are positive integers.

        if top_b % 2 == 0:

            # Even number z (>= 2) splits into z/2, z/2.

            x = top_b // 2

            y = top_b // 2

        else: 

            # Odd number z (>= 3) splits into (z-1)/2, (z+1)/2.

            x = top_b // 2 

            y = top_b // 2 + 1

            

        # Add the two resulting numbers (children) back into B's heap.

        # Store them as negative values for the max heap simulation.

        heapq.heappush(pq_b, -x)

        heapq.heappush(pq_b, -y)

        

    # After the loop finishes, B's heap (pq_b) is empty.

    # For a valid transformation, A's heap (pq_a) must also be empty at this point.

    # This means all elements required by A have been matched.

    if not pq_a:

        # Both heaps are empty. The transformation is possible.

        print("Yes")

    else:

        # B's heap is empty, but A still requires elements. Mismatch.

        print("No")



# Read the number of test cases from standard input.

T = int(readline())

# Process each test case by calling the solve function.

for _ in range(T):

    solve()