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):
def main():
    n = int(input())
    A = input_list()
    B = input_list()
    C = [A[i] - B[i] for i in range(n)]
    s = sum(C)
    if s < 0:
        print(-1)
        exit()
    C.sort()
    ans = 0
    for i in range(n):
        if C[i] < 0:
            continue
        elif C[i] <= s:
            ans += 1
            s -= C[i]
        else:
            break
    print(n-ans)
    

def input_list():
    return list(map(int, input().split()))

if __name__ == "__main__":
    main()

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
diff = [0]*N
sumA = 0
sumB = 0
for i in range(N):
    sa = A[i] - B[i]
    diff[i] = sa
    sumA += A[i]
    sumB += B[i]
if sumA < sumB:
    print(-1)
    exit()
diff.sort(reverse= True)
fixidx = -1
usedI = False
c = 0
usedJ = []
for i in range(N):
    usedI = False
    if diff[i] > 0:
        for j in range(fixidx,-N,-1):
            
            if diff[j] < 0:
                if not usedI:
                    usedI = True
                    c += 1
                if diff[i] + diff[j] >= 0:
                    diff[i] += diff[j]
                    diff[j] = 0
                    usedJ.append(j)
                    fixidx = j+1
                    if diff[i] == 0:
                        break

                else:
                    diff[j] += diff[i]
                    diff[i] = 0
 
                    usedJ.append(j)
                    fixidx = j

                    break
            elif diff[j] >= 0:
                
                print(c+len(set(usedJ)))
                exit()
    else:
        break

print(c+len(set(usedJ)))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    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

    plus = []
    totalsa = 0
    ans = 0
    for i in range(N):
        if A[i] > B[i]:
            plus.append(A[i] - B[i])
        elif A[i] < B[i]:
            totalsa += B[i] - A[i]
            ans += 1

    plus.sort(reverse=True)

    t = 0
    for i in range(len(plus)):
        if t >= totalsa:
            break
        ans += 1
        t += plus[i]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s_a = sum(a)
s_b = sum(b)
if s_b > s_a:
    print(-1)
    exit()
dist_s = s_a - s_b
dist_list = []
for i in range(n):
    if a[i] >= b[i]:
        dist_list.append(a[i]-b[i])
dist_list.sort()
m = len(dist_list)
count = 0
ans = n
while dist_s > 0 and count < m:
    dist_s -= dist_list[count]
    ans -= 1
    count += 1
    if dist_s < 0:
        ans += 1
print(ans)

Solution (Python):

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
diff = [a-b for a,b in zip(A,B)]
diff = sorted(diff,reverse = True)
negative_v = [i for i in diff if i < 0]
tot = sum(negative_v)

if sum(A) < sum(B):
    print(-1)
else:
    if tot >= 0:
        print(0)
    else:
        cnt = 0
        for d in diff:
            tot += d
            
            if tot < 0:
                cnt += 1
            else:
                cnt += 1
                break
        ans = cnt + len(negative_v)
        print(ans)

Solution (Python):
import sys
import heapq 

input = sys.stdin.readline

n = int(input())

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

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

mainasu_a_b = 0

plus_a_b_list = []
ans = 0
for i in range(n):
    if a_list[i] - b_list[i] < 0:
        mainasu_a_b += a_list[i] - b_list[i]
        ans += 1
    else:
        plus_a_b_list.append(a_list[i] - b_list[i])

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

plus_a_b_list = list(map(lambda x: x*(-1), plus_a_b_list))
heapq.heapify(plus_a_b_list)
while mainasu_a_b < 0:
    if len(plus_a_b_list) == 0 and mainasu_a_b < 0:
        print(-1)
        exit(0)
    max_plus = heapq.heappop(plus_a_b_list)*(-1)
    mainasu_a_b += max_plus
    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):
  print(-1)
  exit()
ans=0
ab=[]
cd=[]
for i in range(n):
  m=a[i]-b[i]
  if m<0:
    ab.append(m)
  else:
    cd.append(m)
res=sum(ab)
cd.sort(reverse=True)
t=0
if len(ab)>0:
  while t<len(cd):
    res+=cd[t]
    t+=1
    if res>0:
      print(len(ab)+t)
      exit()
else:
  print(0)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
l=[]
x=0
y=0
z=0
for i in range(n):
    if a[i]>=b[i]:
        x+=1
        l.append(a[i]-b[i])
    else:
        y+=1
        z+=a[i]-b[i]
l.sort(reverse=True)


if x==n:
    print(0)
    exit()
ans=y
for i in range(x):
    z+=l[i]
    ans+=1
    if z>=0:
        print(ans)
        exit()
