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):
IMPOSSIBLE = -1


def solve(xs, ys):
    if sum(xs) < sum(ys):
        return IMPOSSIBLE
    n = len(xs)
    ts = [x - y for x, y in zip(xs, ys)]
    da_idxs = sorted(range(n), key=lambda i: ts[i])
    us = ts[:]
    pos_donor = n - 1
    pos_acceptor = 0
    while pos_acceptor < pos_donor:
        acceptor_actidx = da_idxs[pos_acceptor]
        donor_actidx = da_idxs[pos_donor]
        if us[acceptor_actidx] < 0:
            delta = min(-us[acceptor_actidx], us[donor_actidx])
            us[acceptor_actidx] += delta
            us[donor_actidx] -= delta
        if us[donor_actidx] <= 0:
            pos_donor -= 1
        if us[acceptor_actidx] >= 0:
            pos_acceptor += 1
    return sum(t != u for t, u in zip(ts, us))


def main():
    _ = int(input())
    xs = [int(x) for x in input().split()]
    ys = [int(x) for x in input().split()]
    print(solve(xs, ys))


if __name__ == "__main__":
    main()

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

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


minus = [x for x in delta if x < 0]
plus = [x for x in delta if x >= 0]

plus.sort()
plus.reverse()
min_sum = sum(minus)
min_sum = min_sum * -1

num = -1
plu_sum = 0

if min_sum == 0:
    num = 0
else:
    for i in range(len(plus)):
        plu_sum += plus[i]
        if plu_sum >= min_sum:
            num = i + 1 + len(minus)
            break

print(num)

Solution (Python):
n = int(input())
a_array = [int(x) for x in input().split()]
b_array = [int(x) for x in input().split()]

plus = 0
minus = 0
plus_array = []
minus_cnt = 0
for i, j in zip(a_array, b_array):
    if j <= i:
        plus += i - j
        plus_array.append(abs(j - i))
    else:
        minus += j - i
        minus_cnt += 1
    

plus_array = sorted(plus_array, reverse=True)
plus_res = 0
if plus < minus:
    print(-1)
elif minus == 0:
    print(0)
else:
    for i in range(len(plus_array)):
        minus -= plus_array[i]
        plus_res += 1
        if minus < 0:
            print(plus_res + minus_cnt)
            exit()

Solution (Python):
import numpy as np
import sys

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)
    sys.exit()
c = A - B
c.sort()
i = 0
j = n - 1
ans = 2
if c[0] >= 0:
    print(0)
    sys.exit()
while True:
    if c[i] < 0:
        if c[j] + c[i] >= 0:
            c[j] += c[i]
            c[i] = 0
        else:
            c[i] += c[j]
            c[j] = 0
            j -= 1
            ans += 1
    else:
        if c[i + 1] >= 0:
            break
        else:
            i += 1
            ans += 1
print(ans)

Solution (Python):
import sys

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)]
sumdiff = 0
for i in range(n):
    sumdiff = sumdiff + diff[i]

if sumdiff < 0:
    print(-1)
    sys.exit


else:
    diff.sort()

    ans = 0
    i = 0
    while diff[i] < 0: 
        ans = ans + 1
        i = i + 1

    cost = 0
    for i in range(ans):
       cost = cost + diff[i]
    cost = -1 * cost


    j = n-1
    while cost > 0:
        cost = cost - diff[j]
        j = j - 1
        ans = ans + 1

    print(ans)

Solution (Python):


import math
import sys
import itertools
import numpy as np

n =int(input())


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

d = []
value  =0
value_d =0
number =0
for i in range(n):
    if a[i] < b[i]:
        value += b[i] - a[i]
        number += 1
    else:
        d.append(a[i]-b[i])
        value_d += a[i]-b[i]

if(value > value_d):
    print("-1")
    exit()
d.sort(reverse=True)
if value <= 0:
    print("0")
    exit()

for i in range(len(d)):
    value = value - d[i]
    number += 1
    if value <= 0:
        break
