ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 203 ms):
t = int(input())



while t > 0:

n = int(input())

solved = False

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

for i in range(n-1):

q = min(arr[i], arr[i+1])

arr[i] = arr[i]- q

arr[i+1] = arr[i+1]- q

for i in range(n-1):

if arr[i+1]<arr[i]:

solved = True

print("NO")

break

if not solved:

print("YES")





t -= 1

Solution 2 (runtime: 233 ms):
'''********************* بسم الله الرحمن الرحيم ***********************'''



def listinp(): #list of integers

return list(map(int,input().split()))

def mulint(): #consecutive integers

return map(int,input().split())

def mulstr(): #consecutive str

return map(str,input().split())

def strlist():  #str list

return list(input())

def intinp():  #integer

return int(input())

def inp():   #string

return input()



from collections import defaultdict,deque

from bisect import bisect_left,bisect_right

from heapq import heappop,heappush,heapify

from math import ceil,isqrt



def solution():

x = intinp()

arr = listinp()

for i in range(1,x):

if arr[i-1]<=arr[i]:

arr[i] -= arr[i-1]

arr[i-1] = 0

elif arr[i-1] > arr[i]:

arr[i-1] -= arr[i]

arr[i] = 0

print('YnEoS'[not(arr == sorted(arr))::2])

for _ in range(intinp()):

solution()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 203 ms):
t = int(input())



while t > 0:

n = int(input())

solved = False

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

for i in range(n-1):

q = min(arr[i], arr[i+1])

arr[i] = arr[i]- q

arr[i+1] = arr[i+1]- q

for i in range(n-1):

if arr[i+1]<arr[i]:

solved = True

print("NO")

break

if not solved:

print("YES")





t -= 1

Solution 2 (runtime: 187 ms):
def solve():

import sys

data = sys.stdin.read().split()

if not data:

return

t = int(data[0])

index = 1

output = []

for _ in range(t):

n = int(data[index])

index += 1

a = list(map(int, data[index:index+n]))

index += n



# For n=2, we just need to check the first two elements.

if n >= 2 and a[0] > a[1]:

output.append("NO")

continue



# Build alternating prefix sum array P where:

# P[0] = a[0], P[i] = P[i-1] + (-1)**i * a[i]

P = [0] * n

P[0] = a[0]

for i in range(1, n):

if i % 2 == 1:

P[i] = P[i-1] - a[i]

else:

P[i] = P[i-1] + a[i]



# Check necessary conditions:

# For i from 1 to n-2 (corresponding to pairs (a[i], a[i+1]) for i>=1 in 0-index)

ok = True

for i in range(1, n-1):

gap = a[i] - a[i+1]

# In the original 1-indexed formulation:

# if (i+1) is even, require: a[i]-a[i+1] <= P[i-1]

# if (i+1) is odd (and i+1>=3), require: a[i]-a[i+1] <= -P[i-1]

if (i + 1) % 2 == 0:

if gap > P[i-1]:

ok = False

break

else:

if gap > -P[i-1]:

ok = False

break

output.append("YES" if ok else "NO")

sys.stdout.write("\n".join(output))





if __name__ == '__main__':

solve()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 233 ms):
'''********************* بسم الله الرحمن الرحيم ***********************'''



def listinp(): #list of integers

return list(map(int,input().split()))

def mulint(): #consecutive integers

return map(int,input().split())

def mulstr(): #consecutive str

return map(str,input().split())

def strlist():  #str list

return list(input())

def intinp():  #integer

return int(input())

def inp():   #string

return input()



from collections import defaultdict,deque

from bisect import bisect_left,bisect_right

from heapq import heappop,heappush,heapify

from math import ceil,isqrt



def solution():

x = intinp()

arr = listinp()

for i in range(1,x):

if arr[i-1]<=arr[i]:

arr[i] -= arr[i-1]

arr[i-1] = 0

elif arr[i-1] > arr[i]:

arr[i-1] -= arr[i]

arr[i] = 0

print('YnEoS'[not(arr == sorted(arr))::2])

for _ in range(intinp()):

solution()











Solution 2 (runtime: 187 ms):
def solve():

import sys

