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
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil





def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    if sum(a) < sum(b):
        print(-1)
        return

    pos = []
    neg = 0
    for i in range(n):
        x = a[i] - b[i]
        if x >= 0:
            pos.append(x)
        else:
            neg += -x
    pos.sort(reverse=True)
    m = len(pos)
    i = 0
    cur = 0
    while i < m and cur < neg:
        cur += pos[i]
        i += 1
    print(n - len(pos) + i)
    


    
    
    
t = 1

for case in range(1,t+1):
    ans = solve()


"""

R s
S p
P r

"""

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
nega=0
posi=0
yoyu=[]
ans=0
for i in range(N):
  tmp=A[i]-B[i]
  if tmp>=0:
    posi+=tmp
    yoyu.append(tmp)
  else:
    nega+=-1*tmp
    ans+=1
if posi<nega:
  print(-1)
  exit()
elif nega==0:
  print(0)
  exit()

yoyu.sort(reverse=True)
S=0
i=0
while (1):
  S+=yoyu[i]
  if nega<=S:
    ans+=i+1
    print(ans)
    break
  else:
    i+=1

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

if sum(A) < sum(B):
  ans = -1
else:
  minus = 0
  plus = []
  for i in range(N):
    dif = A[i] - B[i]
    if dif < 0:
      minus -= dif
    else:
      plus.append(dif)
   
  plus.sort(reverse=True)
  if minus <= 0:
    ans = 0
  else:
    for i in range(len(plus)):
      minus -= plus[i]
      if minus <= 0:
        break
    ans = N-len(plus)+i+1
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
d = []
s = 0
for ai, bi in zip(a, b):
    if ai < bi:
        ans += 1
        s += bi - ai
    else:
        d.append(ai - bi)
if ans:
    d.sort(reverse=True)
    ds = 0
    for di in d:
        ds+=di
        ans+=1
        if ds >= s:
            break
    if ds < s:
        print(-1)
    else:
        print(ans)
else:
    print(0)

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

plus = []
minus = []
for i in range(N):
    tmp = A[i] - B[i]
    if tmp > 0:
        plus.append(tmp)
    elif tmp < 0:
        minus.append(tmp)
        
plus = sorted(plus, reverse=True)

plus_cum = []
for i in range(len(plus)):
    if i == 0:
        plus_cum.append(plus[0])
    else:
        plus_cum.append(plus_cum[i-1] + plus[i])
        
minus_sum = sum(minus)
ans = -1
for i in range(len(plus_cum)):
    if plus_cum[i] >= -minus_sum:
        ans = i+1
        break

if len(minus) == 0:
    print(0)
elif ans == -1:
    print(ans)
else:
    print(ans + len(minus))

Solution (Python):


import sys
n = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

for i in range(n):
    if A[i] < B[i]:
        break
else:
    print(0)
    sys.exit()
s = 0
ans = 0
t = []
for i in range(n):
    if A[i] < B[i]:
        s += B[i] - A[i]
        ans += 1
    t.append(A[i] - B[i])
t.sort(reverse=True)

for i in range(n):
    if s < 0:
        break
    s -= t[i]
    ans += 1
if s > 0:
    print(-1)
else:
    print(ans)

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

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

ans = len(S_ba)
now = 0
rem = sum(S_ba)
S_ab.sort(reverse=True)

for i in range(len(S_ab)):
    if rem <= now: break
    now += S_ab[i]
    ans += 1

if rem > sum(S_ab): print(-1)
else: print(ans)

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

S_ba = []
S_ab = []
for i in range(N):
    if A[i] < B[i]: S_ba.append(B[i] - A[i])
    
    else: S_ab.append(A[i] - B[i])

ans = len(S_ba)
now = 0
rem = sum(S_ba)

S_ab.sort(reverse=True)

if rem > sum(S_ab):
    print(-1)
    exit()
for i in range(len(S_ab)):
    if rem <= now: break
    now += S_ab[i]
    ans += 1
print(ans)

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

