qyliang/CodeNanoFix huggingface dataset:
Buggy code:
S = input()

V = "keyence"

for i in range(len(S) - len(V) + 1):
    print(i, S[:i], S[i-len(V):])
    if S[:i] + S[i-len(V):] == V:
        print("YES")
        exit(0)
print("NO")


Fixed code:
S = input()

V = "keyence"
t = len(S) - len(V)

for i in range(len(S) - t + 1):
    if S[:i] + S[i+t:] == V:
        print("YES")
        exit(0)
print("NO")
Buggy code:
s=input()
word='keyence'
flag=0
for i in range(7):
    if s[:i]+s[:-1-(8-i)]==word:
        flag=1
print('YES' if flag else 'NO')

Fixed code:
s=input()
word='keyence'
flag=0
if s[:6]==word:
    flag=1
if s[-7:]==word:
    flag=1
for i in range(7):
    if s[:i]+s[-(7-i):]==word:
        flag=1
print('YES' if flag else 'NO')
Buggy code:
s = input()
print(
	"YES" if any(
      s[:i] + s[j:]
      for i in range(len(s))
      for j in range(len(s))
      if i <= j) else
  	"NO"
)


Fixed code:
s = input()
print(
	"YES" if any(
      s[:i] + s[j:] == "keyence"
      for i in range(len(s))
      for j in range(len(s))
      if i <= j) else
  	"NO"
)
Buggy code:
S = input()
for i in range(7):
    if S[:i] + S[-7+i] == 'keyence':
        print('YES')
        exit()
print('NO')

Fixed code:
S = input()
for i in range(7):
    if S[:i] + S[-7+i:] == 'keyence':
        print('YES')
        exit()
print('NO')
Buggy code:
S = input()
keyence = "keyence"
for idx, (s, k)  in enumerate(zip(S, keyence)):
    if s==k:
        continue
    else:
        nokori = keyence[idx:]
        if S[-len(nokori):] == nokori:
            print("YES")
            break
        else:
            print("NO")
            break

Fixed code:
S = input()
keyence = "keyence"
if S.split()[0] == keyence:
    print("YES")
for idx, (s, k)  in enumerate(zip(S, keyence)):
    if s==k:
        continue
    else:
        nokori = keyence[idx:]
        if S[-len(nokori):] == nokori:
            print("YES")
            break
        else:
            print("NO")
            break
Buggy code:
S = input ( )

dusted = False
cont = False
line = 'keyence'
find = S.find ( 'keyence' )
rfind = S.rfind ( 'keyence' )
#print ( 'find {} rfind {}'.format ( find, rfind ) )
if find == 0 or rfind == len ( S ) - 7:
    print ( 'YES' )
    exit ( 0 )
elif find != -1:
    print ( 'NO' )
    exit ( 0 )

fs = True
line = ''
keyence = [ c for c in "keyence" ]
a_err=False
b_err=False
keyence2='keyence'
count=0
for i in range(len ( S )):

    #print ( keyence )
    #print ( 'i {} ri {}'.format ( S[i], S[len(S)-1-i] ) )
    try:
        if not  b_err:
            if keyence2[ 6 - i ] != S[ len ( S ) - 1 - i ]:
                print ( 'NO' )
                exit ( 0 )
            keyence.remove ( S[len(S)-1-i] )

            count+=1

    except ValueError:
        b_err=True

        if not keyence:
            print ( 'YES' )
            exit ( 0 )
    try:
        if not a_err:
            if keyence2[ i ] != S[ i]:
                print ( 'NO' )
                exit ( 0 )
            keyence.remove ( S[i] )
    except ValueError:
        a_err=True

    if a_err and b_err:
        print('NO')
        exit(0)

    if not keyence:
        print ( 'YES' )
        exit ( 0 )



Fixed code:
S = input ( )

dusted = False
cont = False
line = 'keyence'
find = S.find ( 'keyence' )
rfind = S.rfind ( 'keyence' )
#print ( 'find {} rfind {}'.format ( find, rfind ) )
if find == 0 or rfind == len ( S ) - 7:
    print ( 'YES' )
    exit ( 0 )
elif find != -1:
    print ( 'NO' )
    exit ( 0 )

