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 numpy as np

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

A=np.array(A)
B=np.array(B)
E=A-B
e=sum(E)

if e<0:
    print(-1)

else:

    Eminus=[]
    Ezero=[]
    Eplus=[]
    nn=0
    n0=0
    nminus=0

    for n in range(N):
        if E[n]<0:
            Eminus.append(E[n])
            nminus=nminus+1
        
        elif E[n]>0:
            Eplus.append(E[n])
            nn=nn+1
        
        else:
            Ezero.append(E[n])
            n0=n0+1
    
    Eplus.sort()
    sumup=0

    for n in range(nn):
        sumup=sumup+Eplus[n]

        if sumup>e:
            break

    if nminus==0:
        print(0)
    else:
        print(N-n0-n)

Solution (Python):
import numpy as np
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = np.array(A)
B = np.array(B)
if np.sum(A) < np.sum(B):
    print(-1) 
else:
    sub_a_b = A-B
    p_sub = sorted(list(sub_a_b[sub_a_b>0]),reverse = True)
    
    m_sub = list(sub_a_b[sub_a_b<0])
    max_counter = p_sub[0]
    m_sub_sum = abs(sum(m_sub))
    count = len(m_sub) 
    
    
    
    if list(np.where(sub_a_b<0)[0]) == [] :
        print(0)
        exit()
    else:
        count+=1

        for i in range(len(p_sub)):
            if max_counter >= m_sub_sum:
                print(count)
                exit()
            else:
                max_counter += p_sub[i+1]
                count += 1

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

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

d=[]
e=[]
dif=0
nsum=0
psum=0
cnt=0


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

  if dif<0:
    d.append(dif)
    nsum+=dif
  elif dif>0:
    e.append(dif)


e.sort()

if abs(nsum)>sum(e):
  print(-1)
else:
  i=-1
  while abs(nsum)>psum:
    cnt+=1
    psum+=e[i]
    i-=1
  print(len(d)+cnt)

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

if sum(a) < sum(b):
    res = -1
else:
    c = [a[i] - b[i] for i in range(n)]
    c1 = [i if i > 0 else 0 for i in c]
    c2 = sum([i if i < 0 else 0 for i in c])
    c3 = sum([1 if i < 0 else 0 for i in c])
    if c2 < 0:
        c4 = sorted(c1, reverse=True)
        for i in range(n):
            d = d + c4[i]
            if d + c2 > 0:
                res = c3 + i + 1
                break

print(res)

Solution (Python):
N=int(input())
A=input().split()
B=input().split()
A=[int(i) for i in A]
B=[int(i) for i in B]
sum=0
D,E=list(),list()
for i in range(N):
	sum+=(A[i]-B[i])
if sum<0:
	print(-1)
else:
	sub,p=0,0
	for i in range(N):
		n=0
		n=A[i]-B[i]
		if n<0:
			sub+=n
			p+=1
		elif n>0:
			E.append(n)
	E.sort(reverse=True)
	j=0
	while sub < 0:
		sub=sub+E[j]
		j+=1
	print(j+p)

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:
  c_p=[]
  c_m=[]
  for i in range(len(a)):
    if a[i] > b[i]:
      c_p.append(a[i]-b[i])
    elif a[i] <b[i]:
      c_m.append(a[i]-b[i])
  
  sum_c_m = sum(c_m)
  result = len(c_m)
  c_p.sort(reverse=True)
  
  index=0
  
  while sum_c_m<0:
    sum_c_m += c_p[index]
    index+=1
  result += index
  print(result)

Solution (Python):




"""








"""
def get_input():
    _ = input()
    A = [int(i) for i in input().split(" ")]
    B = [int(i) for i in input().split(" ")]

    return A, B


def solve(A, B):
    """
    >>> solve([2,3,5],[3,4,1])
    >>> solve([2,3,3],[2,2,1])
    """
    need_magic = [a_each - b_each for a_each,b_each in zip(A,B)]
    if sum(need_magic) < 0:
        return -1
    else:
        plus = [x for x in need_magic if x > 0]
        minus = [x for x in need_magic if x < 0]
        return check_needs(plus, minus)