S_ba = []
S_ab = []
for i in range(N):
    if A[i] < B[i]: S_ba.append(B[i] - A[i])
    
    else: S_ab.append(A[i] - B[i])

if sum(S_ba) > sum(S_ab):
    print(-1)
    exit()
    
ans = len(S_ba)
now = 0
rem = sum(S_ba)
S_ab.sort(reverse=True)
for i in range(len(S_ab)):
    if rem <= now: break
    now += S_ab[i]
    ans += 1

print(ans)

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

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

if sum(S_ba) > sum(S_ab):
    print(-1)
    exit()
    
ans = len(S_ba)
now = 0
rem = sum(S_ba)
S_ab.sort(reverse=True)
for i in range(len(S_ab)):
    if rem <= now: break
    now += S_ab[i]
    ans += 1

print(ans)

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

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

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

amari = []
fusoku = []
for i in range(N):
  if A[i] < B[i]:
    fusoku.append(B[i]-A[i])
  elif A[i] > B[i]:
    amari.append(A[i]-B[i])
amari.sort(reverse=True)
ans = len(fusoku)
nokori = sum(fusoku)
now = 0
while nokori > 0:
  nokori -= amari[now]
  now += 1
  ans += 1
print(ans)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n=int(input())
a=lnii()
b=lnii()

plus=[]
minus=0
ans=0
for i in range(n):
  if a[i]<b[i]:
    minus+=b[i]-a[i]
    ans+=1
  else:
    plus.append(a[i]-b[i])

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

plus.sort(reverse=True)
for i in range(len(plus)):
  minus-=plus[i]
  ans+=1
  if minus<=0:
    print(ans)
    exit()

print(-1)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n=int(input())
a=lnii()
b=lnii()

plus=[]
minus=0
ans=0
for i in range(n):
  if a[i]<b[i]:
    minus+=b[i]-a[i]
    ans+=1
  else:
    plus.append(a[i]-b[i])

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

plus.sort(reverse=True)
for i in range(len(plus)):
  minus-=plus[i]
  ans+=1
  if minus<=0:
    print(ans)
    exit()

print(-1)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
b = inpl()
p = []
m = 0
c = 0
for i in range(n):
    if a[i] - b[i] < 0:
        m += b[i]-a[i]
        c += 1
    elif a[i]-b[i] > 0:
        p.append(a[i] - b[i])
p.sort(reverse = True)
if m > sum(p):
    print(-1)
    quit()
for x in p:
    if m <= 0:
        break
    c += 1
    m -= x
print(c)

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

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

  more = []
  less = []

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

  if len(less) == 0:
    print(0)
    return

  ans = len(less)

  v = sum(less)
  more.sort(reverse=True)
  for i in range(len(more)):
    if v <= 0:
      break
    v -= more[i]
    ans += 1

  print(ans)

  return

if __name__ == "__main__":
  resolve()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
P = sorted([A[i]-B[i] for i in range(N) if A[i]-B[i]>0],reverse=True)
C = [0 for _ in range(N+1)]
for i in range(1,len(P)+1):
    C[i] = C[i-1]+P[i-1]
M = [B[i]-A[i] for i in range(N) if B[i]-A[i]>0]
p = sum(P)
m = sum(M)
if m>p:
    ans = -1
else:
    if len(M)==0:
        ans = 0
    else:
        for i in range(1,len(P)+1):
            if C[i]>=m:
                ans = i
                break
        ans += len(M)
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
Bs = list(mapint())

if sum(As)<sum(Bs):
    print(-1)
else:
    minus = 0
    cnt = 0
    lis = []
    for a, b in zip(As, Bs):
        if a<b:
            minus += (b-a)
            cnt += 1
        else:
            lis.append(a-b)
    lis.sort(reverse=True)
    if minus==0:
        print(0)
    else:
        for l in lis:
            minus -= l
            cnt += 1
            if minus<=0:
                break
        print(cnt)

Solution (Python):
import sys
from collections import defaultdict, deque, Counter
import math


from bisect import bisect_left, bisect_right
import heapq




input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD


def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret

