Problem:
 Score : 400 points    Problem Statement A university student, Takahashi, has to take N examinations and pass all of them.
Currently, his readiness for the i -th examination is A_{i} , and according to his investigation, it is known that he needs readiness of at least B_{i} in order to pass the i -th examination.  Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness.  For Takahashi, find the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the following conditions:   The sum of the sequence A_1, A_2, ..., A_{N} and the sum of the sequence C_1, C_2, ..., C_{N} are equal.  For every i , B_i \leq C_i holds.   If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1 .      Constraints  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^9  A_i and B_i are integers.         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_{N}  B_1  B_2  ...  B_{N}       Output Print the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the conditions.
If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1 .        Sample Input 1 3
2 3 5
3 4 1      Sample Output 1 3  (A_1, A_2, A_3) = (2, 3, 5) and (B_1, B_2, B_3) = (3, 4, 1) . If nothing is done, he cannot pass the first and second exams.
The minimum possible number of indices i such that A_i and C_i are different, 3 , is achieved when:   (C_1, C_2, C_3) = (3, 5, 2)        Sample Input 2 3
2 3 3
2 2 1      Sample Output 2 0  In this case, he has to do nothing in order to pass all the exams.       Sample Input 3 3
17 7 1
25 6 14      Sample Output 3 -1  In this case, no matter what is done, he cannot pass all the exams.       Sample Input 4 12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604
74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212      Sample Output 4 5

Solution (Python):
import sys
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
s=sum(A)-sum(B)
if s<0:
    print(-1)
    sys.exit()
D=[]
inf=float("inf")
for i in range(N):
    num=A[i]-B[i]
    if num<0:
        D.append(inf)
    else:
        D.append(num)
D.sort()
cnt=0
for i in range(N):
    s-=D[i]
    if s>=0:
        cnt+=1
    else:
        break
print(N-cnt)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    B=LI()
    amari = []
    shortage = 0
    ans=0
    for a,b in zip(A,B):
        if a<b:
            shortage+=b-a
            ans+=1
        else:
            amari.append(a-b)
    if shortage==0:
        print(0)
        return
    amari.sort(reverse=True)
    for a in amari:
        shortage -= a
        ans += 1
        if shortage<=0:
            print(ans)
            return 

    print(-1)

if __name__ == '__main__':
    main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  a=LI()
  b=LI()

  c=[]
  for i in range(n):
    c.append(a[i]-b[i])

  c.sort()
  cnt=0
  sm=0

  for x in c:
    if x<0:
      cnt+=1
      sm+=x

  for x in c[::-1]:
    if sm>=0:
      return cnt
    if x>0:
      cnt+=1
      sm+=x

  return -1


print(main())

Solution (Python):
import sys

read = sys.stdin.readline

N = int(read())
A = tuple(map(int, read().split()))
B = tuple(map(int, read().split()))

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()
elif sum(d) < 0:
    print(-1)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()

Solution (Python):
import sys

read = sys.stdin.readline

N = int(read())
A = list(map(int, read().split()))
B = list(map(int, read().split()))

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()
elif sum(d) < 0:
    print(-1)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()

Solution (Python):
import sys

read = sys.stdin.readline

N = int(read())
A = list(map(int, read().split()))
B = list(map(int, read().split()))

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()
else:
    print(-1)

Solution (Python):
import sys

read = sys.stdin.readline

N = int(read())
A = list(map(int, read().split()))
B = list(map(int, read().split()))

if sum(A) < sum(B):
    print(-1)
    exit()

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below -= i
        n += 1
    else:
        break

tmp = 0
for i in d[::-1]:
    tmp += i
    n += 1
    if tmp > below:
        print(n)
        exit()

Solution (Python):
import sys

read = sys.stdin.readline

N = int(read())
A = list(map(int, read().split()))
B = list(map(int, read().split()))

if sum(A) < sum(B):
    print(-1)
    exit()