print(number)

Solution (Python):
N = int(input())
An = map(int, input().split(maxsplit=N))
Bn = map(int, input().split(maxsplit=N))

minus = []
plus = []

for Ai, Bi in zip(An, Bn):
    if Ai > Bi:
        plus.append(Ai - Bi)
    elif Ai < Bi:
        minus.append(Ai - Bi)

plus = sorted(plus)


if sum(plus) + sum(minus) < 0:
    print(-1)
    exit()


change_plus = 0
sum_minus = sum(minus)
while sum_minus < 0:
    sum_minus += plus.pop()
    change_plus += 1
print(str(change_plus + len(minus)))

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

count = 0
difference = []

for i in range(len(A)):
    difference.append(A[i] - B[i])

if min(difference) >= 0:
    print(count)
    quit()


difference.sort()
minussum = 0
for d in difference:
    if d < 0:
        count = count + 1
        minussum = minussum + d
difference.reverse()

plussum = 0
for d in difference:
    plussum = plussum + d
    count = count + 1
    if plussum + minussum >= 0:
        print(count)
        quit()

print(-1)

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

Solution (Python):
import sys

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

if sum(A) < sum(B):
    print(-1)
else:
    diff1 = 0
    num = 0
    diff2 = []
    for i in range(N):
        if A[i] < B[i]:
            diff1 += B[i] - A[i]
            num += 1
        elif A[i] > B[i]:
            diff2.append(A[i] - B[i])
    if num == 0:
        print(0)
    else:
        diff2.sort(reverse=True)
        sum = 0
        for i in range(len(diff2)):
            sum += diff2[i]
            if sum >= diff1:
                print(num + i + 1)
                sys.exit()

Solution (Python):
N = raw_input().split()
N = int(N[0])
a_list = raw_input().split()
a_list = [int(x) for x in a_list]
b_list = raw_input().split()
b_list = [int(x) for x in b_list]
if sum(a_list) < sum(b_list):
    print -1
else:
    diff_list = []
    for a, b in zip(a_list, b_list):
        diff_list.append(a-b)
    minus_sum = sum([x for x in diff_list if x < 0])
    minus_len = len([x for x in diff_list if x < 0])
    plus_list_sorted = sorted([x for x in diff_list if x > 0], reverse=True)
    tmp_idx = 0
    for item in plus_list_sorted:
        if minus_sum >= 0:
            break
        minus_sum += item
        tmp_idx += 1
    print minus_len + tmp_idx

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

num = 0
diff = 0
C = []
for a, b in zip(A, B):
  if a < b:
    num += 1
    diff -= (b - a)
  elif a > b:
    C.append(int(a - b))

if diff == 0:
  print(0)
else:
  C.sort(reverse=True)
  for v in C:
    num += 1
    diff += v
    if diff >= 0:
      break

  
  if diff < 0:
    print(-1)
  else:
    print(num)

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

sa=[]
cnt=0
ans=0
for ai, bi in zip(a, b):
    sa.append(ai-bi)
    if ai<bi:
        cnt+=bi-ai
        ans+=1
sa.sort(reverse=True)

for sai in sa:
    if cnt<=0:
        break
    cnt-=sai
    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) < 0:
    print(-1)
    exit()
C = []
ans = 0
count = 0
for i in range(N):
    if A[i]-B[i] < 0:
        ans += 1
        count += A[i]-B[i]
    elif A[i]-B[i] > 0:
        C.append(A[i]-B[i])
    else:
        continue
if count == 0:
    print(0)
    exit()
C.sort()
C.reverse()
for i in range(len(C)):
    count += C[i]
    ans += 1
    if count >= 0:
        print(ans)
        exit()
print(ans)

Solution (Python):
import numpy as np
flag=0
N=int(input())
A=np.array(list(map(int,input().split())))
B=np.array(list(map(int,input().split())))
if np.sum(A-B)<0:
    flag=-1