def solve():

    n = getN()
    anums = getList()
    bnums = getList()
    if sum(anums) < sum(bnums):
        print(-1)
        return
    husoku = 0
    ans = 0
    tari = []
    for a, b in zip(anums, bnums):
        if b > a:
            husoku += b - a
            ans += 1
        else:
            tari.append(a - b)

    tari.sort(reverse=True)
    if husoku == 0:
        print(0)
        return
    
    for t in tari:
        ans += 1
        husoku -= t
        if husoku <= 0:
            print(ans)
            return
def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    
    solve()

Solution (Python):
import bisect
from itertools import accumulate
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(B) > sum(A):
    print(-1)
else:
    donor = []
    acceptance = []
    for a, b in zip(A, B):
        if a > b:
            donor.append(a-b)
        elif a < b:
            acceptance.append(b-a)
    donor.sort(reverse=True)

    ans = len(acceptance)
    ans += bisect.bisect_left([0]+list(accumulate(donor)), sum(acceptance))
    print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(B) > sum(A):
    print(-1)
else:
    donor = []
    acceptance = []
    for a, b in zip(A, B):
        if a > b:
            donor.append(a-b)
        elif a < b:
            acceptance.append(b-a)
    donor.sort()

    ans = 0
    stock = 0
    for x in acceptance:
        ans += 1
        while stock < x:
            stock += donor.pop()
            ans += 1
        else:
            stock -= x
    print(ans)

Solution (Python):
from itertools import accumulate
n, *AB = map(int, open(0).read().split())
A, B = AB[:n], AB[n:]
P = []
N = []
for a, b in zip(A, B):
    if b-a > 0:
        P.append(b-a)
    elif b-a < 0:
        N.append(a-b)
N.sort(reverse=1)
s = sum(P)
if s == 0:
    print(0)
else:
    for i, x in enumerate(accumulate(N), 1):
        if x >= s:
            print(len(P) + i)
            break
    else:
        print(-1)

Solution (Python):
import numpy as np
from bisect import bisect_right

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

diff = [0 for i in range(N)]
need = 0
ans = 0

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

diff = sorted(diff, reverse=True)  
cumsum = np.cumsum(diff)

if need != 0:
    ans += bisect_right(cumsum, need)+1
if ans > N:
    ans = -1
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
p=[]
m=[]
def app(num1):
    p.append(num1)
def apm(num2):
    m.append(num2)
if sum(a)<sum(b):
    ans=-1
else:
    for i in range(n):
        if a[i]>b[i]:
            app(a[i]-b[i])
        elif a[i]<b[i]:
            apm(b[i]-a[i])
    num=sum(m)
    ans=len(m)
    p.sort(reverse=True)
    num1=0
    for i in range(len(p)):
        if num1>=num:
            break
        num1+=p[i]
        ans+=1
        if num1>=num:
            break
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
if sum(A) < sum(B):
  print(-1)
  exit()

D = []
for i in range(N):
  D += [A[i]-B[i]]
D.sort()
M = 0
for i in range(N):
  if D[i] < 0:
    M += D[i]
    ans += 1
  else:
    break
for i in reversed(range(N)):
  if M >= 0:
    print(ans)
    break
  else:
    M += D[i]
    ans += 1

Solution (Python):
def main():
    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)
    else:
        D = sorted([d for a, b in zip(A, B) if (d := a - b) > 0])
        c = sum([a != b for a, b in zip(A, B)])
        for d in D:
            if d <= s:
                c -= 1
                s -= d
            else:
                break
        if s > 0 and len(A) == len(D):
            c += 1
        print(c)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
d = [0]*n
e = 0
t = 0
for i in range(n):
    d[i] = a[i] - b[i]
    if d[i] < 0:
        e += 1
        t -= d[i]
d = sorted(d,reverse=True)
if sum(d) < 0:
    ans = -1
else:
    s = 0
    for i in range(n):
        if s >= t:
            break
        else:
            s += d[i]
    ans = i + e
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline


def log(*args):
    print(*args, file=sys.stderr)