d = [A[i] - B[i] for i in range(N)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below -= i
        n += 1
    else:
        break

tmp = 0
for i in d[::-1]:
    tmp += i
    n += 1
    if tmp > below:
        print(n)
        exit()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
diff=[0]*N
     
ans=0
need=0
for i in range(N):
    diff[i]=B[i]-A[i]
    if diff[i]>0:
        ans+=1
        need+=diff[i]

diff.sort()

for i in range(N):
    if need<=0:
        break
    if diff[i]>0:
        ans=-1
        break
    need+=diff[i]
    ans+=1
    
print(ans)

Solution (Python):
import numpy as np
n=int(input())
A=np.array(list(map(int,input().split())))
B=np.array(list(map(int,input().split())))
C=A-B
C.sort()
husai=0
genkai=0
if sum(C)<0:
    print(-1)
else:
    for i in range(n):
        if C[i]<0:
            husai+=C[i]
        else:
            genkai=i
            break
    if husai==0:
        genkai2=0
    else:
        for j in range(1,n+1):
            husai+=C[-j]
            if husai>=0:
                genkai2=j
                break
    print(genkai+genkai2)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
S=[A[i]-B[i] for i in range(N)]
S.sort()
if S[0]>=0:
  print(0)
  exit()
if S[-1]<0:
  print(-1)
  exit()
  
x=0
y=N-1
while y-x>1:
  if S[(y+x)//2]<0:
    x=(y+x)//2
  else:
    y=(y+x)//2

minus=-sum(S[:y])

z=N-1
s=S[N-1]
while z>y and s<minus:
  z-=1
  s+=S[z]
  
if s<minus:
  print(-1)
else:
  print(N-(z-y))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

dif = []
ans = 0
tmp = 0
dsm = 0
for i in range(N):
    dif.append(A[i] - B[i])
    dsm += A[i] - B[i]
    if A[i] < B[i]:
        ans += 1
        tmp += B[i] - A[i]

if dsm < 0:
    print(-1)
else:
    dif.sort(reverse=True)
    i = 0
    while tmp > 0:
        tmp -= dif[i]
        i += 1
        ans += 1
    print(ans)

Solution (Python):

si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n=ni()
a=nl()
b=nl()
c=[]
d=[]
for i in range(n):
    if a[i]-b[i]<0:
        c.append(a[i]-b[i])
    elif a[i]-b[i]>0:
        d.append(a[i]-b[i])

if len(c)==0 and len(d)>=0:
    print(0)
    exit()

if len(c)>0 and len(d)==0:
    print(-1)
    exit()

d.sort(reverse=True)
sum_c=sum(c)
i=0
while sum_c<0 and i<len(d):
    sum_c+=d[i]
    i+=1

if sum_c<0:
    print(-1)
    exit()

print(i+len(c))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
up = []
down = []
down_sum = 0
for i,j in zip(A,B):
    if i - j > 0:
        up.append(i - j)
    elif i - j < 0:
        down.append(j - i)
        down_sum += j - i
if down_sum == 0:
    ans = 0
else:
    up_sum = 0
    ans = len(down)
    up.sort(reverse=True)
    for i in up:
        up_sum += i
        ans += 1
        if up_sum >= down_sum:
            break
    if up_sum < down_sum:
        ans = -1
print(ans)

Solution (Python):
import sys
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p=[]
m=[]
for i,j in zip(a,b):
    k=i-j
    if k < 0:
        m.append(k)
    else:
        p.append(k)
if sum(p)+sum(m) < 0:
    print(-1)
    sys.exit()
p.sort(reverse=True)
ans=0
P=0
M=abs(sum(m))

for i in range(n):
    if P >= M:
        print(ans+len(m))
        sys.exit()
    P+=p[i]
    ans+=1

Solution (Python):
import sys
N = int(input())

A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]

if sum(A)<sum(B):
    print(-1)
    sys.exit()

gap = [A[i]-B[i]  for i in range(N)]

minus = []
plus = []
for i in range(0,N):
    if gap[i]<0:
        minus.append(gap[i])

    else:
        plus.append(gap[i])

if minus == []:
    print(0)
    sys.exit()

summinus= sum(minus)

plus.sort(reverse=True)

j = 0
while True:
    summinus += plus[j]
    if summinus >= 0:
        print(N-len(plus)+j+1)
        break

    else:
        j += 1

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

if sum(b) > sum(a):
    print(-1)
    exit()

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

minus_sum = 0
minus_count = 0
for i in range(n):
    if d[i] < 0:
        minus_sum += d[i]
        minus_count += 1

ans = 0
for i in range(n)[::-1]:
    if minus_sum >= 0:
        print(ans+minus_count)
        exit()
    minus_sum += d[i]
    ans += 1

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a)<sum(b):
  print(-1)
  exit(0)