def check_needs(plus, minus):
    target = sum(minus)
    count = 0
    for magic in sorted(plus, reverse=True):
        if target >= 0:
            break
        target += magic
        count += 1
    return count + len(minus)


if __name__ == '__main__':
    a = get_input()
    print(solve(a[0], a[1]))

Solution (Python):




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


sum = 0
minuscnt = 0
summinus = 0

for idx in range(n):
    yoyu = a[idx] - b[idx]
    c.append(yoyu)
    sum += yoyu
    if yoyu < 0:
        minuscnt += 1
        summinus += yoyu

c.sort(reverse=True)

if sum < 0:
    ans = -1
else:
    pluscnt = 0
    while summinus < 0:
        summinus += c[pluscnt]
        pluscnt += 1
    ans = minuscnt + pluscnt


print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
df = list([0 for i in range(n)])
ans = -1

if sum(a) < sum(b):
    ans = -1
else:
    for i in range(n):
        df[i] = a[i] - b[i]
    minus = list(filter(lambda x: x < 0, df))
    plus = list(filter(lambda x: x > 0, df))
    m_count = len(minus)
    m_sum = sum(minus)
    
    
    plus.sort(reverse=True)
    
    

    if m_count == 0:
        ans = 0
    else:
        for i in range(len(plus)):
            m_sum += plus[i]
            if m_sum >= 0:
                break

        if m_sum>=0:
            ans = m_count + i + 1
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:
    diff = [a-b for a,b in zip(A,B)]
    plus = []
    minus_num = 0
    lack = 0
    for d in diff:
        if d >= 0:
            plus.append(d)
        elif d < 0:
            minus_num += 1
            lack += d
    plus.sort(reverse=True)
    i = 0
    while lack < 0:
        lack += plus[i]
        i += 1
        
    print(N-len(plus)+i)

Solution (Python):
N=int(input())
X=input()
A1=[]
A1=X.split(" ") 
A=[int(i) for i in A1] 
Y=input()
B1=[]
B1=Y.split(" ") 
B=[int(i) for i in B1] 
W=[]
Z=[]
for j in range(0,N):
    w=A[j]-B[j]
    W.append(w)
for n in range(0,N):
    z=A[n]-B[n]
    Z.append(z)
Z=sorted(Z,reverse=True) 
x2=0
x=0
if sum(B)>sum(A):
    print("-1")
else:
    x=0
    x1=0
    x2=0
    x3=0
    for i in range(0,N):
        if A[i]<B[i]:
            x=x+1 
            x1=x1+W[i]


    for k in range(0,N): 
        if x1<0:
            x1=int(x1)
            p=int(Z[k])
            x1=x1+p
            x3=x3+1
        else:
            break
    x2=x3
    print(x2+x)

Solution (Python):
import numpy
import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
diff = []
for i in range(N):
    diff.append(A[i] - B[i])
neg_diff = [i for i in diff if i < 0]
pos_diff = [i for i in diff if i > 0]
pos_diff.sort(reverse=True)
if abs(sum(neg_diff)) > abs(sum(pos_diff)):
    print(-1)
    exit(0)
else:
    if sum(neg_diff) == 0:
        print(0)
        exit(0)

cum_pos_diff = numpy.cumsum(pos_diff)
insert_index = bisect.bisect_left(cum_pos_diff, abs(sum(neg_diff)))
print(len(neg_diff)+insert_index+1)

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
Asum = sum(A)
Bsum = sum(B)
yoy = []
mnu = []
test = []

if Asum < Bsum:
    flg = "NO"

else:
    flg = "YES"
    for n in range(N):
        a = A[n]
        b = B[n]
        if a >= b:
            test.append(a-b)
            yoy.append(a-b)
        else:
            test.append(a-b)
            mnu.append(a-b)
    min_num = len(mnu)
    min_sum = sum(mnu)

    yoy.sort(reverse=True)

    ynum = 0
    counter = 0
    if min_num != 0:
        for y in yoy:
            counter += 1
            ynum += y
            if ((ynum+min_sum) >= 0):
                break