D=np.sort(A-B)
failNum=0
failSum=0
for i in range(N):
    if D[i]<0:
        failNum+=1
        failSum+=D[i]
passNum=0
if flag==0:
    while failSum<0:
        failSum+=D[N-1-passNum]
        passNum+=1
if flag==-1:
    print(-1)
else:
    print(failNum+passNum)

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

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

minus = 0
count = 0
for i in sub:
    if i >= 0:
        break
    count += 1
    minus += i
sub.reverse()

for i in sub:
    if minus >= 0:
        break
    minus += i
    count += 1
if minus < 0:
    print(-1)
else:
    print(count)

Solution (Python):
n = int(input())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))

if sum(aList)< sum(bList):
    print(-1)
else:
    saList=[]
    minusSum=0
    minusCount=0
    for i in range(n):
        sa=aList[i]-bList[i]
        if sa<0:
            minusCount+=1
            minusSum+=sa
        saList.append(sa)
    saList.sort()
    saList.reverse()
    
    i=0
    while(minusSum<0):
        minusSum=minusSum+saList[i]
        i+=1

    ans=minusCount+i
    print(ans)

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

ans = 0
s = []
negative = 0
for i, j in zip(a, b):
    s.append(i - j)
    if i - j < 0:
        negative += i - j
        ans += 1

s.sort(reverse=True)

if ans != 0:
    if sum(s) < 0:
        ans = -1
    else:
        for i , p in enumerate(s):
            negative += p
            if negative >= 0:
                ans += i + 1
                break
    

print(ans)

Solution (Python):
n = int(input())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
dif = [a[i] - b[i] for i in range(n)]
dif_abs = [abs(dif[i]) for i in range(n)]
sorted_dif = sorted(dif)
dif_minus = [(dif_abs[i] - dif[i]) // 2 for i in range(n)]
total_minus = sum(dif_minus)
len_minus = sum(x>0 for x in dif_minus)

res = 0

if sum(a) < sum(b):
    print(-1)
else:
    if len_minus == 0:
        print(res)
    else:
        res += len_minus
        while True:
            if total_minus <= 0:
                break
            total_minus -= sorted_dif.pop()
            res += 1

        print(res)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = sorted(a - b for a, b in zip(A, B))

def f():
    i, j = 0, len(D) - 1
    p = 0
    ans = 0
    
    while j >= i:
        
        if D[i] >= 0:
            return ans
        if p >= -D[i]:
            p += D[i]
            i += 1
            ans += 1
            continue
        p += D[j]
        j -= 1
        ans += 1
    if p >= 0:
        return ans
    return -1

print(f())

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = sorted(a - b for a, b in zip(A, B))

def f():
    i, j = 0, len(D) - 1
    p = 0
    ans = 0
    
    while j >= i:
        
        if D[i] >= 0:
            return ans
        if p >= -D[i]:
            p += D[i]
            i += 1
            ans += 1
            continue
        p += D[j]
        j -= 1
        ans += 1
    if p >= 0:
        return ans
    return -1

print(f())

Solution (Python):
N = int(input())
a_list = tuple(map(int, input().split()))
b_list = tuple(map(int, input().split()))
a_sum = sum(a_list)
b_sum = sum(b_list)

if b_sum > a_sum:
    print(-1)
    exit()

minus = {}
plus = {}
for i in range(N):
    a = a_list[i]
    b = b_list[i]
    tmp = a - b
    if tmp < 0:
        if tmp in minus:
            minus[tmp] += 1
        else:
            minus[tmp] = 1
    else:
        if tmp in plus:
            plus[tmp] += 1
        else:
            plus[tmp] = 1

minus = sorted(minus.items(), key= lambda x:x[0])
plus = sorted(plus.items(), key= lambda x:-x[0])
ans = 0
changed = 0
for k, v in minus:
    ans += v
    changed += (k * v)
if changed <0:
    for k, v in plus:
        for j in range(1, v+1):
            ans += 1
            changed += k
            if changed >= 0:
                print(ans)
                exit()
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)
elif all([a >= b for (a,b) in zip(A,B)]):
    print(0)