t=sum(a)-sum(b)
l=[]
for i in range(n):
  if a[i] >= b[i]:
    l.append(a[i]-b[i])
l.sort()
ans=0
for x in l:
  t-=x
  if t<0:
    break
  ans+=1
print(n-ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
OV = [a-b for a, b in zip(A, B) if a-b>0]
OV.sort(reverse = True)
UD = [a-b for a, b in zip(A, B) if a-b<0]
ans = len(UD)
if ans != 0:
  OVC = [sum(UD)]*(len(OV)+1)
  for i in range(1,len(OV)+1):
    OVC[i]=OVC[i-1]+OV[i-1]
    if OVC[i]>=0:
      ans += i
      OVC[-1]=0
      break
  if OVC[-1]<0:
    print(-1)
  else:
    print(ans)
else:
  print(ans)

Solution (Python):
n= int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if sum(a) < sum(b):
	    print(-1)
	    exit()
c= 0
for i in range(n):
	    if a[i]>=b[i]:c+=1
if c == n:
	    print(0)
	    exit()
chcnt = 0
needpoint = 0
for i in range(n):
	    a[i]-=b[i]
	    if a[i]<0:
	    	    chcnt+=1
	    	    needpoint-=a[i]
a.sort(reverse = True)
if a[0]== 0:
	    print(0)
	    exit()
givepoint = 0
for i in range(n):
	    givepoint += a[i]
	    if givepoint >= needpoint:
	    	    chcnt+=1
	    	    break
	    chcnt+=1
print(chcnt)

Solution (Python):
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    under, over = [], []
    for i in range(N):
        d = A[i] - B[i]
        if d < 0:
            under.append(d)
        elif 0 < d:
            over.append(d)
    over.sort(reverse=True)
    
    ans = len(under)
    lack = -sum(under)
    for i in range(len(over)):
        if lack <= 0:
            break
        lack -= over[i]
        ans += 1
    
    if 0 < lack:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
ans=0
d=0
flag=True
if sum(a)<sum(b):
    print(-1)
else:
    flag=False
    for i in range(n):
        c.append(a[i]-b[i])
        if a[i]<b[i]:
            d-=a[i]-b[i]
            ans+=1
            
c.sort()
c.reverse()
e=0

for i in range(n):
    if e>=d:
        break
    else:
        e+=c[i]
        ans+=1
if not flag:
    print(ans)

Solution (Python):
import heapq
import sys

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

short = 0
marge = []
ans = 0
for i in range(n):
	dif = a[i] - b[i]
	if dif < 0:
		ans += 1
		short += dif
	else:
		heapq.heappush(marge,dif*(-1))

if ans == 0:
	print(ans)
	sys.exit()

while marge != []:
	add = heapq.heappop(marge)*(-1)
	ans += 1
	short += add
	if short >= 0:
		print(ans)
		sys.exit()

print(-1)

Solution (Python):
import math
import bisect
import collections
import itertools
import heapq
import string
import sys
sys.setrecursionlimit(10**9)
def gcd(a,b):return math.gcd 
def lcm(a,b):return (a*b) // math.gcd(a,b) 
def iin(): return int(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def comb(l, n): return itertools.combinations(l, n) 
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n + 1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def sieve_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N = iin()
A = iln()
B = iln()
A_sum = sum(A)
B_sum = sum(B)

if A_sum < B_sum:
    print(-1)
    exit()

cnt = 0
minus = 0
plus = []
for i in range(N):
    diff = A[i] - B[i]
    if diff < 0:
        cnt += 1
        minus += abs(diff) 
    elif diff > 0:
        plus.append(diff)
plus.sort(reverse=True)
if minus == 0:
    print('0')
else:
    for i in range(len(plus)):
        minus -= plus[i]
        cnt += 1
        if minus <= 0:
            break
    print(cnt)

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, A: "List[int]", B: "List[int]"):

    enough = []
    husoku = 0
    counter = 0
    for a, b in zip(A, B):
        if a < b:
            husoku += b-a
            counter += 1
        else:
            enough.append(a-b)
    enough.sort(reverse=True)
    i = 0
    while husoku > 0 and i < len(enough):
        husoku -= enough[i]
        counter += 1
        i += 1

    if husoku > 0:
        print(-1)
    else:
        print(counter)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    B = [int(next(tokens)) for _ in range(N)]  
    solve(N, A, B)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

X = sorted([a-b for a, b in zip(A,B)])
Y = [x for x in X if x<0]
ans = len(Y)
Z = sum(Y)

for x in reversed(X):
    if Z>=0: break
    ans += 1
    Z += x

print(ans if Z>=0 else -1)

Solution (Python):
N = int(input())
a = [i for i in map(int,input().split())]
b = [i for i in map(int,input().split())]
diff = [a[i] - b[i] for i in range(N)]
diff_origin = [a[i] - b[i] for i in range(N)]
if sum(a) < sum(b):
  print("-1")
else:
  diff.sort()
  diff_origin.sort()
  ans = 0
  upper = len(diff)-1
  lower = 0
  while True:
    if diff[lower] >= 0:
      break
    if abs(diff[upper]) == abs(diff[lower]):
      diff[upper] = 0
      diff[lower] = 0
      upper -= 1
      lower += 1
    elif abs(diff[upper]) < abs(diff[lower]):
      diff[lower] += diff[upper]
      diff[upper] = 0
      upper -= 1
    else:
      diff[upper] += diff[lower]
      diff[lower] = 0
      lower += 1
  for i in range(len(diff)):
    if diff[i] != diff_origin[i]:
      ans += 1
  print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
d=[]
s=0
for i in range(n):
  if a[i]-b[i]>0:
    c.append(a[i]-b[i])
  if a[i]-b[i]<0:
    d.append(a[i]-b[i])
  s+=a[i]-b[i]
c.sort(reverse=True)
if s<0:
  print(-1)
elif len(d)==0:
  print(0)
else:
  ans=len(d)
  i=0
  while len(d)>0:
    if c[i]>=abs(d[0]):
      c[i]-=abs(d[0])
      d.remove(d[0])
    else:
      d[0]+=c[i]
      i+=1
  print(ans+i+1)

Solution (Python):
import numpy as np
N = int(input())

A = np.array(list(map(int,input().split())))
B = np.array(list(map(int,input().split())))
if sum(A) < sum(B):
    print(-1)
    exit()
d = A-B
sd = np.sort(d)[::-1]
negsum = np.sum(sd[sd < 0])
negcnt = np.sum(sd < 0)
cnt = 0
for i in sd:
    if i <= 0:
        break
    if negsum >= 0:
        break
    negsum += i
    cnt += 1
ans = negcnt + cnt
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
d=[a[i]-b[i] for i in range(n)]
d.sort()

if sum(a) < sum(b):
  print(-1)
  exit()

diff = 0
ct = 0
for i in range(n):
  if b[i] > a[i]:
    ct += 1
    diff += b[i] - a[i]

while diff > 0:
  ct += 1
  diff = diff - d[-1]
  del d[-1]

print(ct)

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

li = []
li2 = []

for i in range(n):
    if a[i] > b[i]:
        li.append(a[i] - b[i])
    elif a[i] < b[i]:
        li2.append(b[i] - a[i])

li.sort(reverse = True)
sum1 = sum(li2)

tmp = 0
count = 0

while tmp < sum1:
    if count < len(li):
        tmp += li[count]
        count += 1
    else:
        print(-1)
        exit()

print(count + len(li2))

Solution (Python):
from sys import exit, stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
B = [int(_) for _ in stdin.readline().rstrip().split()]
if (sum(A) < sum(B)):
    print(-1)
else:
    ans = 0
    diff = [a-b for a, b in zip(A,B)]
    plus = sorted([d for d in diff if d > 0])
    minus = [d for d in diff if d < 0]
    smms = sum(minus)
    while smms < 0:
        smms += plus.pop()
        ans += 1
    print(ans + len(minus))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

rest=[]
minussum=0
minuscnt=0
for i in range(N):
  diff=A[i]-B[i]
  if diff<0:
    minussum+=diff*(-1)
    minuscnt+=1
  elif diff==0:
    continue
  else:
    rest.append(diff)

rest=sorted(rest)[::-1]
if minussum>sum(rest):
  print(-1)
  exit(0)
if minuscnt==0:
  print(0)
  exit(0)

ind=0
while minussum>0:
  if minussum>rest[ind]:
    minussum-=rest[ind]
    ind+=1
  else:
    break
print(minuscnt+ind+1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
difAB = [a-b for a,b in zip(A,B)]
sum_difAB = sum(difAB)
if sum_difAB<0:
  print(-1)
elif sum_difAB == 0:
  print(N)
else:
  difAB.sort()
  neg_num = 0
  neg_sum = 0
  for dif in difAB:
    if dif < 0:
      neg_num += 1
      neg_sum += dif
    else:
      break
  
  pos_num = 0
  pos_sum = 0
  for dif in difAB[::-1]:
    if pos_sum + neg_sum < 0:
      pos_num += 1
      pos_sum += dif
    else:
      break
  print(neg_num+pos_num)
  print()

Solution (Python):
import sys

N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))