fs = True
line = ''
keyence = [ c for c in "keyence" ]
a_err=False
b_err=False
keyence2='keyence'
count=0
for i in range(len ( S )):

    #print ( keyence )
    #print ( 'i {} ri {}'.format ( S[i], S[len(S)-1-i] ) )
    try:
        if not  b_err:
            #print ( 'key2 {} S {}'.format ( keyence2[ 6 - i ], S[ len ( S ) - 1 - i ] ) )

            if keyence2[ 6 - i ] != S[ len ( S ) - 1 - i ]:
                b_err=True
            if not b_err:
                keyence.remove ( S[len(S)-1-i] )

            count+=1

    except ValueError:
        b_err=True

    if not keyence:
        print ( 'YES' )
        exit ( 0 )
    try:
        if not a_err:
            if keyence2[ i ] != S[ i]:
                a_err=True
            if not a_err:
                keyence.remove ( S[i] )
    except ValueError:
        a_err=True

    if a_err and b_err:
        print('NO')
        exit(0)

    if not keyence:
        print ( 'YES' )
        exit ( 0 )
Buggy code:
s = input()
n = len(s)

for i in range(0, n):
    for j in range(i+1, n+1):
        if s[0:i] + s[j:] == 'keyence':
            print('YES')
            exit()
        # print(s[i:j])
print('NO')


Fixed code:
s = input()
n = len(s)

if s == 'keyence':
    print('YES')
    exit()

for i in range(0, n):
    for j in range(i+1, n+1):
        if s[0:i] + s[j:] == 'keyence':
            print('YES')
            exit()
        # print(s[i:j])
print('NO')
Buggy code:
# from pprint import pprint
# import math
# import collections

# n = int(input())
s = input()
# n, k = map(int, input().split(' '))
# a = list(map(int, input().split(' ')))

K = 'keyence'

for i in range(len(s) + 1):
    for j in range(7 + 1):
        s1 = s[0:i]
        s2 = s[i:i + j]
        s3 = s[i + j:]

        print(s1, s2, s3)
        if s1 == K or s2 == K or s3 == K or s1 + s2 == K or s1 + s3 == K or s2 + s3 == K:
            print('YES')
            exit()

print('NO')


Fixed code:
# from pprint import pprint
# import math
# import collections

# n = int(input())
s = input()
# s = 'keyxxxxxxxxxxence'
# n, k = map(int, input().split(' '))
# a = list(map(int, input().split(' ')))

K = 'keyence'

for i in range(len(s)):
    for j in range(len(s)):
        s1 = s[0:i]
        s2 = s[i:i + j]
        s3 = s[i + j:]

        # print(s1,s2,s3)
        if s1 == K or s2 == K or s3 == K or s1 + s2 == K or s1 + s3 == K or s2 + s3 == K:
            print('YES')
            exit()

print('NO')
Buggy code:
S=input()
T="keyence"
if S[:7]==T:
    print("YES")
    exit()
for i in range(len(S)-1):
    for j in range(i,len(S)-1):
        if S[:i]+S[j:]==T:
            print("YES")
            exit()
print("No")

Fixed code:
S=input()
T="keyence"
if S[:7]==T:
    print("YES")
    exit()
for i in range(len(S)-1):
    for j in range(i,len(S)):
        if S[:i]+S[j:]==T:
            print("YES")
            exit()
print("NO")
Buggy code:
S = input()
N = len(S)

ans = false
for i in range(N):
  for j in range(i,N):
    if S[:i]+S[j:] == "keyence":
      print("YES")
      ans = True
      break
  if ans: break
else:
  print("NO")

Fixed code:
S = input()
N = len(S)

ans = False
for i in range(N):
  for j in range(i,N):
    if S[:i]+S[j:] == "keyence":
      print("YES")
      ans = True
      break
  if ans: break
else:
  print("NO")
Buggy code:
import re

if re.match("keyence",S) or re.match("^(.*)(keyence)$",S) or re.match("^k(.*)(eyence)$",S) or re.match("^(ke)(.*)(yence)$",S) or re.match("^(key)(.*)(ence)$",S) or re.match("^(keye)(.*)(nce)$",S) or re.match("^(keyen)(.*)(ce)$",S) or re.match("^(keyenc)(.*)(e)$",S)  or re.match("^(keyence)(.*)$",S):
    print("YES")
else:
    print("NO")

Fixed code:
import re

S=input()

if re.match("keyence",S) or re.match("^(.*)(keyence)$",S) or re.match("^k(.*)(eyence)$",S) or re.match("^(ke)(.*)(yence)$",S) or re.match("^(key)(.*)(ence)$",S) or re.match("^(keye)(.*)(nce)$",S) or re.match("^(keyen)(.*)(ce)$",S) or re.match("^(keyenc)(.*)(e)$",S)  or re.match("^(keyence)(.*)$",S):
    print("YES")