if flg == "YES":
    print(min_num+counter)
else:
    print(-1)

Solution (Python):

import sys
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 D[0] >= 0:
    print(0)
    sys.exit()

sum = 0
count = 0
nsum = 0
for i in range(N):
    sum += D[i]
    if D[i] < 0:
        count += 1
        nsum += D[i]
if sum < 0:
    print(-1)
    sys.exit()

j = N - 1
while nsum < 0:
    nsum += D[j]
    count += 1
    j += -1

print(count)

Solution (Python):
import sys

def check(n, a, b) :
    p = [] 
    z = [] 
    m = [] 
    for i in range(n) :
        d = a[i] - b[i]
        if d > 0 :
            p.append(d)
        elif d == 0 :
            z.append(d)
        else :
            m.append(d)

    new_p = sorted(p, reverse=True)

    m_sum = sum(m)
    p_sum = sum(p)

    if p_sum + m_sum < 0 :
        return -1

    if len(p) + len(z) == n :
        return 0

    count = 0
    tmp = 0
    for i, ele in enumerate(new_p) :
        count = count + 1
        tmp = tmp + ele

        if tmp + m_sum >= 0 :
            return (count + len(m))


if __name__ == '__main__' :

    n = int(sys.stdin.readline())
    a = [ int(i) for i in sys.stdin.readline().split( ) ]
    b = [ int(i) for i in sys.stdin.readline().split( ) ]

    print(check(n,a,b))

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)
else:
    dif = A-B
    ans = np.sum(dif<0)
    minus = [dif[i]*-1 for i in range(N) if dif[i] < 0]
    minus_sum = np.sum(minus)
    plus = [dif[i] for i in range(N) if dif[i] > 0]
    plus_sort = np.sort(plus)[::-1]

    for i in plus_sort:
        if minus_sum == 0:
            print(ans)
            break
        minus_sum -= i
        ans += 1
        if minus_sum < 0:
            print(ans)
            break
    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)])
k = sum(x < 0 for x in c)
neg = -sum(c[:k])
pos = c[len(c) - 1]

if sum(c) < 0:
  print(-1)
elif k == 0:
  print(0)
else:
  for i in range(n):
    if pos >= neg:
      print(k + i + 1)
      break
    else: pos += c[len(c) - 2 - i]

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)


C_less=[]
C_more=[]

for i in range(N):
	if A[i]==B[i]:
		continue
	else:
		if A[i]<B[i]:
			C_less.append(abs(A[i]-B[i]))
		if A[i]>B[i]:
			C_more.append(abs(A[i]-B[i]))

C_more2=list(sorted(C_more, reverse=True))


ans=len(C_less)
s=sum(C_less)
if s==0:
	print(0)
	exit(0)

s_m=0
for i in range(len(C_more2)):
	s_m+=C_more2[i]
	
	if s_m>=s:
		d=i+1
		break
print(ans+d)

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

diff_m = 0
diff_p = []
mcount = 0

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

else:
    for i in range(N):
        difftmp = A[i] - B[i]
        
        if difftmp < 0:
            diff_m += difftmp
            mcount += 1
        
        else:
            diff_p.append(difftmp)
    

    if diff_m == 0:
        print(0)

    else:
        diff_p.sort(reverse = True)

        for j in range(0,N):
            diff_m = diff_m + diff_p[j]
 

            if diff_m >= 0:
                print(j+1 + mcount)
                break

            else:
                continue

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


def m():
    if sum(A) < sum(B):
        return -1
    f = 0
    for i in range(N):
        if B[i] > A[i]:
            f = 1
    if f == 0:
        return 0


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

    def swapsum(plus, minus):
        if abs(C[plus]) > abs(C[minus]):
            C[plus] += C[minus]
            C[minus] = 0
        else:
            C[minus] += C[plus]
            C[plus] = 0
    ans = 0
    left = 0
    right = N-1
    while C[right] < 0:
        if -C[left] == C[right]:
            swapsum(left, right)
        else:
            swapsum(left,right)
        if C[left] == 0:
            left += 1
            ans += 1
        if C[right] == 0:
            right -=1
            ans +=1
    if org[left] == C[left]:
        return ans
    else:
        return ans+1

