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):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

amari = sorted([A[i]-B[i] for i in range(N) if A[i]>B[i]])
fusoku = sum([B[i]-A[i] for i in range(N) if A[i] < B[i] ])
fusoku_count = len([B[i]-A[i] for i in range(N) if A[i] < B[i]] )
ans = fusoku_count
while fusoku > 0 :
    if not amari:
        print(-1)
        exit()
    fusoku -= amari.pop()
    ans +=1
print(ans)

Solution (Python):
import sys
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)
    sys.exit()
H=[]
num=0
ans=0
for i in range(N):
    if A[i]>=B[i]:
        c=A[i]-B[i]
        H.append(c)
    else:
        num+=B[i]-A[i]
        ans+=1
H=sorted(H,reverse=True)
if len(H)==N:
    print(0)
    sys.exit()
for i in range(len(H)):
    num-=H[i]
    ans+=1
    if num<=0:
        break
print(ans)

Solution (Python):
from collections import deque
N = int( input())
A = list( map( int, input().split()))
B = list( map( int, input().split()))
ans = 0
kata = []
need = []
for i in range(N):
    if A[i] < B[i]:
        need.append(B[i] - A[i])
        ans += 1
    elif B[i] < A[i]:
        kata.append(A[i] - B[i])
need.sort()
kata.sort(key=None, reverse = True)
now = 0
for n in need:
    while n > now:
        if kata:
            now += kata.pop(0)
            ans += 1
        else:
            ans = -1
            break
    now -= n
    if ans == -1:
        break
print( ans)

Solution (Python):
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)
else:
  C = [A[i] - B[i] for i in range(N)]
  D = sorted([c for c in C if c > 0])
  E = [c for c in C if c < 0]
  cnt = 0
  F = sum(E)
  while F < 0:
    F += D.pop()
    cnt += 1
  print(cnt + len(E))

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

prepare_sum = sum(A)
dif_sum = sum(B)
if prepare_sum < dif_sum:
    print(-1)

else:
    husoku = []
    enough = []
    idx = 0
    for a, b in zip(A,B):
        if a < b:
            husoku.append(idx)
        else:
            enough.append(idx)
        idx += 1

    residual = sorted(np.array(A)[enough] - np.array(B)[enough])

    score = 0
    budget = prepare_sum - dif_sum
    for r in residual:
        if r <= budget:
            score += 1
            budget -= r
        else:
            break
    print(len(A) - score)

Solution (Python):
while(True):
    try:
        n = int(input())
        a = list(map(int, input().split()))
        b = list(map(int, input().split()))
        extra = []
        loss = 0
        ans = 0
        for i in range(n):
            dif = a[i] - b[i]
            extra.append(dif)
            loss += min(0, dif)
            if dif < 0:
                ans += 1
        extra.sort(reverse=True)
        for num in extra:
            if loss < 0:
                loss += num
                ans += 1
            else:
                print(ans)
                exit()
        print(-1)
    except EOFError:
        exit()

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

if a < b:
	print(-1)

else:
	X = []
	for i in range(N):
		X.append(A[i] - B[i])
	X = sorted(X)
	
	x = 0
	c = 0
	for i in range(N):
		if X[i] < 0:
			x += X[i]
			c += 1
		else:
			break

	for i in range(N):
		if x >= 0:
			break
		else:
			c += 1
			x += X[-1 - i]

	print(c)

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:
    D = [A[i] - B[i] for i in range(N)]
    F = [True if D[i] >= 0 else False for i in range(N)]
    if sum(F) == len(F):
        print(0)
    else:
        Dp = [D[i] for i in range(N) if F[i] == True]
        Dm = [D[i] for i in range(N) if F[i] == False]
        Sm = sum(Dm)
        R  = len(Dm)
        Dp.sort()
        while Sm < 0:
            R  += 1
            Sm += Dp[-1]
            del Dp[-1]
        print(R)

Solution (Python):



from collections import deque
import heapq
import bisect
import math


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

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

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

    ex.sort(reverse=True)

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

    cur = 0
    for i in range(len(ex)):
        cur += ex[i]
        if cur >= need:
            print(cnt+i+1)
            exit()


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
c=[]
for i,j in zip(a,b):
    c.append(i-j)
cm=[i for i in c if i<0]
cp=[i for i in c if i>=0]
cp.sort(reverse=True)
scm=sum(cm)
ans=len(cm)
for i in cp:
    if scm>=0:break
    ans+=1
    scm+=i
if scm<0: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()
ans = 0
shortage = 0
for i in D:
	if i >= 0:
		break
	ans += 1
	shortage += i