else:
    print("NO")
Buggy code:
S = input()
a = len(S)
count=0
for i in range(a):
  for j in range(i+1,a):
    XX= S[:i] +S[j:a]
    
    if XX == 'keyence':
      count +=1
if count == 0:
  print('NO')
else:
  print('YES')

Fixed code:
S = input()
a = len(S)
count=0
for i in range(a):
  for j in range(i,a):
    XX= S[:i] +S[j:a]
    
    if XX == 'keyence':
      count +=1
if count == 0:
  print('NO')
else:
  print('YES')
Buggy code:
S = input().rstrip()

if S == 'keyence': print('YES')
elif len(S) == 7: print('NO')
else:
	for length in range(len(S)-7):
		for i in range(len(S)-length):
			print(S[0:i] + S[i+length+1:])
			if S[0:i] + S[i+length+1:] == 'keyence':
				print('YES')
				exit(0)
	print('NO')

Fixed code:
S = input().rstrip()

if S == 'keyence': print('YES')
elif len(S) == 7: print('NO')
else:
	for length in range(len(S)-7):
		for i in range(len(S)-length):
			if S[0:i] + S[i+length+1:] == 'keyence':
				print('YES')
				exit(0)
	print('NO')
Buggy code:
import sys
if sys.platform=='ios':
	sys.stdin=open('input_file.txt')
	
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

ans=0
if sum(A)<sum(B):
	ans=-1
	
else:
	cnt=0
	res=[]
	for i in range(N):
		if A[i]>B[i]:
			res.append(A[i]-B[i])
		else:
			cnt+=B[i]-A[i]
			ans+=1
	if cnt>0:
		ans+=1
		for i in range(len(res)):
			if res[i]<cnt:
				ans+=1
				cnt-=res[i]
			else:
				break

print(ans)

Fixed code:
import sys
if sys.platform=='ios':
	sys.stdin=open('input_file.txt')
	
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

ans=0
if sum(A)<sum(B):
	ans=-1
	
else:
	cnt=0
	res=[]
	for i in range(N):
		if A[i]>=B[i]:
			res.append(A[i]-B[i])
		else:
			cnt+=B[i]-A[i]
			ans+=1
	if cnt>0:
		ans+=1
		res=sorted(res)[::-1]
		for i in range(len(res)):
			if res[i]<cnt:
				ans+=1
				cnt-=res[i]
			else:
				break

print(ans)
Buggy code:
from sys import exit
import bisect

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(A) < sum(B):
    print(-1)
    exit()
L = [b - a for a, b in zip(A, B)]
L.sort()
x = bisect.bisect_left(L,0)
y = bisect.bisect_right(L, 0)
low = abs(sum(L[y:]))
s = 0
ans = 0
for l in L:
    if s >= low:
        break
    s += l
    ans += 1
print(ans)


Fixed code:
from sys import exit
import bisect

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(A) < sum(B):
    print(-1)
    exit()
L = [a - b for a, b in zip(A, B)]
L.sort()
x = bisect.bisect_left(L, 0)
low = abs(sum(L[:x]))
s = 0
ans = x  
L.reverse()
for l in L:
    if s >= low:
        break
    s += l
    ans += 1
print(ans)
Buggy code:
from bisect import bisect_left

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

div = [a[i] - b[i] for i in range(n)]

if sum(div) < 0:
    print(-1)
    exit()

div.sort()
idx = bisect_left(div, 0)
positive = div[idx:]
negative = div[:idx]

if idx == 0:
    print(0)
    exit()

ans = len(negative)
changed = False

for x in negative:
    while x > positive[-1]:
        x -= positive.pop(-1)
        ans += 1
        changed = False
    positive[-1] -= x
    if not changed:
        ans += 1
        changed = True

print(ans)


Fixed code:
from bisect import bisect_left

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

div = [a[i] - b[i] for i in range(n)]

if sum(div) < 0:
    print(-1)
    exit()

div.sort()
idx = bisect_left(div, 0)
positive = div[idx:]
negative = div[:idx]

if idx == 0:
    print(0)
    exit()

ans = len(negative)
changed = False

for x in negative:
    x = abs(x)
    while x > positive[-1]:
        x -= positive.pop(-1)
        if not changed:
            ans += 1
        changed = False
    positive[-1] -= x
    if not changed:
        ans += 1
        changed = True