def main():
    n = int(input())
    a = list(map(int, input().rstrip().split()))
    b = list(map(int, input().rstrip().split()))
    minus_sum = 0
    pluses = []
    ans = 0
    for i in range(n):
        if a[i] < b[i]:
            minus_sum += b[i] - a[i]
            ans += 1
        else:
            pluses.append(a[i] - b[i])
    pluses.sort(reverse=True)
    for v in pluses:
        if minus_sum <= 0:
            break
        minus_sum -= v
        ans += 1
    if minus_sum > 0:
        print(-1)
    else:
        print(ans)


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()

surplus = []
lack = []
for i in range(n):
    if a[i] > b[i]:
        surplus.append(a[i] - b[i])
    elif a[i] < b[i]:
        
        lack.append(b[i] - a[i])
if len(lack) == 0:
    print(0)
    exit()

surplus = sorted(surplus, reverse=True)
lack = sorted(lack, reverse=True)



right = 0
ans = 1
for left in range(len(surplus)):
    while right < len(lack) and surplus[left] - lack[right] >= 0:
        surplus[left] -= lack[right]
        ans += 1
        right += 1
    if right == len(lack):
        break
    
    if right < len(lack):
        
        ans += 1
        
        lack[right] -= surplus[left]
print(ans),

Solution (Python):
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()
b = LIST()

if sum(a) < sum(b):
    print(-1)
else:
    c = sorted([a[i]-b[i] for i in range(n)])
    ans = 0
    reg = 0

    for x in c:
        if x < 0:
            ans += 1
            reg += x
        else:
            break

    for x in c[::-1]:
        if reg < 0:
            reg += x
            ans += 1
        else:
            break

    print(ans)

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

    ans = 0
    P = []
    v = 0
    for a, b in zip(A, B):
        if a >= b:
            P.append(a - b)
        else:
            v += b-a
            ans += 1
    P.sort(reverse=True)

    for p in P:
        if v <= 0:
            break
        v -= p
        ans += 1
    if v <= 0:
        print(ans)
    else:
        print(-1)


if __name__ == '__main__':
    main()

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

if(sum(a)<sum(b)):
    print(-1)
else:
    d=[a[i]-b[i] for i in range(n)]
    d.sort()

    ans=0
    minus_num=bisect.bisect_left(d, 0)
    ans+=minus_num
    minus_sum=sum(d[:minus_num])

    i=n-1
    while(minus_sum<0 and minus_num<=i):
        minus_sum+=d[i]
        i-=1
        ans+=1

    print(ans)

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

shortage = [b - a for a, b in zip(A, B) if a < b]
over = sorted([a - b for a, b in zip(A, B) if a > b], reverse=True)

sum_shortage = sum(shortage)
sum_over = 0
count = -1
if sum_shortage == 0:
    count = 0
elif sum_shortage > sum_over:
    for i, v in enumerate(over):
        sum_over += v
        if sum_shortage <= sum_over:
            count = len(shortage) + i + 1
            break
print(count)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
dis = []
count = 0
for i,j in zip(a,b):
    if j > i:
        ans += 1
        count += j-i
    else:
        dis.append(i-j)
dis.sort(reverse=True)
if count > 0 and dis == []:
    print(-1)
else:
    now = 0
    while now < len(dis) and count > 0:
        ans += 1
        count -= dis[now]
        now += 1
    if count > 0:
        print(-1)
    else:
        print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
dis = []
count = 0
for i,j in zip(a,b):
    if j > i:
        ans += 1
        count += j-i
    else:
        dis.append(i-j)
dis.sort(reverse=True)
if count > 0 and dis == []:
    print(-1)
else:
    now = 0
    while now < len(dis) and count > 0:
        ans += 1
        count -= dis[now]
        now += 1
    if count > 0:
        print(-1)
    else:
        print(ans)

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

diff = [0] * N
neg = []
pos = []
for i in range(N):
    diff[i] = A[i] - B[i]
    if diff[i] < 0:
        neg.append(diff[i])
    else:
        pos.append(diff[i])

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

tobe_supplied = sum(neg)
pos.sort(reverse=True)