if shortage < 0:
	D.reverse()
	for i in D:
		if i < 0:
			ans = -1
			break
		shortage += i
		ans += 1
		if shortage >= 0:
			break
print(ans)

Solution (Python):
import itertools
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
Cp = {}
ans = 0
Cm_sum = 0
for i in range(N):
    c = A[i] - B[i]
    if c > 0:
        Cp[c] = Cp.get(c, 0) + 1
    elif c < 0:
        ans += 1
        Cm_sum -= c
if Cm_sum == 0:
    print(0)
else:
    Cp_sum = 0
    for k in sorted(Cp, reverse=True):
        count = min(1 + (Cm_sum - Cp_sum - 1) // k, Cp[k])
        ans += count
        Cp_sum += k * count
        if Cp_sum >= Cm_sum:
            print(ans)
            exit()
    print(-1)

Solution (Python):
import sys
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a)<sum(b):
    print(-1)
    sys.exit()
    
need = 0
amari = []
ans = 0
for ai,bi in zip(a,b):
    dif = ai-bi
    if dif<0:
        need += abs(dif)
        ans += 1
    else:
        amari.append(dif)
amari.sort(reverse=True)
amari

s = 0
for amari_i in amari:
    if s<need:
        s += amari_i
        ans += 1
    else: break
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)
else:
    dif = [a - b for a, b in zip(A, B)]
    negative = [d for d in dif if d < 0]
    neg_sum = - sum(negative)
    ans = len(negative)
    positive = sorted([d for d in dif if d >= 0], reverse=True)
    i = 0
    while neg_sum > 0:
        neg_sum -= positive[i]
        i += 1
        ans += 1
    print(ans)

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

sa = [A[i]-B[i] for i in range(N)]
sa = sorted(sa)
ans=0
minus = 0
for i in sa:
  if i < 0:
    minus += i
    ans+=1
  if i >= 0:
    break

for i in sa[::-1]:
  if minus >= 0:
    break
  if i > 0:
    minus += i
    ans += 1
  if i <= 0:
    break
    
if minus < 0:
  print(-1)
else:
  print(ans)

Solution (Python):

def main():
    buf = input()
    N = int(buf)
    buf = input()
    buflist = buf.split()
    A = list(map(int, buflist))
    buf = input()
    buflist = buf.split()
    B = list(map(int, buflist))
    if sum(A) < sum(B):
        print(-1) 
        return
    diff = []
    for i in range(N):
        diff.append(A[i] - B[i])
    diff = list(sorted(diff))
    insufficient_value = 0
    insufficient_count = 0
    for i in range(N):
        if diff[i] < 0:
            insufficient_value += -diff[i]
            insufficient_count += 1
    deduction_count = 0
    pointer = -1
    while insufficient_value > 0:
        insufficient_value -= diff[pointer]
        deduction_count += 1
        pointer -= 1
    print(insufficient_count + deduction_count)


if __name__ == '__main__':
    main()

Solution (Python):


II = lambda: int(input())
MI = lambda: map(int, input().split())


def main():
    N = II()
    A = tuple(MI())
    B = tuple(MI())
    D = [A[i] - B[i] for i in range(N)]
    if sum(D) < 0: return -1

    md = tuple(filter(lambda d:d < 0, D))
    d = sum(md)
    if d == 0: return 0
    D = sorted(D, reverse=True)
    for i in range(N):
        d += D[i]
        if d >= 0: return len(md) + i + 1
    return -1


if __name__ == "__main__":
    print(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)
else:
    diff = [0] * N
    for i in range(N):
        diff[i] = A[i] - B[i]
    
    diff.sort()

    ans = 0

    l = 0
    r = N - 1

    point = 0
    while(True):
        left = diff[l]
        if left < 0:
            if point >= abs(left):
                ans += 1
                point -= abs(left)
                l += 1
            else:
                ans += 1
                point += diff[r]
                r -= 1
        else:
            break
    
    print(ans)

Solution (Python):
N = int(input())
a_list = [int(a) for a in input().split()]
b_list = [int(a) for a in input().split()]

if sum(a_list) < sum(b_list):
    print("-1")
else:
    d_list = [a - b for a, b in zip(a_list, b_list)]
    d_list.sort()
    minus = [d for d in d_list if d < 0]
    deficit = sum(minus)
    ans = len(minus)
    if deficit == 0:
        print("0")
    else:
        for i in range(N):
            deficit += d_list[-i - 1]
            if deficit >= 0:
                ans += i + 1
                break
        print(str(ans))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
