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 sol(inp=input):
    n=int(inp())
    curs=list(map(int,inp().split()))
    tgts=list(map(int,inp().split()))
    if sum(curs)<sum(tgts):
        return -1
    diffs=[c-t for c,t in zip(curs,tgts)]
    shorts=[-d for d in diffs if d<0]
    if len(shorts)==0:
        return 0
    sum_shorts=sum(shorts)
    overs_decending=sorted(d for d in diffs if d>0)[::-1]
    tmp_sum_overs=0
    for i,over in enumerate(overs_decending):
        tmp_sum_overs+=over
        if tmp_sum_overs>=sum_shorts:
            return i+len(shorts)+1
print(sol())

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

plus = []
minus = []

for i in range(n):
    if C[i]>0:
        plus.append(C[i])
    elif C[i]<0:
        minus.append(C[i])

plus.sort(reverse=True)
f = sum(minus)
if f == 0:
    print(0)
    exit()

for i in range(len(plus)):
    f += plus[i]
    if f>=0:
        print(i+1+len(minus))
        break
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()

isgoukaku = True
fusokus = []
yobun = []

for a, b in zip(A, B):
    if a < b:
        
        fusokus.append(b - a)
        isgoukaku = False
    else:
        
        yobun.append(a - b)



if isgoukaku:
    print(0)
    exit()

fusoku = sum(fusokus)
yobun.sort(reverse=True)

henkou = len(fusokus)

tmp = 0
for y in yobun:
    
    if tmp >= fusoku:
        break
    tmp += y
    henkou += 1
    

print(henkou)

Solution (Python):
import numpy as np
N=int(input())
A_list = np.array(list(map(int, input().split()))) 
B_list = np.array(list(map(int, input().split())))

D_list= A_list-B_list

need_study=0
do_study=0
note_num=[]
answer=0
for num in D_list:
    if num<0:
        answer+=1
        need_study+=num
    else:
        note_num.append(num)
        do_study += num
if (need_study)*(-1)>do_study:
    print(-1)
else:
    note_num.sort()
    note_num.reverse()
    change_score=0
    need_socre=(need_study)*(-1)
    while change_score<need_socre:
        change_score += note_num[0]
        answer += 1
        note_num.pop(0)
    print(answer)

Solution (Python):
import numpy as np
N = int(raw_input())
A = np.array(map(long, raw_input().split()))
B = np.array(map(long, raw_input().split()))

ans = 0
if A.sum() < B.sum():
  ans = -1
else:
  D = A-B
  E = D[D<0]
  e = sum(E)
  F = np.sort(D[D>0])
  ans = len(E)
  for f in reversed(F):
    if e >= 0:
      break
    e += f
    ans += 1
print ans

Solution (Python):
import bisect






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

sa = [A[i] - B[i] for i in range(len(A))]
sa.sort()

insert_index = bisect.bisect_left(sa,0)

Sum = sum(sa[0:insert_index])
Sumlist = [0 for i in range(N)]
isum = 0

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

for i in range(len(sa),insert_index-1,-1):
    isum = isum + sa[i-1]
    if abs(Sum) <= isum:
        print(insert_index + N - i + 1)
        exit()
    elif sa[i-2] <= 0:
        print(-1)
        exit()

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


def checkC(N,A,B):
    ans = -1
    shortage = np.array([a-b if a<b else 0 for a,b in zip(A,B)])
    exceed = np.array([a-b if a>b else 0 for a,b in zip(A,B)])
    ss = shortage[shortage!=0]
    ee = exceed[exceed!=0]
    
    ssum = -ss.sum()
    ans = ss.shape[0]
    eesort = np.sort(exceed)[::-1]
    
    k = 0
    i = 0
    while k < ssum:
        k +=  eesort[i]
        i += 1
        ans += 1
    

    return ans


if sum(A) < sum(B):
    print(-1)
else:
    print(checkC(N,A,B))

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

count = 0
minus = 0
for i in range(n):
    if delta[i] < 0 :
        count += 1
        minus += delta[i]