print(m())

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

for i in range(n):
    diff = A[i] - B[i]
    if diff >= 0:
        diff_p.append(diff)
    else:
        diff_m.append(diff)

diff_p.sort(reverse=True)
diff_m.sort()

ans = 0
if sum(A) < sum(B):
    ans = -1
else:
    ans += len(diff_m)
    target = sum(diff_m)
    if len(diff_m) > 0:
        for p in diff_p:
            target += p
            ans += 1
            if target >= 0:
                break
            
print(ans)

Solution (Python):
import numpy as np
import sys

n = int(input())

_ = input().split()
a = np.array([int(k) for k in _])

_ = input().split()
b = np.array([int(k) for k in _])


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

count = 0
j = 0
pos = np.sort(diff[np.where(diff > 0)[0]])[::-1]
neg = np.sort(diff[np.where(diff < 0)[0]])

buf = pos[j]
i = 0


while i < len(neg):
    if abs(neg[i]) <= buf:
        buf += neg[i]
        i += 1
        count += 1   
    else:
        d = 0
        while abs(neg[i]) > buf:
            d += 1
            buf += pos[j + d]
        j += d
        count += d
if count > 0:
    count += 1
print(count)

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

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

if shortage < 0:
    supply = [x for x in delta if x > 0]
    supply.sort(reverse=True)
    for i in supply:
        shortage += i
        ans += 1
        if shortage >= 0: break

if shortage >= 0:
	print(ans)
else:
	print(-1)

Solution (Python):
n = int(input())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]
ABs = sorted([a - b for a, b in zip(As, Bs)])

if sum(As) < sum(Bs):
    print(-1)
    exit()
if min(ABs) > 0:
    print(-1)
    exit()
mABs = [ab for ab in ABs if ab < 0]
msum, ans = sum(mABs), len(mABs)

now = -1
while msum < 0:
    msum += ABs[now]
    ans += 1
    now -= 1
print(ans)

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

A = list(map(int,input().split()))
B = list(map(int,input().split()))
L = [A[i] - B[i] for i in range(N)]
ans = 0
if sum(A) < sum(B) :
    ans = -1
else :
    debt = []
    deposit = []
    for i in range(N) :
        if L[i] < 0 :
            debt.append(-L[i])
        elif L[i] > 0 :
            deposit.append(L[i])
        
    debt = sorted(debt)
    deposit = sorted(deposit,reverse=True)
    
    debt_duplicated = debt.copy()
    deposit_duplicated = deposit.copy()
    
    deposit_trash = []
    
    
    
    
    for i in range(len(debt)) :
        
        while True :
            if debt[i] == 0 :
                break
            else :
                if debt[i] <= deposit[0] :
                    deposit[0] -= debt[i]
                    debt[i] = 0
                else :
                    debt[i] -= deposit[0]
                    deposit[0] = 0
                    deposit_trash.append(deposit.pop(0))
        
            
        








    deposit = deposit_trash + deposit
    for i in range(len(debt)) :
        if debt[i] != debt_duplicated[i] :
            ans += 1
    for i in range(len(deposit)) :
        if deposit[i] != deposit_duplicated[i] :
            ans += 1

print(ans)

Solution (Python):
n=int(input())
Alist=[int(i) for i in input().split(" ")]
Blist=[int(i) for i in input().split(" ")]
sumA=sum(Alist)
sumB=sum(Blist)
Dlist=[]
muinus=0
muinus_num=0
count=0
if sumA<sumB:
    print(-1)
else:
    for i in range(n):
        AB=Alist[i]-Blist[i]
        Dlist.append(AB)
        if AB<0:
            muinus+=AB
            muinus_num+=1
    Dlist.sort(reverse=True)
    while muinus<0:
        muinus+=Dlist[count]
        count+=1
    print(muinus_num+count)

Solution (Python):
import numpy as np
import sys
sys.setrecursionlimit(10**8)