else:
    D = sorted([a-b for (a,b) in zip(A,B)])
    Negative = [d for d in D if d < 0]
    negative_sum = sum(Negative)
    positive_sum = 0
    count = 0
    for d in reversed(D):
        count += 1
        positive_sum += d
        if positive_sum > - negative_sum:
            break
    print(count + len(Negative))

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

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



plusdiff=[]
minusdiff=[]
diffcount=0
Asum=0
Bsum=0

for i in range(N):
  Asum+=A[i]
  Bsum+=B[i]
  if A[i]-B[i]>0:
    plusdiff.append(A[i]-B[i])
  elif A[i]-B[i]<0:
  	minusdiff.append(abs(A[i]-B[i]))


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

if Asum<Bsum:
  print(-1)
  exit()

plusdiff.sort(reverse=True)
minusdiff.sort(reverse=True)
  


answer=0
flag=2
while(len(minusdiff)!=0):
  answer+=flag
  if plusdiff[0]>minusdiff[0]:
    plusdiff[0]=plusdiff[0]-minusdiff[0]
    flag=1
    minusdiff.pop(0)
  elif plusdiff[0]==minusdiff[0]:
    plusdiff.pop(0)
    minusdiff.pop(0)
    flag=2
  else:
    minusdiff[0]=minusdiff[0]-plusdiff[0]
    plusdiff.pop(0)
  
    
print(answer)

Solution (Python):
n = input()
arr = map(int,raw_input().split())
brr = map(int,raw_input().split())
drr = []
for i in range(n):
	drr.append(arr[i]-brr[i])
drr.sort()
ans = 0
negsum = 0
for i in range(n):
	if drr[i] >= 0:
		break
	else:
		negsum += abs(drr[i])
		ans += 1
drr = drr[::-1]
possum = 0
for i in range(n):
	if drr[i] <= 0:
		break
	else:
		if possum >= negsum:
			break
		else:
			possum += drr[i]
			ans += 1
if possum >= negsum:
	print ans
else:
	print "-1"

Solution (Python):
import sys
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
l = []
sumA = sum(A)
sumB = sum(B)

tmp = 0
cnt = 0
if sumA < sumB:
    print("-1")
    sys.exit()
else:
    for i in range(N):
        if A[i] >= B[i]:
            l.append(A[i]-B[i])
        else:
            tmp += A[i]-B[i]
            cnt += 1
l.sort(reverse=True)

j = 0
while tmp < 0:
    tmp += l[j]
    cnt += 1
    j += 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)
    exit()
d = [A[i]-B[i] for i in range(N)]
d.sort()
neg = 0
count = 0
for e in d:
    if e < 0:
        neg += e
        count += 1
        continue
    break
for e in d[::-1]:
    if neg >= 0:
        break
    neg += e 
    count += 1
print(count)

Solution (Python):
import bisect
def 解():
    N = int(input())
    aA=[int(_) for _ in input().split()]
    aB=[int(_) for _ in input().split()]
    aD =[aA[i]-aB[i] for i in range(N)]

    if sum(aD) < 0:
        print(-1)
        exit()
    else:
        aD.sort()
        i0 = bisect.bisect_left(aD,0)
        if i0 == 0:
            print(0)
        elif i0 == N-1 :
            print(N)
        else:
            iM = sum(aD[:i0])
            for i in range(N-1,i0 - 1 ,-1):
                iM += aD[i]
                if iM > 0:
                    print(i0 + (N - i))
                    exit()
解()

Solution (Python):
import numpy as np

