Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 843 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: 640 ms):
import sys

import string

import math

import array

import bisect

from heapq import heappop,heappush

from functools import lru_cache,cache

from collections import deque,defaultdict,Counter

from itertools import permutations,combinations

#sys.setrecursionlimit(10**5)

input = sys.stdin.readline

printf = lambda x:sys.stdout.write(x)



INF = 10**15



RI = lambda: int(sys.stdin.readline())

RF = lambda: float(sys.stdin.readline())

RS = lambda: sys.stdin.readline().strip()

RFF = lambda: map(float, sys.stdin.readline().split())

RII = lambda: map(int, sys.stdin.readline().split())

RSS = lambda: map(str, sys.stdin.readline().strip().split())

RIL = lambda: list(RII())

RFL = lambda: list(RFF())

RSL = lambda: list(RSS())



from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):

        if stack:

            return f(*args, **kwargs)

        else:

            to = f(*args, **kwargs)

            while True:

                if type(to) is GeneratorType:

                    stack.append(to)

                    to = next(to)

                else:

                    stack.pop()

                    if not stack:

                        break

                    to = stack[-1].send(to)

            return to



    return wrappedfunc



def main():

    test = RI()

    

    for i in range(test):

        n,m = RII()

        a = RIL()

        b = RIL()

        #db = Counter(b)

        #a.sort()

        da = Counter([str(i) for i in a])

        

        # @bootstrap

        # def dp(i):

        #     if i==1:

        #         da[i] -= 1

        #         yield None

        #     if da[i]>=1:

        #         da[i] -= 1

        #         yield None

        #     x = i//2

        #     y = x+i%2

        #     yield dp(x)

        #     yield dp(y)

        #     yield None

        cnt = 0

        @bootstrap

        def dp(i):

            nonlocal cnt

            if cnt>=n:

                da[str(0)] -= 1

                yield None

            if i==1:

                da[str(i)] -= 1

                cnt += 1

                yield None

            if da[str(i)]>=1:

                da[str(i)] -= 1

                cnt += 1

                yield None

            x = i//2

            y = x+i%2

            yield dp(x)

            yield dp(y)

            yield None

        

        for i in b:

            dp(i)

        #print(da)

        flag = True

        for x in da.values():

            if x!=0:

                flag = False

                break

        

        if flag==True:

            print('Yes')

        else:

            print('No')    





if __name__ == '__main__':

    main()