def nibutan(r, target): 
    l = len(r)
    if l==2:
        if r[0]<target<=r[1]:
            return 1
        elif r[0]>=target:
            return 0
        else:
            return -2
    if r[0]==target:
        return 0
    elif r[-1]==target:
        return l-1
        
    tmp = l//2
    if r[tmp]<target<=r[tmp+1]:
        return tmp+1
    elif target <= r[tmp]:
        return nibutan(r[:tmp+1], target)
    elif target > r[tmp+1]:
        return tmp+nibutan(r[tmp:], target)


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

x = a-b
negative = x[x<0]
positive = sorted(x[x>0], reverse=True)
negative_sum = -negative.sum()

if negative_sum<=0:
    print(0)
    exit()

if len(positive)==0:
    print(-1)
    exit()

sub_sum = []
tmp = 0
for i in range(len(positive)):
    tmp += positive[i]
    sub_sum.append(tmp)



if negative_sum > sub_sum[-1]:
    print(-1)
    exit()
if negative_sum <= sub_sum[0]:
    print(1+len(negative))
    exit()

print(nibutan(sub_sum, negative_sum)+len(negative)+1)

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')
    exit()
 
count = 0
sabun = [a[i]-b[i] for i, _ in enumerate(a)]
husoku = sum([x for x in sabun if x<0])
sabun.sort(reverse=True)

for i in sabun:
    if husoku >= 0:
        break
    husoku += i
    count += 1
 
print(len([x for x in sabun if x<0]) + count)

Solution (Python):
import copy
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
X=[A[n]-B[n] for n in range(N)]
if sum(X)<0:
    print(-1)
else:
    Y=copy.copy(X)
    minus_list=[y for y in Y if y<0]
    plus_list=sorted([y for y in Y if y>0])[::-1]
    count=len(minus_list)
    minus=sum(minus_list)
    plus=0
    for z in plus_list:
        if plus+minus>=0:
            break
        count+=1
        plus+=z
    print(count)

Solution (Python):
def qSort(array):
    left = []
    right = []

    if len(array) <= 1:
        return array

    ref = array[0]
    array = array[1:]
    for a in array:
        if a < ref:
            left.append(a)
        else:
            right.append(a)

    return qSort(left) + [ref] + qSort(right)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C_minus = []
C_plus = []
cnt = 0
if sum(A) < sum(B):
    print("-1")

else:
    for i, a in enumerate(A):
        tmp = A[i] - B[i]
        if tmp > 0:
            C_plus.append(tmp)
        elif tmp < 0:
            C_minus.append(tmp)

    C_plus = list(reversed(qSort(C_plus)))
    C_minus = qSort(C_minus)

    if len(C_minus) == 0:
        print(len(C_minus))
    else:
        start = 0
        judge = 0
        for c_m in C_minus:
            judge = c_m
            for i in range(start, len(C_plus)):
                judge += C_plus[i]
                if judge > 0:
                    start = i
                    C_plus[i] = judge
                    break

        print(len(C_minus)+start+1)

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

plus_list = []
ans = plus = minus = 0
for a, b in zip(A, B):
    if a - b >= 0:
        plus += (a - b)
        plus_list.append(a - b)
    else:
        ans += 1
        minus += (b - a)

if len(plus_list) == N:
    print(0)
elif minus > plus:
    print(-1)
elif minus == plus:
    print(N)
else:
    plus_list.sort(reverse=True)
    for plus_num in plus_list:
        minus -= plus_num
        ans += 1
        if minus <= 0:
            break
    print(ans)

Solution (Python):
n = int(input())

a = list(map(int, input().split()))
b = list(map(int, input().split()))
plus = []
minus = []
for ai, bi in zip(a,b):
    point = ai-bi
    if point > 0:
        plus.append(point)
    elif point < 0:
        minus.append(point)

total_minus = abs(sum(minus))



plus.sort(reverse=True)


ans = 0
if sum(plus) + sum(minus) < 0:
    print(-1)
else:
    sum = 0
    for i in range(len(plus)):
        if sum >= total_minus:
            break
        sum+=plus[i]
        ans+=1

    ans+=len(minus)
    print(ans)

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