data = sys.stdin.read().split()

if not data:

return

t = int(data[0])

index = 1

output = []

for _ in range(t):

n = int(data[index])

index += 1

a = list(map(int, data[index:index+n]))

index += n



# For n=2, we just need to check the first two elements.

if n >= 2 and a[0] > a[1]:

output.append("NO")

continue



# Build alternating prefix sum array P where:

# P[0] = a[0], P[i] = P[i-1] + (-1)**i * a[i]

P = [0] * n

P[0] = a[0]

for i in range(1, n):

if i % 2 == 1:

P[i] = P[i-1] - a[i]

else:

P[i] = P[i-1] + a[i]



# Check necessary conditions:

# For i from 1 to n-2 (corresponding to pairs (a[i], a[i+1]) for i>=1 in 0-index)

ok = True

for i in range(1, n-1):

gap = a[i] - a[i+1]

# In the original 1-indexed formulation:

# if (i+1) is even, require: a[i]-a[i+1] <= P[i-1]

# if (i+1) is odd (and i+1>=3), require: a[i]-a[i+1] <= -P[i-1]

if (i + 1) % 2 == 0:

if gap > P[i-1]:

ok = False

break

else:

if gap > -P[i-1]:

ok = False

break

output.append("YES" if ok else "NO")

sys.stdout.write("\n".join(output))





if __name__ == '__main__':

solve()
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: 593 ms):
import sys
input = sys.stdin.readline
from collections import *
from math import *
from heapq import*
I = lambda: map(int, input().split())
def bit(n):
    a=[]
    while n:
        a.append(n&1)
        n=n>>1
    return a
for _ in range(int(input())):
    n,k=I()
    a=sorted(list(I()))
    b=list(I())
    b=[-j for j in b]
    heapify(b)
    j=n-1
    res="yes"
    while j>-1:
        if len(b)>j+1:
            res="no"
            break
            
        if b:
            x=-heappop(b)
        else:
            res="no"
            break
        if a[j]==x:
            j-=1
        elif a[j]>x:
            res="no"
            break
        else:
            q=x//2
            p=ceil(x/2)
            heappush(b,-p)
            heappush(b,-q)
    if b:
        res="no"       
    print(res)
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: 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")
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: 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")
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 593 ms):
import sys
input = sys.stdin.readline
from collections import *
from math import *
from heapq import*
I = lambda: map(int, input().split())
def bit(n):
    a=[]
    while n:
        a.append(n&1)
        n=n>>1
    return a
for _ in range(int(input())):
    n,k=I()
    a=sorted(list(I()))
    b=list(I())
    b=[-j for j in b]
    heapify(b)
    j=n-1
    res="yes"
    while j>-1:
        if len(b)>j+1:
            res="no"
            break
            
        if b:
            x=-heappop(b)
        else:
            res="no"
            break
        if a[j]==x:
            j-=1
        elif a[j]>x:
            res="no"
            break
        else:
            q=x//2
            p=ceil(x/2)
            heappush(b,-p)
            heappush(b,-q)
    if b:
        res="no"       
    print(res)

Solution 2 (runtime: 656 ms):
from random import randint
class MyDict:
    def __init__(self, func=lambda: 0):
        self.random = randint(0, (1 << 32) - 1) 
        self.default = func
        self.dict = {}
    def _hash(self, key):
        return self.random ^ hash(key)
    def __getitem__(self, key):
        mykey = self._hash(key)
        if mykey not in self.dict:
            self.dict[mykey] = self.default()
        return self.dict[mykey]
    def __setitem__(self, key, value):
        self.dict[self._hash(key)] = value
    def __delitem__(self, key):
        mykey = self._hash(key)
        if mykey in self.dict:
            del self.dict[mykey]
        else:
            raise KeyError(key)
    def get(self, key, default=None):
        return self.dict.get(self._hash(key), default)
    def keys(self):
        return [self._hash(k) for k in self.dict]
    def values(self):
        return self.dict.values()
    def items(self):
        return [(self._hash(k), v) for k, v in self.dict.items()]
    def __contains__(self, key):
        return self._hash(key) in self.dict
    def __iter__(self):
        return iter(self.keys())
    def __len__(self):
        return len(self.dict)
    def __str__(self):
        return str({self._hash(k): v for k, v in self.dict.items()})
    def __repr__(self):
        return f"MyDict({self.default}, {self.dict})"

