Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 1577 ms):
def diff_elements(l1, l2):
    l1.sort()
    l2.sort()
    j = 0
    k = 0
    L1 = []
    L2 = []
    
    while j < len(l1) and k < len(l2):
        if l1[j] == l2[k]:
            j += 1
            k += 1
        elif l1[j] > l2[k]:
            L2.append(l2[k])
            k += 1
        else:
            L1.append(l1[j])
            j += 1
    
    # Add remaining elements in l1 or l2
    if j < len(l1):
        L1.extend(l1[j:])
    if k < len(l2):
        L2.extend(l2[k:])
    
    return L1, L2

for i in range(int(input())):  # Iterate over multiple test cases
    # Reading the input lists
    l = list(map(int, input().split()))  # This is not being used in the logic.
    l1 = list(map(int, input().split()))
    l2 = list(map(int, input().split()))
 
    # Sort both lists
    l1.sort()
    l2.sort()
    
    L1, L2 = diff_elements(l1, l2)
    
    # Print the differences between lists
    #print(L1)
    #print(L2)
    
    if sum(L1) != sum(L2):
        print("no")
    else:
        a = L1[0] if len(L1) > 0 else 1  # Default to 1 if L1 is empty
        b = L2[0] if len(L2) > 0 else 0  # Default to 0 if L2 is empty
        
        # While loop to adjust elements if needed
        while b >= a or len(L2) < len(L1):
            h = []
            for s in L2:
                if s % 2 == 0:
                    h.append(s // 2)
                    h.append(s // 2)
                else:
                    h.append(s // 2)
                    h.append((s // 2) + 1)
            
            L1, L2 = diff_elements(L1, h)
            
            a = L1[0] if len(L1) > 0 else 1
            b = L2[0] if len(L2) > 0 else 0
        
        # Final check for equality of differences
        L1, L2 = diff_elements(L1, L2)
        
        if len(L1) == 0 and len(L2) == 0:
            print("yes")
        else:
            print("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()