needs = []
need = 0
ans = 0
if sum(A) < sum(B):
    print(-1)
else:
    for i in range(N):
        diff = A[i] - B[i]
        if diff < 0:
            ans += 1
            need += (-diff)
        elif diff > 0:
            needs.append(diff)
    if need != 0:
        needs.sort(reverse=True)
        for item in needs:
            need -= item
            ans += 1
            if need <= 0:
                break
    print(ans)

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

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

if amin == []:
    print(0)
    exit()

bmin.sort()



if sum(amin) > sum(bmin):
    print(-1)
    exit()

num = sum(amin)
for i in range(len(bmin)):
    
    num -= bmin[len(bmin)-i-1]
    if num < 0:
        print(len(amin)+i+1)
        break
else:
    print(N)

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

diff = (list)(map(lambda a, b: a - b, A, B))
sum_diff = sum(diff)


def check(a):
    for i in range(n):
        if a[i] < 0:
            return False
    return True


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

elif check(diff):
    print(0)
else:
    ans = 0
    pos_diff = (list)(filter(lambda x: x > 0, diff))
    pos_diff = sorted(pos_diff)
    for i in range(len(pos_diff)):
        rm = pos_diff[i]
        if sum_diff-rm > 0:
            ans += 1
            sum_diff -= rm

        else:
            print(n-ans)
            exit()

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

tmp=0
if sum(A)>=sum(B):
    D=[A[i]-B[i] for i in range(N)]
    D.sort(reverse=True)
    t_ind = [i for i, d in enumerate(D) if d<0]
    if len(t_ind)!=0:
        t_sum=sum([D[t] for t in t_ind])
        for i in range(len(D)-len(t_ind)):
            tmp+=D[i]
            if tmp>=-t_sum:
                print((i+1)+len(t_ind))
                break
    else:
        print(0)
else:
    print(-1)

Solution (Python):
n=int(input())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
h=0
c=0
minl=[]
for i in range(n):
  a=al[i]
  b=bl[i]
  if a<b:
    h+=b-a
    c+=1
  else:
    minl.append(a-b)
minl.sort()
minl.reverse()
if c==0:
  print(c)
  exit()
if len(minl)==0:
  print(-1)
  exit()
for i in minl:
  h-=i
  c+=1
  if h<=0:
    print(c)
    exit()
print(-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())))
l = a-b
l_fu = np.where(l<0,l,0).sum() * (-1)
fu = np.sum(l<0)
z = np.sum(l==0)
m = np.sort(l)[::-1]
o = x = 0

if sum(l) < 0:
  o = -1
else:
  if fu == 0:
    o = 0
  else:
    for i in range(n):
      if m[i] > 0:
        x += m[i]
        if x >= l_fu:
          o = i+1+fu
          break
      else:
        break

print(o)

Solution (Python):

import sys

N = int(input())
A = [int(s) for s in input().split(' ')]
B = [int(s) for s in input().split(' ')]
D = []
cnt = 0
t_diff = 0

if sum(A) < sum(B):
    print(-1)
    sys.exit()
    
for i in range(len(A)):
    if A[i] < B[i]:
        cnt = cnt + 1
        t_diff = t_diff + (B[i] - A[i])
    if A[i] > B[i]:
        D.append(A[i] - B[i])

D.sort(reverse=True)

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

Solution (Python):