print(ans)
Buggy code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sp=sn=0
P=[]
a=0
for i in range(n):
  d=A[i]-B[i]
  if d<0:
    sn+=d
    a+=1
  else:
    sp+=d
    P.append(d)
if sp<-sn:
  print(-1)
  exit()
for p in sorted(P[::-1]):
  if sn>=0:
    break
  else:
    sn+=p
    a+=1
print(a)

Fixed code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sp=sn=0
P=[]
a=0
for i in range(n):
  d=A[i]-B[i]
  if d<0:
    sn+=d
    a+=1
  else:
    sp+=d
    P.append(d)
if sp<-sn:
  print(-1)
  exit()
for p in sorted(P)[::-1]:
  if sn>=0:
    break
  else:
    sn+=p
    a+=1
print(a)
Buggy code:
import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num, ans, val = 0, 0, 0
vals = []

for i in range(n):
    c = b[i]-a[i]
    if c > 0:
        num += c
        ans += 1
    elif c < 0:
        vals.append(-c)
vals.sort()

if len(vals) == 0:
    print(0)
elif sum(a) < sum(b):
    print(-1)
else:
    for i in vals[::-1]:
        ans += 1
        val += i
        if val >= num:
            print(ans)
            break


Fixed code:
import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num, ans, val = 0, 0, 0
vals = []

for i in range(n):
    c = b[i]-a[i]
    if c > 0:
        num += c
        ans += 1
    elif c < 0:
        vals.append(-c)
vals.sort()

if len(vals) == 0:
    print(0)
elif sum(a) < sum(b):
    print(-1)
else:
    for i in vals[::-1]:
        if val >= num:
            break
        ans += 1
        val += i
    print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

surplus = []
short = []

for i in range(n):
    if a[i] > b[i]:
        surplus.append(a[i]-b[i])
    elif b[i] > a[i]:
        short.append(b[i]-a[i])

surplus = sorted(surplus)
taken = False
cnt = 0
for i in range(len(short)):
    cost = short.pop()
    cnt += 1

    while cost > 0:
        if len(surplus) == 0:
            print(-1)
           	exit()

        take = min(cost, surplus[-1])
        cost -= take
        surplus[-1] -= take

        if not taken:
            taken = True
            cnt += 1

        if surplus[-1] == 0:
            surplus.pop()
            taken = False

print(cnt)

Fixed code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

surplus = []
short = []

for i in range(n):
    if a[i] > b[i]:
        surplus.append(a[i]-b[i])
    elif b[i] > a[i]:
        short.append(b[i]-a[i])

surplus = sorted(surplus)
taken = False
cnt = 0
for i in range(len(short)):
    cost = short.pop()
    cnt += 1

    while cost > 0:
        if len(surplus) == 0:
            print(-1)
            exit()

        take = min(cost, surplus[-1])
        cost -= take
        surplus[-1] -= take

        if not taken:
            taken = True
            cnt += 1

        if surplus[-1] == 0:
            surplus.pop()
            taken = False

print(cnt)
Buggy code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = sorted([A[i]-B[i] for i in range(N)],reverse=True)
cp = 0
cm = 0
for i in range(N):
    if C[i]>0:
        cp += C[i]
    elif C[i]<0:
        cm += -C[i]
if cm>cp:
    print(-1)
else:
    cur = 0
    tot = C[cur]
    for i in range(N):
        if C[i]<0:
            while tot-abs(C[i])<0:
                tot += C[cur+1]
                cur += 1
            tot += C[i]
print(cur+1+cm)     

Fixed code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = sorted([A[i]-B[i] for i in range(N)],reverse=True)
cp = 0
cm = 0
cnt = 0
for i in range(N):
    if C[i]>0:
        cp += C[i]
    elif C[i]<0:
        cnt += 1
        cm += -C[i]
if cm>cp:
    print(-1)
elif cnt==0:
    print(0)
else:
    cur = 0
    tot = C[cur]
    for i in range(N):
        if C[i]<0:
            while tot-abs(C[i])<0:
                tot += C[cur+1]
                cur += 1
            tot += C[i]
    print(cur+1+cnt)
Buggy code:
n = int(input())
la = [int(w) for w in input().split()]
lb = [int(w) for w in input().split()]
ld = [a-b for a, b in zip(la, lb)]
if sum(ld) < 0:
    print(-1)
    exit()

llower = [w for w in ld if w < 0]
lupper = [w for w in ld if w > 0]
lowsum = sum(llower)
lupper.sort(reverse=True)