plus=0
minus=[]
for a,b in zip(A,B):
    if a<b:
        ans += 1
        plus += (b-a)
    elif a>b:
        minus.append(a-b)
minus.sort(reverse=True)
while True:
    if plus<=0:
        print(ans)
        break
    if minus==[]:
        print(-1)
        break
    plus -= minus.pop(0)
    ans += 1

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    D = [0] * N
    if sum(A) < sum(B):
        print(-1)
        return
    retval = 0
    minussum = 0
    for i in range(N):
        D[i] = A[i] - B[i]
        if D[i] < 0:
            minussum += D[i]
            retval += 1
    D.sort(reverse=True)

    for d in D:
        if minussum >= 0:
            break
        if d > 0:
            minussum += d
            retval += 1
    print(retval)


if __name__ == "__main__":
    main()

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

D = [a - b for a, b in zip(A, B)]
over0s = []
sumMinus = 0
ans = 0
for d in D:
  if d < 0:
    ans += 1
    sumMinus += d
  else:
    over0s.append(d)
over0s.sort()
while sumMinus < 0:
  ans += 1
  sumMinus += over0s.pop()
print(ans)

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

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

LOW=[B[i]-A[i] for i in range(n) if B[i]>A[i]]
HIGH=[A[i]-B[i] for i in range(n) if A[i]>B[i]]

HIGH.sort(reverse=True)

ANS=len(LOW)
rest=sum(LOW)

i=0
while rest>0:
    if i==len(HIGH):
        print(-1)
        sys.exit()
    rest-=HIGH[i]
    i+=1
    ANS+=1

print(ANS)

Solution (Python):

from sys import stdin
import numpy as np

s_in = lambda: stdin.readline()[:-1]  
d_in = lambda: int(stdin.readline())  
ds_in = lambda: list(map(int, stdin.readline().split()))  


N = d_in()
A_list = np.array(ds_in(), dtype=int)
B_list = np.array(ds_in(), dtype=int)

if sum(A_list) < sum(B_list):
    print(-1)
    exit()

diff = A_list - B_list
neg = - diff[diff < 0].sum()
ans = (diff < 0).sum()

plus = np.sort(diff)[::-1]
for p in plus:
    if neg <= 0:
        break
    ans += 1
    neg -= p

print(ans)

Solution (Python):

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

C_plus = []
C_minus = []
for i in range(N):
    c = A[i]-B[i]
    if c>=0:
        C_plus.append(c)
    else:
        C_minus.append(c)
    
C_plus.sort()
C_plus = C_plus[::-1]

minus_num=len(C_minus)
minus=-sum(C_minus)

count=0
for i in range(len(C_plus)):
    if minus<=0:
        break
    minus-=C_plus[i]
    count+=1

if minus>0:
    print(-1)
else:
    print(minus_num+count)

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)]
if sum(c) < 0:
  print(-1)
else:
  c.sort()
  s = 0
  q = 0
  for i in range(n):
    if c[i] < 0:
      s += c[i]
      q += 1
    else:
      break
  for i in range(n):
    if s >= 0:
      print(q)
      break
    s += c[n-1-i]
    q += 1

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(reverse = 1)

rakudai = 0
fusoku = 0
for i in range(N):
    if D[i] < 0 :
        rakudai += 1
        fusoku -= D[i]

if sum(A) < sum(B):
    print(-1)
elif rakudai == 0:
    print(0)
else:
    hitsuyou = [0, 0]
    for i in range(N):
        hitsuyou[0] += 1
        hitsuyou[1] += D[i]
        if hitsuyou[1] >= fusoku:
            print(rakudai + hitsuyou[0])
            break

Solution (Python):

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

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

D = [a-b for a, b in zip(A, B)]
D.sort()


mi = 0
ans = 0
for d in D:
    if d < 0:
        mi += d
        ans += 1

for i, d in enumerate(D[::-1], 0):
    if mi >= 0:
        print(i+ans)
        exit()
    mi += d
print(i+ans)

Solution (Python):

N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

res = 0
diff = 0
D_list = [0]*N


for i in range(N):
    if A_list[i] < B_list[i]:
        diff += B_list[i] - A_list[i] 
        res += 1
    else:
        D_list[i] = B_list[i] - A_list[i]

D_list_s = list(sorted(D_list))

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

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

N = int(fin())
A_list = [int(elem) for elem in fin().split()]
B_list = [int(elem) for elem in fin().split()]

if sum(B_list) > sum(A_list):
    print(-1)
    exit(0)