if z<0:
    print(-1)

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

insufficient = 0
yoyu = []

ans = 0
for i in range(n):
    p = A[i] - B[i]
    if p < 0:
        insufficient += abs(p)
        ans += 1
    else:
        yoyu.append(p)
yoyu.sort(reverse=True)

if sum(yoyu) < insufficient:
    print(-1)
    exit()

ind = 0    
while insufficient > 0:
    insufficient -= yoyu[ind]
    ind += 1

print(ans+ind)

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a = LI()
    b = LI()
    need = 0
    ans = 0
    surplus = []
    for i in range(n):
        if a[i]>=b[i]:
            surplus.append(a[i]-b[i])
        else:
            need += b[i] - a[i]
            ans += 1
    
    surplus.sort(reverse=True)

    if need <= 0:
        print(0)
        exit(0)
    else:
        for r in surplus:
            need -= r
            ans += 1
            if need <= 0:
                print(ans)
                exit(0)
        
        print(-1)
main()

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

Solution (Python):
from collections import deque
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

AB_list = sorted([A-B for A,B in zip(A_list, B_list)])
AB_q = deque(AB_list)

if sum(AB_list) < 0:
    print(-1)
    exit()
if AB_list[0] >= 0:
    print(0)
    exit()

cnt = 0
stock = 0
while len(AB_q) > 0:
    if stock <= 0:
        stock += AB_q.pop()
    else:
        lv = AB_q.popleft()
        if lv >= 0:
            break
        stock += lv
    cnt += 1
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 0
t = []
ans = 0
for i in range(n):
    if a[i] < b[i]:
        c += b[i] - a[i]
        ans += 1
    else:
        t.append(a[i]-b[i])
t.sort(reverse=True)
i = 0
while c > 0 and i < len(t):
    ans += 1
    c -= t[i]
    i += 1
if c > 0:
    print(-1)
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()
cnt = 0
d = [0] * n
s = 0
for i in range(n):
  d[i] = a[i]-b[i]
  if d[i] < 0:
    cnt += 1
    s += abs(d[i])
if cnt == 0:
  print (0)
  exit()

d.sort(reverse=True)

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

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

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import sin, cos, tan, asin, acos, atan

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]

N,A = ilint()
B = lint()
if sum(A)<sum(B):
    print(-1)
    exit()
index = list(range(N))
index.sort(reverse=True, key=lambda x:B[x]-A[x])
ans = tmp = 0
for i in index:
    if B[i]-A[i]<=0:
        break
    tmp += B[i]-A[i]
    ans += 1
for i in index[::-1]:
    if tmp==0:
        break
    tmp -= min(tmp,A[i]-B[i])
    ans += 1
print(ans)

Solution (Python):
import sys, math
from functools import lru_cache
from collections import defaultdict
from itertools import accumulate
import bisect
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    A = list(mi())
    B = list(mi())
    if sum(A) < sum(B):
        print(-1)
        return

    short = []
    excess = []
    for i in range(N):
        if A[i] < B[i]:
            short.append(B[i]-A[i])
        if A[i] > B[i]:
            excess.append(A[i]-B[i])
    
    if not short:
        print(0)
        return

    excess.sort(reverse=True)
    acc = list(accumulate(excess))

    k = bisect.bisect_right(acc, sum(short))

    print(len(short)+k+1)




if __name__ == '__main__':
    main()

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

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

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

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


res=0
count=0
for i in range(N):
  if B[i]>A[i]:
    res += B[i]-A[i]
    count += 1
    
if count==0:
  print(0)
  exit()
  
plus = [A[i]-B[i] for i in range(N) if A[i]>B[i]]
plus = sorted(plus,reverse=True)

for i in range(len(plus)):
  res -= plus[i]
  count += 1
  if res<=0:break
    
print(count)

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

C = [x - y for x, y in zip(A, B)]
Minus = [i for i in C if i < 0]
minus = sum(Minus)
EX = [i for i in C if i > 0]
EX.sort(reverse=True)

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

i = 0
while minus < 0:
    minus += EX[i]
    i += 1

print(len(Minus) + i)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = [0] * n
    for i in range(n):
        c[i] = a[i] - b[i]
    c.sort()
    i = 0
    j = n - 1
    cnt = 0
    tmp = c[i] + c[j]
    while i != j:
        if c[i] < 0:
            if tmp >= 0:
                cnt += 1
                i += 1
                tmp += c[i]
            else:
                cnt += 1
                j -= 1
                tmp += c[j]
        else:
            break
    if tmp >= 0:
        print(cnt + 1 if cnt > 0 else 0)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Solution (Python):