import heapq
def solve():
    n,m = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    st = []
    for i in b :
        st.append(-i)

    heapq.heapify(st)

    freq = MyDict()
    for i in a:
        freq[i] += 1

    for i in range(2*n-m):
        if not st:
            print("NO")
            return
        val = - heapq.heappop(st)
        if freq[val] != 0:
            freq[val] -= 1

        else:
            heapq.heappush(st,-(val // 2))
            heapq.heappush(st,-((val+2-1)//2))
    if st :
        print("NO")
    else:
        print("YES")

for __ in range(int(input())):
    solve()
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 593 ms):
import sys
input = sys.stdin.readline
from collections import *
from math import *
from heapq import*
I = lambda: map(int, input().split())
def bit(n):
    a=[]
    while n:
        a.append(n&1)
        n=n>>1
    return a
for _ in range(int(input())):
    n,k=I()
    a=sorted(list(I()))
    b=list(I())
    b=[-j for j in b]
    heapify(b)
    j=n-1
    res="yes"
    while j>-1:
        if len(b)>j+1:
            res="no"
            break
            
        if b:
            x=-heappop(b)
        else:
            res="no"
            break
        if a[j]==x:
            j-=1
        elif a[j]>x:
            res="no"
            break
        else:
            q=x//2
            p=ceil(x/2)
            heappush(b,-p)
            heappush(b,-q)
    if b:
        res="no"       
    print(res)

Solution 2 (runtime: 921 ms):
import sys
from collections import Counter
from heapq import heapify, heappop, heappush

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")

def cud(n, d):
    if d.get(str(n),0):
        d[str(n)] -= 1
        return True
    if n == 1:
        return False
    return cud(n // 2, d) and cud((n // 2) + (n&1),d)
    

def solve(test_case):
    N,M = multi_int_input()
    A = list_input()
    B = list_input()
    
    b = sorted(B, reverse=True)
    
    d = {}
    for i in A:
        i = str(i)
        d[i] = d.get(i,0) + 1
        
    for i in b:
        if not cud(i, d):
            P1('No')
            return
        
        
        
    #print(d)
    if any(d.values()):
        P1('No')
    else:
        P1('Yes')
        
                


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 593 ms):
import sys
input = sys.stdin.readline
from collections import *
from math import *
from heapq import*
I = lambda: map(int, input().split())
def bit(n):
    a=[]
    while n:
        a.append(n&1)
        n=n>>1
    return a
for _ in range(int(input())):
    n,k=I()
    a=sorted(list(I()))
    b=list(I())
    b=[-j for j in b]
    heapify(b)
    j=n-1
    res="yes"
    while j>-1:
        if len(b)>j+1:
            res="no"
            break
            
        if b:
            x=-heappop(b)
        else:
            res="no"
            break
        if a[j]==x:
            j-=1
        elif a[j]>x:
            res="no"
            break
        else:
            q=x//2
            p=ceil(x/2)
            heappush(b,-p)
            heappush(b,-q)
    if b:
        res="no"       
    print(res)

Solution 2 (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")
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 593 ms):
import sys
input = sys.stdin.readline
from collections import *
from math import *
from heapq import*
I = lambda: map(int, input().split())
def bit(n):
    a=[]
    while n:
        a.append(n&1)
        n=n>>1
    return a
for _ in range(int(input())):
    n,k=I()
    a=sorted(list(I()))
    b=list(I())
    b=[-j for j in b]
    heapify(b)
    j=n-1
    res="yes"
    while j>-1:
        if len(b)>j+1:
            res="no"
            break
            
        if b:
            x=-heappop(b)
        else:
            res="no"
            break
        if a[j]==x:
            j-=1
        elif a[j]>x:
            res="no"
            break
        else:
            q=x//2
            p=ceil(x/2)
            heappush(b,-p)
            heappush(b,-q)
    if b:
        res="no"       
    print(res)

Solution 2 (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")