surplus_list = []
debt_sum = 0
num_changed = 0
for i, (A, B) in enumerate(zip(A_list, B_list)):
    if A - B < 0:
        debt_sum += B - A
        num_changed += 1
    elif A - B > 0:
        surplus_list.append(A - B)

surplus_list.sort(reverse=True)
cur_sum_surplus = 0
for surplus in surplus_list:
    if cur_sum_surplus >= debt_sum:
        break
    else:
        cur_sum_surplus += surplus
        num_changed += 1
print(num_changed)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))


def solve(N, As, Bs):
    A_sum = sum(As)
    B_sum = sum(Bs)
    if A_sum < B_sum:
        return -1
    margin = A_sum - B_sum
    
    ans = 0
    need = 0
    extra = []
    for a, b in zip(As, Bs):
        if b > a:
            need += b - a
            ans += 1
        elif a > b:
            extra.append(a - b)
    extra.sort()
    extra.reverse()
    i = 0
    while need > 0:
        need -= extra[i]
        i += 1
        ans += 1
    return ans

print(solve(N, As, Bs))

Solution (Python):
n = input()
a = map(int, raw_input().split())
b = map(int, raw_input().split())
greater = []
req = 0
ans = 0
for i in xrange(n):
	if a[i] > b[i]:
		greater.append(a[i]-b[i])
	elif a[i] < b[i]:
		req += b[i] - a[i]
		ans += 1
if sum(greater) < req:
	print -1
else:
	greater.sort(reverse = True)
	ptr = 0
	while req > 0:
		req -= greater[ptr]
		ptr += 1
		ans += 1
	print ans

Solution (Python):
n=int(input())
*a,=map(int,input().split())
*b,=map(int,input().split())
c,d=[],[]
for s,t in zip(a,b):
    if s<t:
        c.append(t-s)
    elif s>t:
        d.append(s-t)
c.sort()
d.sort(reverse=1)
i=0
j=0
f=0
while i<len(c) and j<len(d):
    if c[i]<=d[j]:
        d[j]-=c[i]
        i+=1
        f=1
    elif c[i]>d[j]:
        c[i]-=d[j]
        j+=1
if i<len(c):
    print(-1)
else:
    print(i+j+f)

Solution (Python):
import sys
N, = map(int, raw_input().split())
A = map(int, raw_input().split())
B = map(int, raw_input().split())
if sum(A)<sum(B):
  print -1
  sys.exit(0)
dl = []
dm = []
for i in xrange(N):
  d = A[i]-B[i]
  if d>0:
    dm.append(d)
  if d<0:
    dl.append(d)
dm.sort()
sdl=abs(sum(dl))
j=0
sdm=0
for dmj in dm[::-1]:
  if sdm>=sdl:
    break
  j+=1
  sdm+=dmj
print len(dl)+j

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

C = []
minus = 0
ans = 0

for a, b in zip(A, B):
    C.append(a - b)
    if a - b < 0:
        minus += a - b
        ans += 1

C.sort(reverse = True)
for c in C:
    if minus >= 0:
        break
    minus += c
    ans += 1

print(ans)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
X=sum(A)
Y=sum(B)
if X<Y:
    print(-1)
    exit()
C=[A[i]-B[i] for i in range(N)]
L=[]
R=[]
for i in range(N):
    if C[i]<0:
        L.append(-C[i])
    else:
        R.append(C[i])
R.sort(reverse=True)
Z=sum(L)
K=0
M=len(R)
l=-1
for i in range(M):
    K+=R[i]
    if K>=Z:
        l=i+1
        break
if len(L)==0:
    print(0)
else:
    print(len(L)+l)

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

need = 0
small_num = 0
large = []
for a, b in zip(A, B):
    if a > b:
        large.append(a - b)
    elif a < b:
        need += b - a
        small_num += 1
large.sort()
for i, x in enumerate(reversed(large)):
    if need <= 0:
        print(i + small_num)
        quit()
    need -= x
if need <= 0:
    print(len(large) + small_num)