def judge(A, B):
    sum_A, sum_B = sum(A), sum(B)
    if sum_A < sum_B:
        return -1
    D = np.sort(np.array(A) - np.array(B))
    num = 0
    pid = D.argmax()
    nid = len(D[D < 0]) - 1
    pnew, nnew = True, True
    while nid >= 0:
        if D[pid] + D[nid] > 0:
            num += pnew + nnew
            D[pid] += D[nid]
            D[nid] = 0
            nid -= 1
            pnew, nnew = False, True
        elif D[pid] + D[nid] == 0:
            num += pnew + nnew
            D[pid] = 0
            D[nid] = 0
            pid -= 1
            nid -= 1
            pnew, nnew = True, True
        else:
            num += pnew + nnew
            D[nid] += D[pid]
            D[pid] = 0
            pid -= 1
            pnew, nnew = True, False
    return num

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

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()
count=0
if sum(d)<0:
    print(-1)
    quit()
e=0
f=0
for i in d:
    if i<0:
        e+=i
        count+=1
while e<0:
    e+=d[-1]
    d.pop(-1)
    count+=1
print(count)

Solution (Python):
def main():
    """
    1 <= N <= 10^5
    1 <= Ai,Bi <= 10^9
    """
    N = int(input())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    ans = f(N, A, B)
    print(ans)


def a(N, A, B):
    ans = 0
    return ans


def f(N, A, B):
    diff = [b - a for a, b in zip(A, B)]
    diff = sorted(diff)
    diff_sum = sum(diff)

    if diff_sum > 0:
        return -1

    if len([d for d in diff if d <= 0]) == N:
        return 0

    start = 0
    end = N - 1
    used = [False] * N
    while start < end:
        ds = diff[start]
        de = diff[end]
        if ds <= 0 and de <= 0:
            break

        if ds < 0 and de > 0:
            cost = min(abs(ds), de)
            diff[start] += cost
            diff[end] -= cost

            used[start] = True
            used[end] = True

        if diff[start] >= 0:
            start += 1
        if diff[end] <= 0:
            end -= 1

    ans = sum(used)
    return ans


if __name__ == "__main__":
    main()

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)


sumA = sum(A)
sumB = sum(B)

if sumA < sumB:
    print(-1)
else:
    gap = A - B
    minus = gap[gap<0]
    numminus = len(gap[gap<0])
    if numminus == 0:
        print(0)
    else:
        needs = - minus.sum()
        sortgap = np.sort(gap)[::-1]
        s = 0
        for i in range(N):
            s += sortgap[i]
            if s >= needs:
                changedplus = i+1
                break
        print(changedplus + numminus)

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


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

    diff.sort()

    minus = sum([d for d in diff if d < 0])
    result = len([d for d in diff if d < 0])

    for d in diff[::-1]:
        if minus >= 0:
            break
        minus += d
        result += 1

    return(result)

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

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

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

c.sort(reverse=True)
d=np.array(c)
z=np.sum(d < 0)

x=np.sum(d[ d < 0])
y=np.sum(d[ d > 0])

if x == 0:
    print("0")
    exit()

if x + y < 0:
    print("-1")
    exit()    

for i,num in enumerate(c):
    x += num
    if x >= 0:
        print(z+i+1)
        exit()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
diff = [0 for _ in range(N)]
for i in range(N):
    diff[i] = A[i] - B[i]
diff = sorted(diff)
diff_copy = diff.copy()

i = 0
j = N-1

flag = True
while True:
    if diff[i]<0:
        while True:
            if i>=N or j<0 or i==j:
                flag = False
                break

            if abs(diff[i])>diff[j]:
                diff[i] += diff[j]
                diff[j] = 0
                j -= 1
            else:
                diff[j]-=abs(diff[i])
                diff[i] = 0
                break
        i += 1
    else:
        break



if flag:
    ans = 0
    for i in range(N):
        if diff_copy[i]!=diff[i]:
            ans += 1
    print(ans)
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(m) for m in input().split()]
b = [int(m) for m in input().split()]
difp = []
difm = []
difmmm = []
difr = []
for i in range(n):
    if a[i] - b[i] >= 0:
        difp.append([a[i] - b[i], i])
    else:
        difm.append([a[i] - b[i], i])
        difmmm.append(1)
        difr.append(abs(a[i] - b[i]))