cnt = len(neg)

for p in pos:
    if tobe_supplied >= 0:
        break
    cnt += 1
    tobe_supplied += p    

print(cnt)

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:
  diffs = [a - b for a, b in zip(A, B)]
  diffs.sort()
  changed = set()
  l = 0
  r = n - 1
  while l < r:
    if diffs[l] >= 0:
      break
    if diffs[r] + diffs[l] == 0:
      changed.add(l)
      changed.add(r)
      l += 1
      r -= 1
    elif diffs[r] + diffs[l] > 0:
      changed.add(l)
      changed.add(r)
      diffs[r] += diffs[l]
      l += 1
    else:
      changed.add(l)
      changed.add(r)
      diffs[l] += diffs[r]
      r -= 1
  print(len(changed))

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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 nega > 0:
    posi.sort(reverse=True)
    for i, x in enumerate(posi):
        nega -= x
        if nega <= 0:
            cnt += i + 1
            break
    else:
        cnt = -1
print(cnt)

Solution (Python):
import sys

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

sum_a=sum(a)
sum_b=sum(b)
c=[]
cnt=0
ans=0
d=0
if(sum_a<sum_b):
    print(-1)
else:
    for i in range(n):
        if a[i]<b[i]:
            cnt+=1
            d+=(b[i]-a[i])
        else:
            c.append(a[i]-b[i])
    if cnt==0:
        print(cnt)
        sys.exit()
    cc=sorted(c,reverse=True)
    cc_sum=0
    for i in range(n):
        cc_sum+=cc[i]
        if cc_sum>d:
            cnt+=(i+1)
            break
    print(cnt)

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    arr = [ai - bi for ai, bi in zip(a, b)]
    arr.sort(reverse=True)
    tmp_1 = sum([-ai for ai in arr if ai < 0])
    tmp_2 = sum([ai for ai in arr if ai > 0])
    ans = 0
    for ai in arr:
        if ai < 0:
            ans += 1
    if tmp_2 < tmp_1:
        print(-1)
    else:
        for ai in arr:
            if ai > 0:
                if tmp_1 > 0:
                    tmp_1 -= ai
                    ans += 1

        print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

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

plus_l=[]
minus_l=[]
cnt=0
for i in range(n):
    if a[i]<b[i]:
        minus_l.append(b[i]-a[i])
        cnt+=1
    elif a[i]>b[i]:
        plus_l.append(a[i]-b[i])

if sum(minus_l)>sum(plus_l):
    print(-1)
    exit()

sum_minus=sum(minus_l)
tmp_sum_plus=0

plus_l.sort(reverse=True)

for p_i in plus_l:
    if tmp_sum_plus>=sum_minus:
        break
    tmp_sum_plus+=p_i
    cnt+=1
print(cnt)

Solution (Python):
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
if sum(alst) - sum(blst) < 0:
    print(-1)
    exit()


ans = 0
res = []
plus = 0
for a, b in zip(alst, blst):
    if a < b:
        ans += 1
        plus += b - a
    else:
        res.append(a - b)
res.sort(reverse = True)
pos = 0
while plus > 0:
    plus -= res[pos]
    ans += 1
    pos += 1
print(ans)

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

c = sorted([ai - bi for ai, bi in zip(a, b)])
if sum(c) < 0:
    print(-1)
    exit()

ans, cnt = 0, 0
for i in c:
    if i >= 0:
        break
    ans += 1
    cnt += i
for i in c[::-1]:
    if cnt >= 0:
        print(ans)
        break
    ans += 1
    cnt += i

Solution (Python):
n = int(input())
a_li = list(map(int, input().split()))
b_li = list(map(int, input().split()))

if sum(a_li) < sum(b_li):
    print(-1)
    exit()
    
check = [0]*n
cnt = 0
SUM_ma = 0

for i in range(n):
    check[i] = a_li[i] - b_li[i]
    
    if check[i] < 0:
        cnt += 1
        SUM_ma += check[i]