if sum(a) < sum(b):
    print(-1)
    
else:
    for i in range(n):
        if minus >= 0:
            break
            
        count += 1
        minus += delta[n-i-1]

    
    print(count)

Solution (Python):
from itertools import accumulate
import numpy as np

def parse(f):
    N = int(next(f))
    A = np.array(list(map(int, next(f).split())))
    B = np.array(list(map(int, next(f).split())))
    return A, B

def test(A, B):
    d = (A - B).astype(np.int64)
    shorts = d[d < 0]
    short = -np.sum(shorts)
    n = len(shorts)
    suffs = np.array(list(accumulate(sorted(d[d > 0], reverse=True))))
    if n == 0:
        print(0)
        return
    for i, b in enumerate(suffs > short):
        if b:
            print(i + n + 1)
            break
    else:
        print(-1)

import sys
test(*parse(sys.stdin))

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

M = []
P = []

for i in range(N):
    tmp = A[i] - B[i]
    if tmp < 0:
        M.append(tmp)
    else:
        P.append(tmp)

ms = sum(M)
if ms == 0:
    print(0)
    exit(0)

P.sort(reverse=True)

count = 0
for pi in P:
    count += 1
    ms += pi
    if ms >= 0:
        break
else:
    print(-1)
    exit(0)

print(count + len(M))

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


a_sum=sum(a)
b_sum=sum(b)

c = a[:]

if a_sum< b_sum:
    print(-1)

    
else:
    sub_ab=[0 for i in range(n)]
    for i in range(n):
        sub_ab[i]=c[i]-b[i]

    if sum(1 for i in range(n) if sub_ab[i] > 0) == n:
        print(0)
              
    else:
        minus_value=sum(sub_ab[i] for i in range(n) if sub_ab[i] < 0)
        minus_number=sum(1 for i in range(n) if sub_ab[i] < 0)
        surplas=0
        m=0
        sub_ab_sorted=sorted(sub_ab, reverse=True)

        while surplas<abs(minus_value):
            surplas=sum(sub_ab_sorted[0:m+1])
            m+=1
        
        print(m+minus_number)

Solution (Python):
import numpy as np

N = int(input())
A_array = np.asarray(list(map(int, input().split(" "))))
B_array = np.asarray(list(map(int, input().split(" "))))

sum_A = A_array.sum()
sum_B = B_array.sum()

yobun = sum_A - sum_B

if sum_A < sum_B:
    print(-1)
else:
    A_B_array = A_array - B_array

    
    A_B_minus = A_B_array[A_B_array < 0]
    num_minus = A_B_minus.shape[0]
    if num_minus == 0:
        print(0)
    else:
        minus_sum = - sum(A_B_minus)

        A_B_plus = np.sort(A_B_array[A_B_array >= 0])[::-1]
        A_B_plus_ruiseki = np.cumsum(A_B_plus)
        

        num_plus = 1 + np.where( A_B_plus_ruiseki >= minus_sum )[0][0]
        print(num_minus + num_plus)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = []
minus = 0
minuskamoku = 0
for i in range(N):
    C.append(A[i]-B[i])
D = sorted(C)[::-1]
for i in C:
    if i < 0:
        minus += i
        minuskamoku += 1
count = 0
for i in D:
    if minus >= 0:
        break
    else:
        minus += i
        count += 1
if minus >= 0:
    print(count + minuskamoku)
else:
    print(-1)

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

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

    A_B.sort()
    tmp = 0
    cnt = 0
    for i in range(N):
        if A_B[i] >= 0:
            break
        else:
            tmp += abs(A_B[i])
            cnt += 1

    A_B = A_B[::-1]

    for i in range(N):
        if tmp <= 0:
            break
        tmp -= A_B[i]
        cnt += 1

    if tmp > 0:
        cnt = -1
    print(cnt)
C()

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())))
diff = A-B
lack = diff[diff<0].sum()
lack_number = diff[diff<0].shape[0]
rich = np.sort(diff[A>B])[::-1]
count = 0
if A.sum() < B.sum():
    count = -1