"""
Created on Sat Jan 12 18:50:46 2019
KeyenceC
@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = [0]*n
ok = False

if sum(a) >= sum(b):
    ok = True
    sum_fusoku = 0
    cnt_fusoku = 0
    larger = []
    for i in range(n):
        ab = a[i] - b[i]
        if ab < 0:
            sum_fusoku += -ab
            cnt_fusoku += 1
        else:
            larger.append(ab)
                
    larger.sort(reverse=True)
    cnt_large = 0
    for i in larger:
        if sum_fusoku <= 0:
            break
        sum_fusoku -= i
        cnt_large += 1

if ok == False:
    print(-1)
else:
    ans = cnt_fusoku + cnt_large
    print(ans)

Solution (Python):
N = int(input())
A = []
for i in input().split():
	A.append(int(i))
B = []
diff = []
Ind = 0
zero_or_positive_diff = []
negative_diff = []
for i in input().split():
	b = int(i)
	B.append(b)
	d = A[Ind] - b
	diff.append(d)
	if d >= 0:
		zero_or_positive_diff.append(d)
	else:
		negative_diff.append(d)
	Ind += 1

zero_or_positive_diff.sort()
zero_or_positive_diff.reverse()
sum_negative_diff = sum(negative_diff)

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

Ind = 0
part_sum_zero_or_positive_diff = 0
while part_sum_zero_or_positive_diff + sum_negative_diff < 0:
	sum_negative_diff += zero_or_positive_diff[Ind]
	Ind += 1

print(Ind+len(negative_diff))

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total_A = sum(A)
total_B = sum(B)
if total_A < total_B:
    print(-1)
d = [a_i - b_i for a_i, b_i in zip(A, B)]
d.sort(reverse=True)
minus = [i for i in d if i < 0]
len_d = len(minus)
total_d = sum(minus)
n = 0
for i in d:
    if total_d >= 0:
        print(n + len_d)
        break
    total_d += i
    n += 1

Solution (Python):
import bisect


def read():
    N = int(input().strip())
    A_iter = map(int, input().strip().split())
    B_iter = map(int, input().strip().split())
    return N, A_iter, B_iter


def index(a, x):
    i = bisect.bisect_left(a, x)
    return i


def solve(N, A_iter, B_iter):
    C = []
    for a, b in zip(A_iter, B_iter):
        C.append(a - b)
    C = sorted(C)
    zero_index = index(C, 0)
    few = C[:zero_index]
    many = C[zero_index:]

    few_size = len(few)
    few_sum = -sum(few)

    if few_sum == 0:
        return few_size
    for i, m in enumerate(reversed(many)):
        few_sum -= m
        if few_sum <= 0:
            return few_size + i + 1
    return -1


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

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:
    num, fix = 0, 0
    for i in range(N):
        if A[i] - B[i] <  0:
            num += 1
            fix += B[i] - A[i]
    if num != 0:
        ret = []
        for i in range(N):
            if A[i] - B[i] > 0:
                ret.append(A[i] - B[i])
        ret.sort(reverse=True)
        su = 0
        for i in range(len(ret)):
            su += ret[i]
            num += 1
            if su >= fix:
                break
    print(num)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Diff = [None]*N

for i in range(N):
  Diff[i] = A[i] - B[i]
  
sort_Diff= sorted(Diff,reverse=True)

flag=0
plus_sum=0
ans=0
minus=N+1

for i in range(N):
  if sort_Diff[i]<0:
    minus = i
    break
  plus_sum += sort_Diff[i]

if sum(sort_Diff)<0:
  print(-1)
else:
  ans = N - minus
  minus_sum = sum(sort_Diff)-plus_sum


    
  for i in range(N):
    if minus_sum + abs(sort_Diff[i]) < 0:
      minus_sum = minus_sum + abs(sort_Diff[i]) 
      if minus_sum ==0:
        break
      ans += 1
    else:
      ans = ans+1
      break

  print(ans)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    if sum(A) < sum(B):
        return -1

    d = [0]*N
    ans = 0
    fusoku = 0
    for i in range(N):
        d[i] = A[i] - B[i]
        if d[i] < 0:
            ans += 1
            fusoku -= d[i]
    d.sort()
    for dd in d[::-1]:
        if fusoku <= 0:
            break
        fusoku -= dd
        ans += 1
    return ans
        

if __name__ == "__main__":
    print(main())

Solution (Python):
def magic(n, a, b):
    if sum(a) < sum(b):
        return(-1)

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

    em = 0
    cm = 0
    for i in range(n):
        if d[i] < 0:
            em += d[i]
            cm += 1
    d.sort(reverse=True)
    ep = 0
    cp = 0
    for i in range(n):
        ep += d[i]
        cp += 1
        if ep >= abs(em):
            break
    return(cm + cp)

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

print(magic(n, a, b))