else:
    print(-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()
if all(a>=b for a,b in zip(A,B)):
    print(0)
    exit()

lack_v = lack_n = 0
gains = []
for a,b in zip(A,B):
    if a<b:
        lack_n += 1
        lack_v += b-a
    else:
        gains.append(a-b)

ans = lack_n
for g in sorted(gains, reverse=True):
    lack_v -= g
    ans += 1
    if lack_v <= 0:
        print(ans)
        exit()
print(-1)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
Over = []
Minus = 0
Change = 0
for i in range(N):
    if A[i] >= B[i]: Over.append(A[i]-B[i])
    else:
        Minus += B[i] - A[i]
        Change += 1
if Minus == 0: print(0)
else:
    Over.sort(reverse = True)
    for o in Over:
        if Minus >= o:
            Minus -= o
        else: Minus = 0
        Change += 1
        if Minus == 0: break
    if Minus > 0: print(-1)
    else: print(Change)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect

setrecursionlimit(10**7)

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()
A = reads()
B = reads()

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

failed = [B[i] - A[i] for i in range(N) if B[i] - A[i] > 0]
succ = [A[i] - B[i] for i in range(N) if A[i] - B[i] > 0]

ans = len(failed)
rem = sum(failed)
succ.sort()
while rem > 0:
  rem -= succ.pop()
  ans += 1
print(ans)

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

X = [] 
d = 0
c = 0

for i in range(N):
    if A[i] > B[i]:
        X.append(A[i]-B[i])
    elif A[i] < B[i]:
        d += B[i] - A[i]
        c += 1

if sum(X) < d:
    print(-1)
else:
    X = sorted(X)[::-1]
    i = 0
    while d > 0:
        d -= X[i]
        i += 1
        c += 1
        
    print(c)

Solution (Python):


import sys

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

yoyuu = [A[i]-B[i] for i in range(N)]
yoyuu.sort(reverse=True)

if sum(yoyuu) < 0:
    print(-1)
    sys.exit()
    
nega_sum = 0
ans = 0
for tmp in yoyuu:
    if tmp < 0:
        nega_sum -= tmp
        ans += 1
tmp_sum = 0
for tmp in yoyuu:
    if tmp_sum < nega_sum and tmp > 0:
        tmp_sum += tmp
        ans += 1
        
print(ans)

Solution (Python):
from collections import deque
import sys


n = int(input())

a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = []
mai = 0
maigo = 0
for i in range(n):
    c.append(a[i]-b[i])
    if a[i]-b[i]<0:
        mai += 1
        maigo -= a[i]-b[i]
c.sort(key=lambda x:-x)
if mai == 0:
    print(0)
    exit()
ans = mai
for i in range(n):
    if c[i] < 0:
        print(-1)
        exit()
    maigo -= c[i]
    ans += 1
    if maigo <= 0:
        print(ans)
        exit()

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
diff = [0] * n

ans = 0
com = 0

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

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

diff.sort()
d = -1
while d >= -1 * n and diff[d] > 0:
    ans += 1
    com += diff[d]
    d -= 1
    if com >= 0:
        print(ans)
        break
else:
    print(-1)

Solution (Python):
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    a = map(int, stdin.readline().split(), repeat(10, n))
    b = map(int, stdin.readline().split(), repeat(10, n))
    if sum(a) < sum(b):
        print -1
        return
    ans = s = 0
    p = []
    for i in xrange(n):
        if a[i] < b[i]:
            s += b[i] - a[i]
            ans += 1
        elif a[i] > b[i]:
            p.append(a[i] - b[i])
    p.sort(reverse=True)
    for x in p:
        if s <= 0:
            break
        s -= x
        ans += 1
    print ans
main()

Solution (Python):
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
xlst = []
ylst = []
for a, b in zip(alst, blst):
    if a < b:
        xlst.append(b - a)
    if a > b:
        ylst.append(a - b)

total = sum(xlst)
ans = len(xlst)
ylst.sort(reverse=True)
if total == 0:
    print(ans)
else:
    for y in ylst:
        total -= y
        ans += 1
        if total <= 0:
            print(ans)
            break
    else:
        print(-1)

Solution (Python):
from bisect import bisect_left

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a) < sum(b):
	print(-1)
else:
	excess = []
	needed_num = 0
	needed_score = 0
	for i in range(n):
		if a[i] > b[i]:
			excess.append(a[i]-b[i])
		elif a[i] < b[i]:
			needed_num += 1
			needed_score += b[i]-a[i]

	if needed_num == 0:
		print(0)
	else:
		excess.sort(reverse=True)
		e_sum = [excess[0]]
		for i in range(1, len(excess)):
			e_sum.append(e_sum[-1]+excess[i])
		ind = bisect_left(e_sum, needed_score)
		print(needed_num + ind+1)

Solution (Python):

import sys

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

n=int(input())
a=list(mii())
b=list(mii())

need=0
ans=0
helper=[]
for i in range(n):
    if a[i]>=b[i]:
        helper.append(a[i]-b[i])
    else:
        need+=b[i]-a[i]
        ans+=1

helper.sort()
while need>0 and helper:
    need-=helper[-1]
    helper.pop(-1)
    ans+=1

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