else:
    if lack_number == 0:
        count = 0
    else:
        count = lack_number
        for i in range(rich.shape[0]):
            lack += rich[i]
            count += 1
            if lack >= 0:
                break
print(count)

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


delta = sorted([a - b for a, b in zip(A, B)], reverse=True)
if delta[-1] >= 0:
    print(0)
elif sum(delta) < 0:
    print(-1)
else:
    minus_sum = 0
    minus_cnt = 0
    for i in delta:
        if i < 0:
            minus_sum += i
            minus_cnt += 1

    for i, plus in enumerate(delta):
        if plus > 0:
            minus_sum += plus
        if minus_sum >=0:
            break
    print(minus_cnt + i + 1)

Solution (Python):


import numpy
import scipy

from collections import defaultdict
from pprint import pprint

N = int(input())
A = [int(x) for x in input().split(" ")]
B = [int(x) for x in input().split(" ")]

diff = [0 for x in range(N)]

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

if (sum(diff) < 0):
    print (-1)
else:
    point = 0
    ans = 0
    for d in diff:
        if d < 0:
            point += d
            ans += 1
    for d in sorted(diff, reverse=True):
        if point >= 0:
            break
        ans += 1
        point += d
    print(ans)

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

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

c = 0
cp = []
cm = []
for a, b in zip(A, B):
    if a - b >= 0:
        cp.append(a - b)
    elif a - b < 0:
        cm.append(a - b)

ans = len(cm)
cp.sort(reverse=True)
pi = 0
pnum = cp[0]
for mnum in cm:
    while abs(mnum) >= pnum:
        pi += 1
        if pi >= len(cp):
            print(-1)
            exit()
        pnum += cp[pi]
    pnum += mnum

if cm:
    print(ans + pi + 1)
else:
    print(ans)

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)
d = a - b

d.sort()
m = 0

for i in range(n):
    if d[i] >= 0:
        k = i
        break
    m += d[i]



if sum(d) < 0:
    print(-1)
else:
    j = 0
    i = n - 1
    tt = 0
    while m < 0:
        j += 1
        m += d[i]
        tt += d[i]
        i -= 1

    print(j + k)

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]

diff_up_0 = []
diff_equal_0 = [] 
diff_down_0 = []

for d in diff:
    if d > 0:
        diff_up_0.append(d)
    elif d == 0:
        diff_equal_0.append(d) 
    else:
        diff_down_0 .append(d)

diff_up_0.sort()
diff_up_0 = diff_up_0[::-1]
d_sum = -sum(diff_down_0)
u_sum = 0

cnt = 0
len_u = len(diff_up_0)
while cnt < len_u:
    if u_sum >= d_sum:
        break
    else:
        u_sum += diff_up_0[cnt]
        cnt += 1
if u_sum < d_sum:
    print(-1)
else:
    print(cnt + len(diff_down_0))

Solution (Python):
import numpy as np
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
L = [i - j for (i,j) in zip(a,b)]
check = np.sum(x < 0 for x in L)
if check == 0:
  print(0)
else:
  neg = np.sum(x for x in L if x < 0)
  pos = sorted([x for x in L if x > 0], reverse=True)
  sum = 0
  finish = 0
  for i in range(len(pos)):
    sum += pos[i]
    if neg + sum >= 0:
      print(i + 1 + check)
      finish = 1
      break
  if finish == 0:
    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()
diffl = [0]*len(a)
for i in range(len(a)):
    diffl[i] = a[i] - b[i]
diffl.sort()
if diffl[0] >= 0:
    print(0)
    exit()
ret = 0
msum = 0
for num in diffl:
    if num < 0:
        msum -= num
        ret += 1
for num in diffl[::-1]:
    if num >= msum:
        ret += 1
        break
    else:
        ret += 1
        msum -= num
print(ret)

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

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

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

for i in sorted(a_minus_b, reverse=True):
    sum_minus += i
    ans += 1
    if sum_minus >= 0:
        print(ans)
        exit()