difp.sort(reverse=True)

if sum(a) < sum(b):
    print(-1)
    exit()
Q = sum(difmmm)
R = sum(difr)
tmp = 0
sign = Q
if difm == []:
    print(0)
    exit()
for i in range(len(difp)):
    tmp += difp[i][0]
    if tmp >= R:
        sign += i + 1
        break

print(sign)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
dis1 = []
dis2 = []
num = 0
ans = 0
tmp = 0

for i in range(N):
    num = A[i] - B[i]
    if num > 0:
        dis1.append(num)
    elif num < 0:
        dis2.append(num)

if sum(A) < sum(B):
    ans = -1
elif len(dis2) == 0:
    ans = 0
else:
    dis1.sort(reverse = True)
    num = sum(dis2)
    ans += len(dis2)
    for i in range(len(dis1)):
        num += dis1[i]
        ans += 1
        if num >= 0:
            break

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)


if np.sum(a) < np.sum(b):
    ans = -1
else:

    diff = a - b
    diff = np.sort(diff)
    if np.sum(diff<0) == 0:
        ans = 0
    else:
        for ind in range(len(diff)):
            if diff[ind] > 0:
                break

        minus = np.sum(diff[:ind])
        minus_num = ind

        yoyuu = diff[ind:][::-1]

        for ind in range(len(yoyuu)):
            minus += yoyuu[ind]
            if minus > 0:
                ans = minus_num + ind + 1
                break
            if minus == 0:
                ans = minus_num + ind
                break

print(ans)

Solution (Python):

n = int(input())

a = [int(i) for i in input().split()]

b = [int(i) for i in input().split()]
c = [0 for _ in range(n)]
for i in range(n):
    c[i] = a[i] - b[i]
if sum(c) < 0:
    print(-1)
    exit()
c.sort()
count = 0
minus = 0
for i in range(n):
    if c[i] < 0:
        minus += c[i]
        count += 1
    else:
        break
for i in reversed(range(n)):
    if minus < 0:
        minus += c[i]
        count += 1
    else:
        print(count)
        exit()

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

q = []
p = []
d = 0

for i in range(n):
    if a[i] - b[i] < 0:
        q.append(a[i]-b[i])
    elif a[i] - b[i] > 0:
        p.append(a[i]-b[i])
    else:
        d += 1

s = sum(q)
p = sorted(p)
while s < 0:
    if p == []:
        print(-1)
        exit()
    else:
        t = p.pop()
        s += t

print(n - d - len(p))

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)


sumA = sum(A)
sumB = sum(B)

if sumA < sumB:
    print(-1)
else:
    gap = A - B
    minus = gap[gap<0]
    numminus = len(gap[gap<0])
    if numminus == 0:
        print(0)
    else:
        needs = - minus.sum()
        sortgap = np.sort(gap)[::-1]
        s = 0
        for i in range(N):
            s += sortgap[i]
            if s >= needs:
                changedplus = i+1
                break
        print(changedplus + numminus)

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

a_sum = 0
b_sum = 0
diff = list()
for i in range(n):
  a_sum += int(a[i])
  b_sum += int(b[i])
  diff.append(int(a[i])-int(b[i]))

if a_sum < b_sum:
  print(-1)
else:
  diff.sort()

  minus = 0
  for i in range(n):
    if diff[i] < 0:
      minus += diff[i]
    else:
      change1 = i
      break

  plus = 0
  for i in range(n-1, -1, -1):
    plus += diff[i]
    if plus >= -minus:
      change2 = n-i
      break
  
  if minus == 0:
    change2 = 0
  
  print(change1 + change2)

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

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

diff = sorted(diff, reverse=True)
cumsum = [0] * (N + 1)
for i in range(N):
    cumsum[i] += diff[i] + cumsum[i - 1]







def check(x):
    a = x - minus_count
    if a < 0:
        return False

    plus_sum = cumsum[a - 1]
    
    
    
    if plus_sum >= minus_sum:
        return True