import sys 

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

sum_C = sum(A)

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

lack_sum = 0
surplus_sum = []
count = 0

for i in range(N):
    if B[i] > A[i]:
        lack_sum = lack_sum + (B[i] - A[i])
        count = count + 1
    elif B[i] <= A[i]:
        surplus_sum.append(A[i]-B[i])

surplus_sum = sorted(surplus_sum,reverse=True)

if len(surplus_sum) == N:
    print(0)
    sys.exit()


for j in surplus_sum:
    lack_sum = lack_sum - j
    count = count + 1

    if lack_sum <= 0:
        print(count)
        sys.exit()

Solution (Python):


import sys

import heapq


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = input_int_list()
    B = input_int_list()
    plus = []
    minus = 0
    cnt = 0

    
    
    for i in range(n):
        d = B[i] - A[i]
        if d > 0:
            minus += d
            cnt += 1
        else:
            heapq.heappush(plus, d)

    while minus > 0 and plus:
        minus -= -heapq.heappop(plus)
        cnt += 1

    if minus <= 0:
        print(cnt)
    else:
        print(-1)
    return


if __name__ == "__main__":
    main()

Solution (Python):

import sys
import numpy as np


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = np.array(LI())
b = np.array(LI())
diff = a - b
pos = [diff[i] for i in range(len(diff)) if diff[i] > 0]
neg = [diff[i] for i in range(len(diff)) if diff[i] < 0]
if sum(pos) + sum(neg) < 0:
    print(-1)
    exit()





ans = len(neg)
s_neg = sum(neg)
sort_pos = sorted(pos)[::-1]
for elem in sort_pos:
    if s_neg < 0:
        ans += 1
        s_neg += elem
    else:
        break
print(ans)

Solution (Python):

import sys
import numpy as np


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = np.array(LI())
b = np.array(LI())
diff = a - b
pos = [diff[i] for i in range(len(diff)) if diff[i] > 0]
neg = [diff[i] for i in range(len(diff)) if diff[i] < 0]
if sum(pos) + sum(neg) < 0:
    print(-1)
    exit()

if len(neg) == 0:
    print(0)
    exit()

ans = len(neg)
s_neg = sum(neg)
sort_pos = sorted(pos)[::-1]
for elem in sort_pos:
    if s_neg < 0:
        ans += 1
        s_neg += elem
    else:
        break
print(ans)

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

l = [A - B for A,B in zip(a,b)]
if sum(l) < 0:
    print(-1)
    exit()

nega = [i for i in l if i < 0]
posi = [i for i in l if i > 0]
nega.sort()
posi.sort()

p = 0
ans = 0
for n in nega:
    ans += 1
    p += n
    if p <= 0 and posi:
        
        p += posi.pop()
        ans += 1

print(ans)

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)])
if sum(C)<0:print(-1);exit()
D=[i for i in C if i<0]
m=sum(D)
i=0
while m<0:m+=C[N-i-1];i+=1
print(len(D)+i)

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)])
if sum(C)<0:
    print(-1)
else:
    D=[i for i in C if i<0]
    m=sum(D)
    i=0
    while m<0:
        m+=C[N-i-1]
        i+=1
    print(len(D)+i)

Solution (Python):
import bisect
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)])
if sum(C) < 0:
    print(-1)
elif min(C) >= 0:
    print(0)
else:
    D = [i for i in C if i < 0]
    m = sum(D)
    for i in range(N-1,-1,-1):
        m += C[i]
        if m >= 0:
            print(len(D)-i+N)
            break

Solution (Python):
import bisect
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)])
if sum(C) < 0:
    print(-1)
elif min(C) >= 0:
    print(0)
else:
    D = sorted([i for i in C if i > 0], reverse=True)
    r = [D[0]]
    for i in range(len(D)-1):
        r.append(r[i] + D[i+1])
    print(len([i for i in C if i < 0])+bisect.bisect_left(r, sum(-i for i in C if i < 0))+1)

Solution (Python):
import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = []
for i in range(N):
    C.append(A[i] - B[i])
C.sort()
if sum(-i for i in C if i < 0) > sum(i for i in C if i > 0):
    print(-1)
else:
    if min(C) >= 0:
        print(0)
    else:
        D = sorted([i for i in C if i > 0], reverse=True)
        r = [D[0]]
        for i in range(len(D)-1):
            r.append(r[i] + D[i+1])
        print(len([i for i in C if i < 0])+bisect.bisect_left(r, sum(-i for i in C if i < 0))+1)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7







N = getN()
A = getList()
B = getList()
plus = []
minus = []
for i in range(N):
    score = A[i] - B[i]
    if score < 0:
        minus.append(score)
    else:
        plus.append(score)