Solution (Python):
import numpy as np
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:
    a_n = np.array(a)
    b_n = np.array(b)

    d = a_n - b_n
    d_min = d[d < 0]
    d_max = np.sort(d[d > 0])[::-1]
    num = len(d_min)
    if num == 0:
        print(0)
        exit()
    for i in range(len(d_max)+1):

        if np.sum(d_min) + np.sum(d_max[:i]) > 0:
            print(num + i)
            exit()

Solution (Python):
from itertools import accumulate


def main():
  n = int(input())
  As = map(int, input().split())
  Bs = map(int, input().split())

  diffs = [a - b for a, b in zip(As, Bs)]
  pos_acc = list(accumulate(sorted(filter(lambda x: x>0, diffs), reverse=True)))
  negatives = list(filter(lambda x: x<0, diffs))
  neg_sum = abs(sum(negatives))
  num_negs = len(negatives)
  if num_negs == 0:
    print(0)
    return
  for i, pa in enumerate(pos_acc):
    if neg_sum <= pa:
      print(num_negs + i + 1)
      return
  print(-1)


if __name__ == '__main__':
  main()

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

lack = 0
count = 0
C = [0] * N
for i in range(N):
    if A[i] < B[i]:
        count = count + 1
        lack = lack + B[i] - A[i]
    else:
        C[i] = A[i] - B[i]

if lack == 0:
    print("0")
elif count == N:
    print("-1")
else:
    C = sorted(C, reverse = True)
    for i in range(N):
        if C[i] == 0:
            break
        if lack <= C[i]:
            count = count + 1
            lack = 0
            break
        else:
            lack = lack - C[i]
            count = count + 1
    if lack == 0:
        print(count)
    else:
        print("-1")

Solution (Python):
from sys import stdin

def main():
    N = int(input())
    A = map(int, input().split())
    B = map(int, input().split())

    plus, minus = [], []
    for a, b in zip(A, B):
        n = a - b
        if n < 0:
            minus.append(n)
        else:
            plus.append(n)

    x = sum(minus)
    if abs(x) > sum(plus):
        print(-1)
        return
    elif x == 0:
        print(0)
        return
    else:
        ans = len(minus)

    plus.sort(reverse=1)
    for i in plus:
        x += i
        ans += 1
        if x >= 0:
            break
    print(ans)

input = lambda: stdin.readline()
main()

Solution (Python):

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

diff = [i - j for i, j in zip(a, b)]

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

short = [i for i in diff if i < 0]
change = len(short)
shortage = sum(short)

over = [i for i in diff if i > 0]
for i in sorted(over, reverse=True):
    if shortage < 0:
        change += 1
        shortage += i
    else:
        break
print(change)

Solution (Python):
N = int(input())
a =list(map(int,input().split()))
b =list(map(int,input().split()))
dif = []
for i in range(N):
	dif.append(a[i]-b[i])
dif.sort()
if sum(dif) < 0:
	print(-1)
else:
	Count1 = 0
	Sum1 = 0
	for i in range(N):
		if dif[i] < 0:
			Count1 += 1
			Sum1 += dif[i]
		else:
			c = Count1
			break
	Sum2 = 0
	for i in range(N-c):
		if  Sum2 < (-1)*Sum1:
			Sum2 += dif[N-i-1]
			Count1 += 1
		else:
			break
	print(Count1)

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

ans = -1

not_enough = []
enough = []

for i in range(N):
    if A[i] - B[i] < 0:
        not_enough.append(abs(A[i] - B[i]))
    else:
        enough.append(A[i] - B[i])

enough.sort()
enough.reverse()




count = 0
whre = 0
res_eno = 0
res_not_eno = 0

if len(not_enough) == 0:
    print('0')
elif len(enough) == 0:
    print('-1')