up = []
dsum = 0
dnum = 0

for i in range(N):

    if A[i] > B[i]:
        up.append(A[i] - B[i])
    elif B[i] > A[i]:
        dsum += (B[i] - A[i])
        dnum += 1

up.sort()
up.reverse()

if sum(up) < dsum:
    print (-1)
    sys.exit()
elif dnum == 0:
    print (0)
    sys.exit()

usum = 0

for i in range(len(up)):

    usum += up[i]

    if usum >= dsum:
        print (dnum + i + 1)
        sys.exit()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
plus = []
total_minus = 0
for i in range(n):
    diff = a[i] - b[i]
    if diff >= 0:
        plus.append(diff)
    else:
        total_minus += (-1) * diff
plus.sort(reverse=True)
if total_minus == 0:
    print(0)
elif sum(plus) < total_minus:
    print(-1)
else:
    ans = n - len(plus)
    for i in range(n):
        if total_minus <= 0:
            break
        total_minus -= plus[i]
        ans += 1
    print(ans)

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
plus = []
minus = []
for i in range(n):
    diff = a[i] - b[i]
    if diff >= 0:
        plus.append(diff)
    else:
        minus.append((-1) * diff)
plus.sort(reverse=True)
plus = collections.deque(plus)
if len(minus) == 0:
    print(0)
    exit()