ans = len(llower)

print(llower)
for d in lupper:
    if lowsum >= 0:
        break
    ans += 1
    lowsum += d


print(ans)


Fixed code:
n = int(input())
la = [int(w) for w in input().split()]
lb = [int(w) for w in input().split()]
ld = [a-b for a, b in zip(la, lb)]
if sum(ld) < 0:
    print(-1)
    exit()

llower = [w for w in ld if w < 0]
lupper = [w for w in ld if w > 0]
lowsum = sum(llower)
lupper.sort(reverse=True)

ans = len(llower)

for d in lupper:
    if lowsum >= 0:
        break
    ans += 1
    lowsum += d


print(ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

if sum(b)>=sum(a):
    print(-1)
else:
    c = [a[i]-b[i] for i in range(n) if a[i]-b[i]>=0]
    d = [a[i]-b[i] for i in range(n) if a[i]-b[i]<0]
    d.append(0)
    c.sort(reverse=True)
    from itertools import accumulate
    c = list(accumulate(c))

    from bisect import bisect_left
    index= bisect_left(c,-1*sum(d))
    print(len(d)+index)

Fixed code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

if sum(b)>sum(a):
    print(-1)
else:
    c = [a[i]-b[i] for i in range(n) if a[i]-b[i]>=0]
    d = [a[i]-b[i] for i in range(n) if a[i]-b[i]<0]
    d.append(0)
    c.sort(reverse=True)
    from itertools import accumulate
    c = list(accumulate(c))
    from bisect import bisect_left
    index= bisect_left(c,-1*sum(d))
    if d == [0]:
        d.pop(0)
    print(len(d)+index)
Buggy code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    cnt, nega, posi = 0, 0, []
    for x, y in zip(a, b):
        if x < y:
            nega += y - x
            cnt += 1
        else:
            posi.append(x - y)
    posi.sort(reverse=True)
    i = 0
    while nega > 0:
        nega -= posi[i]
        cnt += 1
        i += 1
    print(cnt)

if __name__ == "__main__":
    main()

Fixed code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    cnt, nega, posi = 0, 0, []
    for x, y in zip(a, b):
        if x < y:
            nega += y - x
            cnt += 1
        else:
            posi.append(x - y)
    if sum(posi) - nega < 0:
        print(-1)
        exit()
    posi.sort(reverse=True)
    i = 0
    while nega > 0:
        nega -= posi[i]
        cnt += 1
        i += 1
    print(cnt)

if __name__ == "__main__":
    main()
Buggy code:
import heapq
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
if sum(a_list)<sum(b_list):
    print(-1)
else:
    diff_list = [a_list[i]-b_list[i] for i in range(n)]
    posi_heap=heapq.heapify([])
    cnt,diff_sum = 0,0
    for diff in diff_list:
        if diff<0:
            cnt+=1
            diff_sum+=diff
        elif diff>0:
            heapq.heappush(posi_heap,-1*diff)
    diff_sum*=(-1)
    i=0
    while diff_sum>0:
        diff_sum+=heapq.heappop(posi_heap)
        i+=1
    print(i+cnt)

Fixed code:
import heapq
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
if sum(a_list)<sum(b_list):
    print(-1)
else:
    diff_list = [a_list[i]-b_list[i] for i in range(n)]
    posi_heap=[]
    heapq.heapify(posi_heap)
    cnt,diff_sum = 0,0
    for diff in diff_list:
        if diff<0:
            cnt+=1
            diff_sum+=diff
        elif diff>0:
            heapq.heappush(posi_heap,-1*diff)
    diff_sum*=(-1)
    i=0
    while diff_sum>0:
        diff_sum+=heapq.heappop(posi_heap)
        i+=1
    print(i+cnt)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = []
D = []

for i in range(N):
    s = B[i] - A[i]
    if(s > 0):
        C.append(s)
    elif(s < 0):
        D.append(s)

D = sorted(D)

csum = sum(C)
dsum = sum(D)

if(csum + dsum > 0):
    print(-1)
elif(csum == 0):
    print(0)
else:
    ans = len(C)
    for i in D:
        csum += D
        ans += 1
        if(csum <= 0):
            break
    print(ans)

Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0]
D = [0]

for i in range(N):
    s = B[i] - A[i]
    if(s > 0):
        C.append(s)
    elif(s < 0):
        D.append(s)

D = sorted(D)

csum = sum(C)
dsum = sum(D)

if(csum + dsum > 0):
    print(-1)