else:
    for i in enough:

        res_eno = i
        count += 1

        while res_eno > 0:
            
            
            
            

            if res_not_eno == 0:
                if res_eno >= not_enough[whre]:
                    
                    res_eno -= not_enough[whre]
                    count += 1
                    whre += 1
                    if whre == len(not_enough):
                        break
                else:
                    
                    res_not_eno = not_enough[whre] - res_eno
                    res_eno = 0
            else:
                if res_eno >= res_not_eno:
                    

                    res_eno -= res_not_eno
                    res_not_eno = 0
                    count += 1
                    whre += 1
                    if whre == len(not_enough):
                        break
                else:
                    

                    res_not_eno = res_not_eno - res_eno
                    res_eno = 0
        if whre == len(not_enough):
            break

    if res_not_eno == 0 and whre == len(not_enough):
        print(count)
    else:
        print('-1')

Solution (Python):
def solve(n, a, b):
    negatives = [b[i] - a[i] for i in range(n) if a[i] < b[i]]
    positives = [a[i] - b[i] for i in range(n) if a[i] > b[i]]
    s = sum(negatives)
    positives.sort(reverse=True)
    
    if s == 0:
        return 0
    
    accum = 0
    for i, a in enumerate(positives):
        accum += a
        if accum >= s:
            return len(negatives) + i + 1

    return -1

def main():
    n = int(input())
    a = [int(s) for s in input().split()]
    b = [int(s) for s in input().split()]

    print(solve(n, a, b))


main()

Solution (Python):
import sys

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    AB = list(zip(A, B))
    bigger_diff = list(map(lambda ab: max(ab[0] - ab[1], 0), AB))
    lesser_diff = list(map(lambda ab: min(ab[0] - ab[1], 0), AB))
    exceeded = sum(bigger_diff)
    shortage = sum(lesser_diff)

    if exceeded < abs(shortage):
        print(-1)
        sys.exit()

    if all(map(lambda ab: ab[0] >= ab[1], AB)):
        print(0)
        sys.exit()



    ans = len(list(filter(lambda d: d<0, lesser_diff)))

    bigger_diff = sorted(bigger_diff, reverse=True)

    ss = 0
    for i in range(len(bigger_diff)):
        ss += bigger_diff[i]
        if ss >= abs(shortage):
            ans += i + 1
            break
    print(ans)



if __name__=='__main__':
    main()

Solution (Python):
n = int(input())
ary = list(map(int, input().split()))
bry = list(map(int, input().split()))

n
ary
bry

if sum(ary) < sum(bry):
    print(-1)

else:
    diff = sorted([ary[i] - bry[i] for i in range(n)])

    ns = [d for d in diff if d < 0]
    minus = abs(sum(ns))
    cnt = len(ns)
    for p in diff[::-1]:
        if minus <= 0:
            break
        else:
            minus -= p
            cnt += 1

    print(cnt)

Solution (Python):
import numpy as np

def solve(A, B):
  diff = np.array(A) - np.array(B)
  shortage = list(filter(lambda d: d < 0, diff))
  remainder = sorted(list(filter(lambda d: d > 0, diff)), reverse=True)
  short_sum = sum(shortage)
  if short_sum == 0: return 0
  counter = 0
  for r in remainder:
    short_sum += r
    counter += 1
    if short_sum >= 0: return len(shortage) + counter
  return -1

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

print(solve(A, B))

Solution (Python):


import numpy as np

def solve():
    N = int(input())
    A = np.array(list(map(int,input().split())))
    B = np.array(list(map(int,input().split())))
    if A.sum() < B.sum():
        res = -1
    else:
        C = B-A
        d = np.maximum(C,0).sum()
        M = np.minimum(np.maximum(C,0),1).sum()
        C.sort()
        for c in C:
            if d <= 0:
                break
            d += c
            M += 1
        res = M
    return str(res)

if __name__ == '__main__':
    print(solve())

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


point = 0
upper = []
count = 0

for a, b in zip(A, B):
    if a < b:
        
        point += b - a
        count += 1
        
    else:
        
        upper.append(a - b)
        
upper.sort(reverse = True)

index = 0
while point > 0:
    point -= upper[index]
    index += 1
    
print(count + index)

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