def main():
    if sum(A) < sum(B):
        print(-1)
        return

    sup = N
    inf = -1
    while sup - inf > 1:
        mid = (sup + inf) // 2

        if check(mid):
            sup = mid
        else:
            inf = mid

    print(sup)


if __name__ == '__main__':
    main()

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

dic = []
S = [0,0]
minus = [0,0]
for i in range(N):
    a = int(A[i])
    b = int(B[i])
    S[0]+=a
    S[1]+=b
    k = a-b
    if k >= 0:
        dic.append(k)
    else:
        minus[0]+=1
        minus[1]+=k
if S[0] < S[1]:
    print(-1)
    exit()

lis = sorted(dic,reverse=True)

n=0
while minus[1]<0:
    minus[0]+=1
    minus[1]+=lis[n]
    n+=1


print(minus[0])

Solution (Python):
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())), np.int64)
B = np.array(list(map(int, input().split())), np.int64)
if sum(B) > sum(A):
    print(-1)
else:
    diff = A - B
    sort_idx = diff.argsort()
    
    
    diff = diff[sort_idx]
    l = 0
    r = N-1
    ans = sum(diff < 0)
    if ans != 0:
        ans += 1
    while True:
        if diff[l] >= 0:
            break
        if diff[r] >= -diff[l]:
            diff[r] += diff[l]
            diff[l] = 0
            l += 1
        else:
            diff[l] += diff[r]
            diff[r] = 0
            r -= 1
            ans += 1
    print(ans)

Solution (Python):
n = int(input())
import numpy as np
a = np.zeros(n, dtype=int)
b = np.zeros(n, dtype=int)
buf1  = input().split()
buf2  = input().split()
for i in range(n):
	a[i] = int(buf1[i])
	b[i] = int(buf2[i])
r = 0
if(np.sum(a) < np.sum(b)):
	r = -1
else:
	c = a - b
	c = np.sort(c)
	if(c[0] >= 0):
		r = 0
	else:
		ii = -1
		for i in range(n):
			if(c[i] >= 0):
				ii = i
				break
		debt = np.sum(c[:i])
		for i in range(n):
			debt += c[n-1-i]
			if(debt >= 0):
				r = ii+(i+1)
				break
print(r)

Solution (Python):
import math
import copy




def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l

n=nCast(input())
a=nCast(input().split())
b=nCast(input().split())
c=list()
def func():
    result=0
    if sum(a)<sum(b):
        return -1
    for idx in range(0,len(a)):
        c.append(a[idx]-b[idx])
    c.sort()
    need=0
    result=0

    for number in c:
        if number<0:
            need+=number
            result+=1
        else:
            break

    if need==0:
        return 0
    for number in reversed(c):
        result+=1
        need+=number
        if need>=0:
            break
    return result

print(func())

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

    diff_sum = 0
    diff_cnt = 0
    diff_list = [0] * N
    for i in range(N):
        diff_list[i] = A[i] - B[i]
        if A[i] < B[i]:
            diff_sum += B[i] - A[i]
            diff_cnt += 1

    diff_list_sorted = sorted(diff_list, reverse=True)

    use_sum = 0
    for i in range(N):
        if use_sum >= diff_sum:
            break
        use_sum += diff_list_sorted[i]
        diff_cnt += 1

    return diff_cnt


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

Solution (Python):
def main():
    n = int(input())
    As = list(map(int, input().split()))
    sumA = sum(As)
    Bs = list(map(int, input().split()))
    sumB = sum(Bs)

    if sumB > sumA:
        print(-1)
        return

    pos = []
    neg = []
    for i in range(n):
        diff = As[i] - Bs[i]

        if diff > 0:
            pos.append(diff)
        elif diff < 0:
            neg.append(diff)

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

    work_neg = sum_neg
    i = 0
    while work_neg < 0:
        work_neg += pos[i]
        i += 1

    ans = len(neg) + i
    print(ans)

    return

main()