if SUM_ma < 0:
        
    check.sort(reverse=True)
    
    for i in check:
        SUM_ma += i
        cnt += 1
        
        if SUM_ma >= 0:
            break
        
print(cnt)

Solution (Python):
import sys
 
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
 
import numpy as np
 
N = int(readline())
A = np.array(readline().split(), dtype=np.int64)
B = np.array(readline().split(), dtype=np.int64)
 
diff = A-B
lack = -diff[diff<0]
remain = diff[diff>0]
 
x = lack.sum()
remain.sort()
cum = remain[::-1].cumsum()
 
y = 0 if x == 0 else np.searchsorted(cum,x) + 1
 
answer = len(lack) + y if y <= len(remain) else -1
print(answer)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
pos = [10**10]+[a[i]-b[i] for i in range(n) if a[i] > b[i]]
neg = [b[i]-a[i] for i in range(n) if a[i] < b[i]]
pos.sort(reverse=True)
pos[0] = 0
for i in range(len(pos)-1): pos[i+1] += pos[i]
need = sum(neg)
ans = len(neg)
for i in range(len(pos)):
    if pos[i] >= need:
        ans += i
        break
else:
    ans = -1
print(ans)

Solution (Python):

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

D=[]

for i in range(N):
    D.append(A[i]-B[i])

D.sort()

k=0
need=0
ans=0
while D[k]<0 and k!=N:
    need+=D[k]
    k+=1
    ans+=1

l=-1
while need<0 and D[l]>0:
    need+=D[l]
    ans+=1
    l-=1

if need<0:
    print(-1)
else:
    print(ans)

Solution (Python):
import numpy as np
n = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
C = np.array([a-b for a, b in zip(A, B)], dtype='int64')
C.sort()
cnt_neg = np.sum(C < 0)
cnt_pos = np.sum(C > 0)
sum_neg = -1 * np.sum(C[C < 0])
sum_pos = np.sum(C[C > 0])
if sum_pos - sum_neg < 0:
    print(-1)
elif sum_neg == 0:
    print(0)
else:
    C = C[C>0]
    C.sort()
    C = C[::-1]
    C = np.cumsum(C)
    print(cnt_neg + np.count_nonzero(C < sum_neg) + 1)

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

c = 0
tmp = 0
m = []
for i in range(n):
    if a[i] < b[i]:
        c += 1
        tmp -= (b[i]-a[i])
    else:
        m.append(a[i]-b[i])

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

m.sort(reverse=True)
i = 0
while i < len(m):
    tmp += m[i]
    i += 1
    if tmp >= 0:
        print(c+i)
        exit()
        break
print(-1)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[]
SA=sum(A)
SB=sum(B)
S=SA-SB
cnt=0
ns=0
if SB>SA:
    print(-1)
    exit()
for a,b in zip(A,B):
    if a>b:
        C.append(a-b)
    elif a<b:
        ns+=b-a
        cnt+=1
C.sort(reverse=True)
for i in range(len(C)):
    if ns>0:
        cnt+=1
        ns-=C[i]
    else:break
print(cnt)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = [a-b for a,b in zip(A,B)]
A.sort()
if sum(A)<0:
    print(-1)
else:
    cnt = sum([a<0 for a in A])
    max_i = -1
    for i in range(n):
        while A[i]<0:
            if A[max_i] > abs(A[i]):
                A[max_i] += A[i]
                A[i] = 0
            else:
                A[i] += A[max_i]
                A[max_i] = 0
                max_i -= 1
    print(cnt-max_i if cnt!=0 else 0)

Solution (Python):
n=int(input())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))

plist=[]
nval=0
ncounter=0

for i in range(n):
	if alist[i] > blist[i]:
		plist.append(alist[i]-blist[i])
	elif alist[i] < blist[i]:
		nval += blist[i] - alist[i]
		ncounter += 1

plist.sort(reverse = True)
i=0
maxi=len(plist)
pcounter = 0

while nval != 0 and i < maxi:
	pcounter += 1
	nval = max(0,nval-plist[i])
	i += 1

if nval == 0:
	print(pcounter + ncounter)
else:
	print(-1)