if sum(A) < sum(B) : 
    print(-1)
else :
    diff=[A[i] - B[i] for i in range(len(A))]
    pos=sorted([x for x in diff if x > 0])
    neg=[x for x in diff if x < 0]
    nes=sum(neg)
    if nes >= 0 :
        print(0)
    else:
        items = 0
        total_pos = 0
        for p in reversed(pos):
            items += 1
            total_pos += p
            if(total_pos + nes >= 0) :
                print(items + len(neg))
                break

Solution (Python):
import numpy as np

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

c = np.sort(a - b).tolist()
c_minus = [x for x in c if x < 0]
c_plus = sorted([x for x in c if x > 0], reverse=True)

cnt = len(c_minus)
cnt_sum = -sum(c_minus)

while cnt_sum > 0:
    if len(c_plus) <= 0:
        cnt = -1
        break
    cnt_sum -= c_plus.pop(0)
    cnt += 1
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:
    ans=0
    sco=0
    C=[]
    for i in range(N):
        if A[i]<B[i]:
            ans+=1
            sco+=B[i]-A[i]
        elif A[i]>B[i]:
            C.append(A[i]-B[i])
    if ans==0:
        print(0)
        exit()
    C.sort(reverse=True)
    
    for i in range(len(C)):
        ans+=1
        sco-=C[i]
        if sco<=0:
            break
    print(ans)

Solution (Python):
N = int(input())
As = list(map(int,input().strip().split()))
Bs = list(map(int,input().strip().split()))

diff = []
for i in range(0,N):
    diff.append(As[i] - Bs[i])

minuss = list(filter(lambda x: x < 0, diff))
sorted_diff = sorted(diff,reverse=True)
pla = list(filter(lambda x: x >= 0, sorted_diff))

minus_num = len(minuss)
minus = sum(minuss)
i = 0
while i < len(pla):
    if minus >= 0:
        print(i + minus_num)
        exit()
    minus += pla[i]
    i += 1

if minus >= 0:
    print(i + minus_num)
else:
    print("-1")

Solution (Python):


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


need_prepare = 0
need_change = 0
prepare_margin = 0
for a, b in zip(A, B):
    if a < b:
        need_prepare += b - a
        need_change += 1
    else:
        prepare_margin += a - b

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


if prepare_margin < need_prepare:
    print(-1)
    exit(0)


margins = []
for i in range(len(A)):
    margins.append((A[i] - B[i], i))


margins = sorted(margins, reverse=True)



for i in range(len(margins)):
    need_prepare -= margins[i][0]
    need_change += 1
    if need_prepare <= 0:
        break

print(need_change)

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

loss = 0
count = 0
c = n*[0]
for i in range(n):
    if(a[i] < b[i]):
        loss += b[i] - a[i]
        count += 1
    else: c[i] = a[i] - b[i]


c.sort(reverse=True)
for i in range(n):
    if(loss <= 0): break
    count += 1
    loss -= c[i]

print(count)

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

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

diffs = sorted([(ai, bi, ai - bi) for ai, bi in zip(a, b)], key = lambda x: x[2])

if diffs[0][2] >= 0:
    print(0)
    exit()

remains = [d for _, _, d in diffs]
idx = 1
remains_changed = deepcopy(remains)
for i, d in enumerate(remains_changed):
    if d >= 0:
        break
    else:
        while d < 0:
            if remains_changed[-idx] >= -d:
                remains_changed[-idx] += d
                d = 0
                remains_changed[i] = 0
            else:
                d += remains_changed[-idx]
                remains_changed[-idx] = 0
                idx += 1


ans = 0
for r, rc in zip(remains, remains_changed):
    if r != rc:
        ans += 1
print(ans)

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

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

neg = [v for v in c if v < 0]
total = sum(neg)
num = len(neg)

if sum(c) < 0:
    print(-1)
elif num == 0:
    print(0)
else:
    for v in reversed(c[num:]):
        total += v
        num += 1
        if total >= 0:
            break
    print(num)