elif(csum == 0):
    print(0)
else:
    ans = len(C) -1
    for i in D:
        csum += i
        ans += 1
        if(csum <= 0):
            break
    print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
s = 0
m = []
l = 0
if sum(B)<=sum(A):
    for i in range(N):
        if A[i] < B[i]:
            k += 1
            s += (B[i]- A[i])
        elif A[i] > B[i]+1:
            m.append(A[i]- B[i])
        elif A[i] == B[i]+1:
            l += (A[i]- B[i])
        n = sorted(m,reverse = True)
    for j in range(len(m)):
        if s > 0:
            s -= n[j]
            k += 1
        if s <0:
            break
    if (s > 0) and(k > 0):
         k += s
else:
    print(-1)
print(k)

Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
s = 0
m = []
l = 0
if sum(B)<=sum(A):
    for i in range(N):
        if A[i] < B[i]:
            k += 1
            s += (B[i]- A[i])
        elif A[i] > B[i]+1:
            m.append(A[i]- B[i])
        elif A[i] == B[i]+1:
            l += (A[i]- B[i])
    if len(m)>1:
        n = sorted(m,reverse = True)
    else:
        n = m
    for j in range(len(m)):
        if s > 0:
            s -= n[j]
            k += 1
        if s <0:
            break
    if (s > 0) and(k > 0):
         k += s
else:
    k = -1
print(k)
Buggy code:
n = int(input())
l_a = list(map(int,input().split()))
l_b = list(map(int,input().split()))
if sum(l_a)<sum(l_b):
  print(-1)
  exit()
l_sa = sorted(list(i-j for i,j in zip(l_a,l_b)))
s = 0
cou =0
print(l_sa)
for i in l_sa:
  if i<0:
    s += i
    cou +=1
  else:
    break
#print(s,cou)
l_sb = l_sa[::-1]
for i in l_sb:
  if s<0:
    s += i
    cou += 1
 #   print(s,cou)
  else:
    break
print(cou)
  

Fixed code:
n = int(input())
l_a = list(map(int,input().split()))
l_b = list(map(int,input().split()))
if sum(l_a)<sum(l_b):
  print(-1)
  exit()
l_sa = sorted(list(i-j for i,j in zip(l_a,l_b)))
s = 0
cou =0
#print(l_sa)
for i in l_sa:
  if i<0:
    s += i
    cou +=1
  else:
    break
#print(s,cou)
l_sb = l_sa[::-1]
for i in l_sb:
  if s<0:
    s += i
    cou += 1
    #print(s,cou)
  else:
    break
print(cou)
Buggy code:
from bisect import bisect_left

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))

    surplus = []
    need = 0
    ans = 0
    for a, b in zip(A, B):
        diff = a - b
        if diff > 0:
            surplus.append(diff)
        if diff < 0:
            need += -diff
            ans += 1

    surplus.sort(reverse=True)
    cusum = [0] * (len(surplus) + 1)
    for i, sur in enumerate(surplus):
        cusum[i+1] = cusum[i] + sur

    plus = bisect_left(cusum, need)
    ans += plus

    if plus == len(surplus):
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()

Fixed code:
from bisect import bisect_left

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))

    surplus = []
    need = 0
    ans = 0
    for a, b in zip(A, B):
        diff = a - b
        if diff > 0:
            surplus.append(diff)
        if diff < 0:
            need += -diff
            ans += 1

    surplus.sort(reverse=True)
    cusum = [0] * (len(surplus) + 1)
    for i, sur in enumerate(surplus):
        cusum[i+1] = cusum[i] + sur

    plus = bisect_left(cusum, need)
    ans += plus
    if need and plus == len(cusum):
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()
Buggy code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

C = [A[i] - B[i] for i in range(n)]
C.sort()

if sum(C)<0:
  print(-1)
  exit()
  
ans = 0

for collections import deque
C = deque(C)
c = 0
while 1:
  nc = C.popleft()
  if nc >=0:
    break
  c+= nc
  ans += 1
  while c<0:
    c+=C.popright()
    ans += 1
print(ans)

Fixed code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

C = [A[i] - B[i] for i in range(n)]
C.sort()

if sum(C)<0:
  print(-1)
  exit()
  
ans = 0

from collections import deque
C = deque(C)
c = 0
while C:
  nc = C.popleft()
  if nc >=0:
    break
  c+= nc
  ans += 1
  while c<0:
    c+=C.pop()
    ans += 1
print(ans)