if sum(plus) < sum(minus):
    print(-1)
    exit()
ans = len(minus)
total = sum(minus)
while total > 0:
    total -= plus.popleft()
    ans += 1
print(ans)

Solution (Python):
N = int(input())
A = map(int, input().split())
B = map(int, input().split())
D = [0] * N

minus = 0
cnt = 0
for i, j in enumerate(zip(A, B)):
    d = j[0] - j[1]
    D[i] = d
    if d < 0:
        minus += d
        cnt += 1

if minus >= 0:
    print(0)
    exit()

D.sort(reverse=True)
for idx, d in enumerate(D):
    if d > 0:
        minus = minus + d
        if minus >= 0:
            print(idx + 1 + cnt)
            exit()
    else:
        break

print(-1)

Solution (Python):
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()
chsum = 0
ans = 0
if sum(c)<0:
  print(-1)
  exit()
for i in range(n):
  if c[i] < 0:
    chsum -= c[i]
    c[i] = 0
    ans += 1
for i in range(n-1,-1,-1):
  if chsum > 0:
    chsum -= c[i]
    c[i] -= (min(chsum,c[i]))
    ans += 1
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

suf = []
insuf = []
S1 = 0
S2 = 0

for i in range(N):
    if A[i] < B[i]:
        insuf.append(B[i]-A[i])
    elif A[i] > B[i]:
        suf.append(A[i]-B[i])