Solution (Python):
import copy
N=int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
plus=[]
minus=[]
for a,b in zip(A,B):
	if a < b:
		minus.append(b - a)
	elif b < a:
		plus.append(a - b)
plus.sort(reverse=True)
p = 0
m = 0
befor = copy.deepcopy(plus)
while m != len(minus):
	if p == len(plus):
		print(-1)
		exit()
	if plus[p] > minus[m]:
		plus[p] -= minus[m]
		m += 1
	elif plus[p] == minus[m]:
		p += 1
		m += 1
	else: 
		minus[m] -= plus[p]
		p += 1
ans = len(minus)
if plus[p] != befor[p]:
	ans += p + 1
else:
	ans += p
print(ans)

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)]
diff.sort()

suma=[0 for i in range(n)]
sumb=[0 for i in range(n)]
suma[0]=a[0]
sumb[0]=b[0]
for i in range(n-1):
    suma[i+1]=suma[i]+a[i+1]
    sumb[i+1]=sumb[i]+b[i+1]
if suma[n-1]>sumb[n-1]:
    point=0
    cnt=0
    for i in range(n):
        if diff[i]>=0:
            break
        point+=diff[i]
        cnt+=1
    
    i=-1
    if point<0:
        for i in range(n):
            point+=diff[n-1-i]
            if point>=0:
                break
    print(cnt+i+1)
else:
    print(-1)

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

D = []

miCount = 0
miSum = 0

P = []

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

for i in range(N):
    d = A[i] - B[i]
    D.append(d)
    if d < 0:
        miSum += d
        miCount += 1
    elif d > 0:
        P.append(d)


P.sort(reverse=True)
pCount = 0
for p in P:
    if miSum >= 0:
        break
    pCount += 1
    miSum += p

print(pCount + miCount)

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

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

diff = []

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

diff.sort()
ans = 0

if diff[0] >= 0:
    print(0)
    exit()

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

i = 0
j = n-1
memo = [0] * n
while True:
    if i >= j:
        print(sum(memo))
        exit()
    if diff[i] >= 0:
        print(sum(memo))
        exit()

    if diff[j] > -diff[i]:
        diff[j] += diff[i]
        memo[j] = 1
        memo[i] = 1
        i += 1

    elif diff[j] < -diff[i]:
        diff[i] += diff[j]
        memo[i] = 1
        memo[j] = 1
        j -= 1

    else:
        memo[i] = 1
        memo[j] = 1
        i += 1
        j -= 1

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
G1=[]
G2=[]
for i in range(N):
  t=A[i]-B[i]
  if t>=0:
    G1.append(t)
  else:
    G2.append(t)
G1.sort(reverse=True)    
k=len(G2)

if k==0:
  print(0)
else:
  s=sum(G2)
  for i in range(len(G1)):
    s+=G1[i]
    if s>=0:
      print(k+i+1)
      break
  else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
d = [a[i]-b[i] for i in range(n)]
e = [i for i in d if i > 0]
f = [i for i in d if i < 0]
e.sort(reverse = True)
g = abs(sum(f))
if sum(d) < 0:
    print(-1)
else:
    if f == []:
        print(0)
    else:
        sum = 0
        for i in range(len(e)):
            sum += e[i]
            if sum >=  g:
                print(i+len(f)+1)
                break

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

l=sorted([j-i for i,j in zip(a,b)])[::-1]
t=0
ans=0

if sum(l)>0:
  print(-1)
else:
  for i in l:
    if i>0:
      t+=i
      ans+=1
    else:break

  for i in l[::-1]:
    if t+i>=0 and i<0:
      t=t+i
      ans+=1
    elif t:
      t=0
      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)
    exit()

d = [0] * n

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

if sorted(d)[0] >= 0:
    print(0)
    exit()

p.sort(reverse=True)
m.sort()

for i in range(1, len(p)):
    p[i] += p[i - 1]

sum_m = sum(m)

for i in range(len(p)):
    if p[i] >= abs(sum_m):
        print(len(m) + i + 1)
        exit()