plus.sort(reverse = True)

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

cnt = sum(minus)
ans = len(minus)
for i in range(N - ans):
    cnt += plus[i]
    ans += 1
    if cnt >= 0:
        print(ans)
        exit()
print(-1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
positive = []
negative = []
for a, b in zip(A,B):
    now = a - b
    if now > 0:
        positive.append(now)
    elif now < 0:
        negative.append(-now)
if not negative:
    print(0)
    exit()
if sum(positive) < sum(negative):
    print(-1)
    exit()
positive.sort(reverse=True)
n_sum = sum(negative)
ans = 0
for p in positive:
    ans += 1
    n_sum -= p
    if n_sum <= 0:
        break
print(ans + len(negative))

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

pos = []
neg = []
for i in range(n):
    d = a[i] - b[i]
    if d >= 0:
        pos.append(d)
    else:
        neg.append(-d)

pos.sort()


cnt = len(neg)
val = 0
while len(neg) > 0:
    n = neg.pop()
    while n > val:
        if len(pos) == 0:
            print(-1)
            exit()
        val += pos.pop()
        cnt += 1
    val -= n

print(cnt)

Solution (Python):
n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))

s = 0
num = 0
for i in range(n):
    if blis[i] - alis[i] > 0:
        s += blis[i] - alis[i]
        num += 1

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

nlis = []
for i in range(n):
    if alis[i]-blis[i] >= 0:
        nlis.append(alis[i]-blis[i])

nlis.sort(reverse=True)
tmp = 0
cnt = 0
for i in nlis:
    if tmp >= s:
        break
    tmp += i
    cnt += 1

if tmp < s:
    print(-1)
else:
    print(cnt+num)

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

Solution (Python):
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

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

lst.sort()


for i in range(len(lst))[::-1]:
    if cnt<=0:
        break
    cnt-=lst[i]
    ans+=1
print(ans)

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

Solution (Python):
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 sum(a) < sum(b):
    print(-1)
else:
    for i in vals[::-1]:
        if val >= num:
            break
        ans += 1
        val += i
    print(ans)

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

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

sl = 0
ans = 0
for di in d:
    if di < 0:
        sl += di
        ans += 1
    else:
        break

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

sr = 0
for di in reversed(d):
    if di >= 0 and sr +sl < 0:
        sr += di
        ans += 1

if sr + sl >= 0:
    print(ans)
else:
    print(-1)

Solution (Python):
def main2():
    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)
    else:
        S = sum(A) - sum(B)
        C = [[a, b, b, a - b] for a, b in zip(A, B)] 
        C = sorted(C, key=lambda x:x[3])

        ans = 0
        for i in C:
            a, b, c, d = i

            if d == 0:
                pass
            
            elif d > 0:
                if d <= S:
                    S -= d
                else:
                    ans += 1
            else:
                ans += 1

        print(ans)

if __name__ == "__main__":
    main2()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
cnt = 0
lack = 0

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

c = sorted(c)[::-1]

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

for i in c:
    if lack <= 0:
        break
    lack -= i
    cnt += 1

print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
cnt = 0
lack = 0

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

c = sorted(c)[::-1]
asum = sum(a)
bsum = sum(b)

if asum < bsum:
    print(-1)
    exit()
if cnt == 0:
    print(0)
    exit()

for i in c:
    lack -= i
    cnt += 1
    if lack <= 0:
        break

print(cnt)

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


ans = 0
rest = 0
ex = [] 
heapq.heapify(ex)
for i in range(N):
    if A[i] < B[i]:
        ans += 1
        rest += B[i] - A[i]
    else:
        heapq.heappush(ex, B[i] - A[i])

pre_len = len(ex)


while rest > 0:
    if len(ex) <= 0:
        print(-1)
        break
    minus_rest = heapq.heappop(ex)
    rest += minus_rest
else:
    print(ans + (pre_len - len(ex)))

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

minus_sum = 0
plus_list = []
ans = 0
for i in range(N):
    if A[i] < B[i]:
        minus_sum += (B[i] - A[i])
        ans += 1
    elif A[i] > B[i]:
        plus_list.append(A[i] - B[i])
plus_list.sort(reverse=True)

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

i = 0
while minus_sum > 0:
    minus_sum -= plus_list[i]
    ans += 1
    i += 1

print(ans)

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

surplus = []
short = 0
cnt = 0

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

surplus = sorted(surplus)
s = 0

while s < short:
    if len(surplus) == 0:
        print(-1)
        exit()

    s += surplus.pop()
    cnt += 1

print(cnt)