if len(insuf) == 0:
    print(0)
    exit()
else:
    S1 = sum(insuf)
    suf.sort(reverse=True)
    for i in range(len(suf)):
        S2 += suf[i]
        if S2 >= S1:
            ans = len(insuf) + i + 1
            break

    if S2 < S1:
        ans = -1

    print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
S = 0
cnt = 0
for i in range(N):
    if a[i] < b[i]:
        S += (b[i] - a[i])
        cnt += 1
if S == 0:
    print(0)
    exit()
T = 0
sumlst = []
for i in range(N):
    if a[i] >= b[i]:
        sumlst.append(a[i] - b[i])

sumlst.sort(reverse=True)
for i in sumlst:
    T += i
    cnt += 1
    if T >= S:
        break
if S > T:
    print(-1)
    exit()
print(cnt)

Solution (Python):
import numpy as np
import sys
input = sys.stdin.buffer.readline

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

c = a - b
m = np.sum(c[c < 0])
p = np.sort(c[c >= 0])[::-1]
cnt = n - p.size

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

for i in p:
    m += i
    cnt += 1
    if m >= 0:
        print(cnt)
        exit()
print(-1)

Solution (Python):

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    D = []
    short = 0
    count = 0
    for i in range(N):
        now = A[i]-B[i]
        D.append(now)
        if now < 0:
            short += now
            count += 1

    if sum(D)<0:
        print(-1)
        exit()
    
    D.sort(reverse=True)
    
    for i in range(N):
        if short>=0:
            print(count)
            exit()    
        short += D[i]
        count += 1
    print(N)
    

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

if sum(A) < sum(B) :
  print(-1) 
else :
  l = []
  ret = 0
  rest = 0
  for a, b in zip(A, B) :
    if a <= b :
      if a != b :
        ret += 1
      rest += b - a
    else :
      l.append(a - b)

  l.sort()
  while rest > 0 :
    ret += 1
    rest -= l.pop()

  print(ret)

Solution (Python):
from collections import deque
n=int(input())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
ans=0
m=0
p=0
pl=deque()
for a,b in zip(al,bl):
    if a<b:
        m+=b-a
        ans+=1
    else:
        p+=a-b
        pl.append(a-b)
if m==0:
    print(0)
    exit()
if m>p:
    print(-1)
    exit()
pl=sorted(pl)
t=0
while t<m:
    t+=pl.pop()
    ans+=1
print(ans)

Solution (Python):
from itertools import accumulate
import bisect
n = int(input())
d = []
c = 0
co = 0
for i,j in zip(map(int,input().split()),map(int,input().split())):
  if i - j > 0:
    d.append(i-j)
  elif i - j < 0:
    c += i-j
    co += 1
d.sort(reverse=1)
if sum(d) + c < 0:
  print(-1)
elif c == 0:
  print(0)
else:
  d = list(accumulate(d))
  print(bisect.bisect_left(d,-c)+co+1)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a)<sum(b):
    print(-1)
else:
    c=[0 for i in range(n)]
    for j in range(n):
        c[j]=a[j]-b[j]
    c=sorted(c,reverse=True)
    d=c.copy()
    first=0
    second=n-1
    flag=True
    while flag:
        if c[second]>=0:
            flag=False
        else:
            if c[first]>=-c[second]:
                c[first]+=c[second]
                c[second]=0
                second-=1
            elif c[first]<-c[second]:
                c[second]+=c[first]
                c[first]=0
                first+=1
    ans=0
    for k in range(n):
        if d[k]!=c[k]:
            ans+=1
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if sum(A)>=sum(B):
    neg = []
    pos = []
    for i in range(N):
        d = A[i]-B[i]
        if d<0:
            neg.append(-d)
        elif d>0:
            pos.append(d)
    negsum = sum(neg)
    pos.sort(reverse=True)
    possum = 0
    for i in range(len(pos)+1):
        if possum >= negsum:
            print(i+len(neg))
            break
        possum += pos[i]
else:
    print(-1)

Solution (Python):
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)])
tot = sum(C)
cnt = 0
if tot<0:
    cnt = -1
else:
    a = 0
    for i in range(N):
        if C[i]<0:
            a += C[i]
            cnt += 1
        else:
            break
    for i in range(N-1,-1,-1):
        if a>=0:break
        a += C[i]
        cnt += 1
print(cnt)

Solution (Python):
from itertools import accumulate
import bisect
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = []
ans = 0
c = 0
for i in range(n):
  d = A[i]-B[i]
  if d > 0:
    C.append(d)
  elif d < 0:
    ans += 1
    c -= d
Q = list(accumulate(sorted(C, reverse=True)))
if c == 0:
  print(0)
elif sum(A) < sum(B):
  print(-1)
else:
  t = bisect.bisect_left(Q, c)
  ans += t+1
  print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
cnt=[]
num=0
sum=0
for i in range(n):
    q=a[i]-b[i]
    if q<0:
        num+=1
        sum-=q
    else:
        cnt.append(q)

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

else:
    res=num
    cnt.sort(reverse=True)
    p=0
    for i in range(len(cnt)):
        p+=cnt[i]
        res+=1
        if p>=sum:
            break

    if p>=sum:
        print(res)
        exit()

print(-1)

Solution (Python):
n=int(input())
a=list(map(int, input().split())) 
b=list(map(int, input().split())) 
countA=0
countA1=0
countB=0
countB1=0
v=[]
        
if sum(a)<sum(b):
    print(-1)
    exit()

for i in range(n):
    if a[i]>b[i]:
        v.append(a[i]-b[i])
    elif a[i]==b[i]:
        continue
    else:
        countA+=(b[i]-a[i])
        countA1+=1

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

v.sort()
v.reverse()

for i in range(len(v)):
    countB+=v[i]
    countB1+=1
    if countB>=countA:
        print(countA1+countB1)
        exit()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

dp=[];dn=[]
for i in range(N):
    if A[i]>B[i]:
        dp.append(A[i]-B[i])
    elif A[i]<B[i]:
        dn.append(B[i]-A[i])

if sum(dn)>sum(dp):
    ans=-1
elif len(dn)==0:
    ans=0
else:
    dnsum=sum(dn)
    dp.sort(reverse=True)
    ans=len(dn)
    for dpi in dp:
        dnsum-=dpi
        ans+=1
        if dnsum<=0:
            break

print(ans)

Solution (Python):

import sys
import numpy as np

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = np.array(sys.stdin.read().split(), dtype=np.int64)
    a = ab[:n]
    b = ab[n:]

    res = np.sort(a - b)
    if res.sum() < 0:
        print(-1)
        sys.exit()

    negative_cnt = np.sum(res < 0)
    if not negative_cnt:
        print(0)
        sys.exit()

    negative_sum = np.sum(res[:negative_cnt])
    positive_cnt = 0
    for i in range(n-1, -1, -1):
        negative_sum += res[i]
        positive_cnt += 1
        if negative_sum >= 0:
            break
    cnt = positive_cnt + negative_cnt

    print(cnt)
    
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
if sum(A) < sum(B):
  print(-1)
  exit()
S = 0
cnt = 0
AB = []
for i in range(N):
  if A[i] >= B[i]:
    AB.append(A[i] - B[i])
  else:
    S += B[i] - A[i]
    cnt += 1
AB = sorted(AB)[::-1]
i = 0
while S > 0:
  S -= AB[i]
  cnt += 1
  i += 1
print(cnt)