Problem:
 Score : 400 points    Problem Statement A university student, Takahashi, has to take N examinations and pass all of them.
Currently, his readiness for the i -th examination is A_{i} , and according to his investigation, it is known that he needs readiness of at least B_{i} in order to pass the i -th examination.  Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness.  For Takahashi, find the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the following conditions:   The sum of the sequence A_1, A_2, ..., A_{N} and the sum of the sequence C_1, C_2, ..., C_{N} are equal.  For every i , B_i \leq C_i holds.   If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1 .      Constraints  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^9  A_i and B_i are integers.         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_{N}  B_1  B_2  ...  B_{N}       Output Print the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the conditions.
If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1 .        Sample Input 1 3
2 3 5
3 4 1      Sample Output 1 3  (A_1, A_2, A_3) = (2, 3, 5) and (B_1, B_2, B_3) = (3, 4, 1) . If nothing is done, he cannot pass the first and second exams.
The minimum possible number of indices i such that A_i and C_i are different, 3 , is achieved when:   (C_1, C_2, C_3) = (3, 5, 2)        Sample Input 2 3
2 3 3
2 2 1      Sample Output 2 0  In this case, he has to do nothing in order to pass all the exams.       Sample Input 3 3
17 7 1
25 6 14      Sample Output 3 -1  In this case, no matter what is done, he cannot pass all the exams.       Sample Input 4 12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604
74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212      Sample Output 4 5

Solution (Python):

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    pos, neg = [], []
    for i, j in zip(A, B):
        x = i - j
        if x >= 0:
            pos += [x]
        else:
            neg += [x]
    ans, shortage = len(neg), sum(neg)
    for i in sorted(pos, reverse=1):
        if shortage >= 0:
            print(ans)
            return
        ans += 1
        shortage += i
    print(ans if shortage >= 0 else -1)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    dif = sorted(i - j for i, j in zip(A, B))
    ans, shortage = 0, 0
    for i in dif:
        if i < 0:
            ans += 1
            shortage += i
        else:
            break
    for i in dif[::-1]:
        if shortage >= 0:
            print(ans)
            return
        ans += 1
        shortage += i
    print(-1)


if __name__ == "__main__":
    main()

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

diff = [0] * (N)
diffsum = 0
for i in range(N):
    diff[i] = A[i] - B[i]
    diffsum += diff[i]
if diffsum < 0:
    print("-1")
    exit()
diff.sort()
count = 0
sumn = 0
for i in range(N):
    if diff[i] < 0:
        count += 1
        sumn -= diff[i]
    else:
        break
for j in range(N)[::-1]:
    if sumn > 0:
        count += 1
        sumn -= diff[j]
print(count)

Solution (Python):
n,*l=map(int,open(0).read().split())
d=sorted([a-b for a,b in zip(l[:n],l[n:])])+[0]
m=[x for x in d if x<0];s=sum(m)
c=~n-1 if sum(d)<0 else len(m)-1
for i in d[::-1]:
	s+=i;c+=1
	if s>=0:
		break
print(c)

Solution (Python):
n,*l=map(int,open(0).read().split())
d=sorted([a-b for a,b in zip(l[:n],l[n:])])
s=sum(d)
if s<0:
	print(-1)
else:
	minus=[x for x in d if x<0]
	ms=sum(minus)
	cnt=len(minus) if minus!=[] else -1
	for i in d[::-1]:
		ms+=i
		cnt+=1
		if ms>=0:
			print(cnt)
			exit()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np

N = int(input())
A = np.array(input().split(),dtype=np.int64)
B = np.array(input().split(),dtype=np.int64)

diff = A-B
lack = -diff[diff<0]
remain = diff[diff>0]

x = lack.sum()
remain.sort()
cum = remain[::-1].cumsum()
y = 0 if x == 0 else np.searchsorted(cum,x) + 1

answer = len(lack) + y if y <= len(remain) else -1

print(answer)

Solution (Python):
from collections import defaultdict
import heapq

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

if sum(a) < sum(b):
    ans = -1
else:
    ans = 0
    res = 0
    rep = []
    for i in range(N):
        diff = b[i] - a[i]
        if diff > 0:
            ans += 1
            res += diff
        elif diff < 0:
            heapq.heappush(rep, diff)
    while res > 0:
        ans += 1
        res += heapq.heappop(rep)

print(ans)

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

c=sorted([a[i]-b[i] for i in range(n)],reverse=True)

if sum(c)<0:
    print(-1)
else:
    d=[i for i in c if i<0]
    cnt=len(d)
    d=abs(sum(d))
    i=0
    while d>0:
        d-=c[i]
        cnt+=1
        i+=1
    print(cnt)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
OK = []
NG = 0
count = 0
for i in range(N):
    x = A[i] - B[i]
    if x < 0:
        NG -= x
        count += 1
    else:
        OK.append(x)
l = len(OK)
OK.sort(reverse = True)
i = 0
while NG > 0 and i < l:
    NG -= OK[i]
    count += 1
    i += 1
if NG <= 0:
    print(count)
else:
    print(-1)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
b = list(map(int,sys.stdin.readline().split()))
s = 0
c = []
ans = 0
for i in range(n):
    if a[i] < b[i]:
        s += b[i] - a[i]
        ans += 1
    else:
        c.append(a[i]-b[i])
c.sort(reverse = True)
for i in c:
    if s > 0:
        s -= i
        ans += 1
    else:
        break
if s > 0:
    print(-1)
else:
    print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from bisect import bisect_left
from itertools import accumulate

n = ni()
a = list(li())
b = list(li())

a_larger = []
lack = 0
ans = 0



for ai, bi in zip(a, b):
    if ai > bi:
        a_larger.append(ai - bi)
    elif ai < bi:
        lack += (bi - ai)
        ans += 1

a_larger.sort(reverse=True)
a_larger_cum = [0] + list(accumulate(a_larger))


idx = bisect_left(a_larger_cum, lack)
if idx >= len(a_larger_cum):
    print(-1)
else:
    print(ans + idx)

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

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


A.sort()


tmp = 0
ans = 0
for i in A:
    if i < 0:
        ans += 1
        tmp -= i
    else:
        break

for i in range(N - 1, -1, -1):
    if tmp <= 0:
        print (ans)
        exit()
    if A[i] > 0:
        tmp -= A[i]
        ans += 1
    else:
        print (-1)
        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()


ans = 0
sa = [0] * n
tmp = 0
idx = 0
for i in range(n):
    if a[i] < b[i]:
        tmp -= a[i] - b[i]
        ans += 1
    elif a[i] > b[i]:
        
        sa[idx] = a[i] - b[i]
        idx += 1

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



r  = sorted(sa[:idx], reverse=True)



for i in range(idx):
    if tmp > r[i]:
        ans += 1
        tmp -= r[i]
    else:
        ans += 1
        
        break
print(ans)

Solution (Python):
from heapq import heappush,heappop
I=lambda:list(map(int,input().split()));n,a,b=int(input()),I(),I()
w=x=y=z=0
c,h=[],[]
for i in range(n):
  x+=a[i];y+=b[i]
  if a[i]<b[i]:c.append(b[i]-a[i]);z+=1
  else:heappush(h,b[i]-a[i])
if x<y:print(-1);exit()
for i in c:
  w-=i
  while w<0:w+=-heappop(h);z+=1
print(z)

Solution (Python):
from bisect import bisect_right
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a) == sum(b):
    print(n)
mp = 0
ans = 0
l = []
for i in range(n):
    if a[i] < b[i]:
        mp -= b[i] - a[i]
    elif a[i] >= b[i]:
        mp += a[i] - b[i]
        l.append(a[i] - b[i])
l.sort()
if mp < 0:
    print(-1)
else:
    for i in range(len(l) - 1):
        l[i + 1] += l[i]
    print(n - bisect_right(l, mp))

Solution (Python):
import sys
input = sys.stdin.readline
  
def solve():
  n = int(input())
  a = list(int(i) for i in input().split())
  b = list(int(i) for i in input().split())
  
  if sum(a) < sum(b):
    print(-1)
    exit()
  else:
    sabun = [0]*n
    for i in range(n):
      sabun[i] = a[i]-b[i]
    sabun = sorted(sabun,reverse = False)
    sabun2 = sabun[::-1]
    cnt = 0
    husai = 0
    for i in range(n):
      if sabun[i] <0:
        cnt += 1
        husai += sabun[i]
      else:
        break
    j = 0
    while husai<0:
      husai += sabun2[j]
      cnt += 1
      j+=1
    print(cnt)
solve()

Solution (Python):
import math
N = int(input())
As = list(map(int,input().split(" ")))
Bs = list(map(int,input().split(" ")))
over = []
under = []
for a,b in zip(As,Bs):
    if a < b:
        under.append(b-a)
    if a > b:
        over.append(a-b) 

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

over.sort(reverse=True)
under_sum = sum(under)
l = len(over)
for i in range(l):
    under_sum -= over[i]
    if under_sum  <= 0:
        break
if under_sum  > 0:
    print(-1)
else:
    print(i+ 1 + len(under))

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

C = sorted(a-b for a,b in zip(A,B))
ans = 0
S = 0
for c in C:
    if c>=0:break
    S += c
    ans += 1
for c in C[::-1]:
    if c<=0:break
    if S>=0:break
    S+=c
    ans += 1
if S<0:ans=-1
print(ans)

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

BA = [b-a for a, b in zip(A, B) if b > a]
AB = sorted([a-b for a, b in zip(A, B) if a > b], reverse=True)
if len(BA) == 0:
  print(0)
else:
  S = sum(BA)
  Sd = 0
  for i, ab in enumerate(AB):
    Sd += ab
    if Sd >= S:
      break
  if Sd >= S:
    print(len(BA)+i+1)
  else:
    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())))
c=a-b
c.sort()
g=0
p=-1
for i in range(n):
    if c[i]<0:
        g+=c[i]
    else:
        break
if g==0:
    print(0)
else:
    for j in range(1,n+1):
        if g>=0:
            p=0
            break
        if c[-j]>0:
            g+=c[-j]
        else:
            break
    if p==-1:
        print(-1)
    else:
        print(i+j-1)

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

offset = []
for n in range(N):
	offset.append(A[n] - B[n])

offset.sort()

count = 0
sum_minus = 0
for n in offset:
	if 0 <= n:
		break
	count += 1
	sum_minus += n

for n in offset[::-1]:
	if 0 <= sum_minus:
		break
	if n < 0:
		count = -1
		break
	count += 1
	sum_minus += n

print(count)

Solution (Python):
N=int(input())
*A, = map(int,input().split())
*B, = map(int,input().split())
D = sorted(a-B[i] for i,a in enumerate(A))
S = 0
for ans,d in enumerate(D):
    if d>=0:break
    S += d
if S==0:ans = -1
for i,d in enumerate(D[::-1],1):
    S += d
    if S>=0:break
ans = (-1) if S<0 else (ans+i)
print(ans)

Solution (Python):
import numpy as np
n = int(input())
an = np.array([int(i) for i in input().split()])
bn = np.array([int(i) for i in input().split()])
difn = an - bn
cnt = 0
m_ab = 0

difn.sort()

for i in range(n):
  if difn[i] < 0:
    m_ab += difn[i]
    cnt += 1
  else:
    break

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

for i in range(1,n+1):
  if difn[-i] <= 0:
    print(-1)
    exit()
  m_ab += difn[-i]
  cnt += 1
  if m_ab >= 0:
    print(cnt)
    exit()

Solution (Python):
n = int(input())
an = [int(i) for i in input().split()]
bn = [int(i) for i in input().split()]
difn = [0]*n
cnt = 0
m_ab = 0

for i in range(n):
  difn[i] = an[i] - bn[i]
difn.sort()

for i in range(n):
  if difn[i] < 0:
    m_ab += difn[i]
    cnt += 1
  else:
    break

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

for i in range(1,n+1):
  if difn[-i] <= 0:
    print(-1)
    exit()
  m_ab += difn[-i]
  cnt += 1
  if m_ab >= 0:
    print(cnt)
    exit()

Solution (Python):
N=int(input())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))

ablist=[]
for i in range(N):
  ablist.append(alist[i]-blist[i])
  
ablist.sort()


if ablist[0] >= 0:
  print("0")
elif sum(ablist) < 0:
  print("-1")
else:
  cnt = 0
  sum_ab = 0
  for ab in ablist:
    if ab<0:
      cnt += 1
      sum_ab += ab
    else:
      break
      
  for ab in reversed(ablist):
    cnt += 1
    sum_ab += ab
    if sum_ab >= 0:
      break
      
  print(cnt)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
lower = 0
cnt = 0
upper = []
for ind,a in enumerate(A):
    if(a<B[ind]):
        lower+=B[ind]-a
        cnt+=1
    else:
        upper.append(a-B[ind])
upper.sort(reverse=True)
surpass = 0
for i in upper:
    if(surpass>=lower):
        break
    surpass+=i
    cnt+=1
    if(surpass>=lower):
        break
else:
    print(-1)
    quit()
print(cnt)

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
B = list(map(int, sys.stdin.readline().split()))

A = np.array(A)
B = np.array(B)

diffs = A - B

count = (diffs < 0).sum()
insufficient = -diffs[diffs < 0].sum()

diffs.sort()
redundant = diffs[diffs > 0][::-1]

sub = np.searchsorted(redundant.cumsum(), insufficient) + 1

if insufficient == 0:
    print(0)
elif sub > len(redundant):
    print(-1)
else:
    print(count + sub)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
pdiff=[]
ndiff=[]
asum=0
bsum=0
cnt=0
for i in range(n):
  asum+=a[i]
  bsum+=b[i]
  if a[i]-b[i]>=0:
    pdiff.append(a[i]-b[i])
  else:
    ndiff.append(b[i]-a[i])
    cnt+=1
pdiff=sorted(pdiff,reverse=True)
if bsum>asum:
  print(-1)
else:
  psum=0
  nsum=sum(ndiff)
  ppos=0
  npos=0
  l=len(ndiff)
  while psum<nsum:
    psum+=pdiff[ppos]
    ppos+=1
  print(cnt+ppos)

Solution (Python):
import numpy as np
def solve(n, a, b):
    a = np.asarray(a)
    b = np.asarray(b)
    if np.sum(a) < np.sum(b):
        return -1
    d = b - a
    ans = 0
    points = 0
    for i in range(n):
        if d[i] > 0:
            points += d[i]
            d[i] = 0
            ans += 1
    for v in sorted(d):
        if points <= 0:
            return ans
        points += v
        ans += 1
    return ans

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

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()
diff = [A[i]-B[i] for i in range(N)]
m = 0
cnt = 0
for d in diff:
    if d < 0:
        m += d
        cnt += 1
diff.sort(reverse = 1)
for d in diff:
    if m >= 0:
        break
    m += d
    cnt += 1  
print(cnt)

Solution (Python):
n = int(input())
a = [int(item) for item in input().split()]
b = [int(item) for item in input().split()]

asubb = []
for aa, bb in zip(a, b):
    asubb.append(aa - bb)
asubb.sort(reverse=True)

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

minus_sum = 0
ans = 0
for item in asubb[::-1]:
    if item >= 0:
        break
    minus_sum += item
    ans += 1

for item in asubb:
    if minus_sum >= 0:
        break
    minus_sum += item
    ans += 1

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mp = 0
ans = 0
l = []
for i in range(n):
    if a[i] < b[i]:
        mp -= b[i] - a[i]
    else:
        ans += 1
        l.append(a[i] - b[i])
l.sort(reverse=True)
for p in l:
    if mp == 0:
        break
    mp += min(p, -mp)
    ans -= 1
if mp < 0:
    print(-1)
else:
    print(n - 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 = sorted([a - b for a, b in zip(A, B)], reverse=True)
D_orig = D[:]
l = 0
r = N - 1
while D[r] < 0:
    if D[l] + D[r] >= 0:
        D[l] += D[r]
        D[r] = 0
        r -= 1
    else:
        D[r] += D[l]
        D[l] = 0
        l += 1
print(N - sum([d == o for d, o in zip(D, D_orig)]))

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

over = 0
loss = 0
c = []
for i in range(N):
    d = B[i] - A[i]
    if d > 0:
        loss += d
    else:
        over -= d
        c += [-d] 
   
c.sort()
amari = over - loss
cnt = 0
for i in range(len(c)):
    if c[i] > amari:
        break
    else:
        amari -= c[i]
        cnt += 1
        
        
if over >= loss:
    print(N-cnt)
else:
    print(-1)

Solution (Python):
from bisect import*
from numpy import*
n=int(input())
*a,=map(int,input().split())
*b,=map(int,input().split())
x=array(a)-array(b)
y=x[x<0]
z=sorted(x[x>0],reverse=1)

if len(y)==0:
    print(0)
elif sum(y)+sum(z)<0:
    print(-1)
else:
    ans=len(y)
    ans+=bisect_left(cumsum(z),-sum(y))+1
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
diff = []
for a, b in zip(A, B):
    diff.append(a - b)
if sum(diff) < 0:
    print(-1)
else:
    positive = [i for i in diff if i > 0]
    negative = [i for i in diff if i < 0]
    if len(negative):
        numNegative = count = len(negative)
        sumNegative = sum(negative)
        totalPositive = 0
        for p in sorted(positive, reverse=True):
            count += 1
            sumNegative += p
            if sumNegative > 0:
                break
        print(count)
    else:
        print(0)

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

def solve(A,B):
    if sum(A)<sum(B):
        return -1
    ans=0
    s=0
    L=[]
    for a,b in zip(A,B):
        d=a-b
        if d<0:
            ans+=1
            s-=d
        else:
            L.append(d)
    if ans==0:
        return 0
    L.sort()
    t=0
    while t<s:
        t+=L.pop()
        ans+=1
    return ans

print(solve(A,B))

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

def solve(A,B):
    if sum(A)<sum(B):
        return -1
    ans=0
    s=0
    L=[]
    for a,b in zip(A,B):
        d=a-b
        if d<0:
            ans+=1
            s-=d
        else:
            L.append(d)
    if ans==0:
        return 0
    L.sort()
    t=0
    while t<s:
        t+=L.pop()
        ans+=1
    return ans

print(solve(A,B))

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

Solution (Python):

"""
C - Exam and Wizard
https://atcoder.jp/contests/keyence2019/tasks/keyence2019_c

"""
import sys


def solve(N, A, B):
    total_A = sum(A)
    total_B = sum(B)
    if total_A < total_B:
        return -1
    r = total_A - total_B
    margins = sorted([a-b for a, b in zip(A, B) if a-b >= 0], reverse=True)
    ans = 0
    while margins and margins[-1] <= r:
        r -= margins.pop()
        ans += 1
    return N-ans


def main(args):
    N = int(input())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    ans = solve(N, A, B)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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

d = []

p = 0
m = 0

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

if p<m:
    print(-1)
    exit()

d.sort()
d.reverse()

i = 0
while m>0:
    m-=d[i]
    ans += 1
    i += 1
print(ans)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
sumA = sum(As)
sumB = sum(Bs)
D = sumA-sumB
if sumA < sumB:
    ans = -1
else:
    ans = 0
    ds = 0
    dif = [As[i]-Bs[i] for i in range(N)]
    dif.sort()
    for d in dif:
        if d < 0:
            ds += d
            ans += 1
        else:
            break
    for i in range(1, N+1):
        if ds < 0:
            ds += dif[-i]
            ans += 1
        else:
            break
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if sum(a)<sum(b):
    print(-1)
    exit()
c = []
s = 0
cnt = 0
for i in range(n):
    if a[i]-b[i]>=0:
        c.append(a[i]-b[i])
    else:
        s+= a[i]-b[i]
        cnt+=1
c = sorted(c)[::-1]
for i in c:
    if s>=0:
        break
    s+=i
    cnt+=1
print(cnt)

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[]
count,ans=0,0
for i in range(n):
  if A[i]<B[i]:
    count+=B[i]-A[i]
    ans+=1
  elif A[i]>B[i]:
    C.append(A[i]-B[i])
C.sort(reverse=True)
c=0
while count>0 and c<len(C):
  count-=C[c]
  c+=1
if count<=0:
  print(ans+c)
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)
else:
    c = list(a[i]-b[i] for i in range(n))
    c.sort()
    if c[0] >= 0:
        print(0)
    else:
        extra = sum(c)
        cnt = 0
        for i in range(n):
            if c[i] > 0 and c[i] <= extra:
                extra -= c[i]
                cnt += 1
        print(n-cnt)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[]
dif=0
ans=0
if sum(a)<sum(b):
    print(-1)
else:
    for i in range(n):
        x,y=a[i],b[i]
        if x<y:
            dif+=y-x
            ans+=1
        else:
            c.append(x-y)
    c.sort()
    while c and dif>0:
        ans+=1
        dif-=c.pop()
    print(ans)

Solution (Python):
N = int(input())
Ali = [ int(it) for it in input().split() ]
Bli = [ int(it) for it in input().split() ]

Sli = [ Bli[i] - Ali[i] for i in range(N) ]

if ( sum(Sli)>0 ):
  print (-1)
else:
  po = [ it for it in Sli if it>0 ]
  ng = [ it for it in Sli if it<0 ]
  agr = sum(po)
  ng.sort()
  
  for i in range(1000000):
    if (agr<=0):
      print (len(po)+i)
      break
    agr+=ng[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)
else:
  D = [a-b for a,b in zip(A,B)]
  D.sort()
  
  import bisect
  i = bisect.bisect_left(D,0)
  up = - sum(D[:i])
  
  j=0
  while up>0:
    j += 1
    up -= D[-j]
    
  print(i + j)

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


surplus = [(a - b) for a, b in zip(A, B) if (a - b) > 0]
surplus.sort(reverse=True)
surplus_acc = [0] + list(accumulate(surplus))

short = [(b - a) for a, b in zip(A, B) if (b - a) > 0]
need = sum(short)
short_num = len(short)


if need > sum(surplus):
    print(-1)
    exit()
else:
    for i in range(len(surplus_acc)):
        if surplus_acc[i] >= need:
            print(i + short_num)
            exit()

Solution (Python):
if __name__=="__main__":
    n = int(input())
    A = [int(i) for i in input().split(' ')]
    B = [int(i) for i in input().split(' ')]
    allA = 0
    allB = 0
    for a, b in zip(A, B):
        allA += a
        allB += b
    if (allA < allB):
        print(-1)
        exit(0)
    C = []
    sum = 0
    ans = 0
    for i in range(n):
      if (A[i] < B[i]):
          sum += A[i] - B[i]
          ans += 1
      else:
          C.append(A[i] - B[i])
    if (sum >= 0):
        print(ans)
        exit(0)
    C = sorted(C)
    C.reverse()
    for i in (C):
        sum += i
        ans += 1
        if (sum >= 0):
            print(ans)
            exit(0)

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:
    x,num,m = [],0,0
    for i in range(n):
        if a[i]<b[i]:
            num+=b[i]-a[i]
            m+=1
        else: x.append(b[i]-a[i])
    x,k = sorted(x),0
    while num>0:
        num+=x[k]
        k+=1
    print(m+k)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [A[i] - B[i] for i in range(N)]

ans = 0
p = 0
q = 0
l = []

for i, c in enumerate(C):
    if c < 0:
        ans += 1
        p += c
    else:
        q += c
        l.append(c)

if p + q < 0:
    print(-1)
    exit()

l.sort()


while p < 0:
    p += l[-1]
    l.pop()
    ans += 1

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a) < sum(b):
    print(-1)
else:
    cnt = ssum = 0
    d = [a[i]-b[i] for i in range(n)]
    for i in range(n):
        if d[i] < 0:
            cnt += 1
            ssum -= d[i]
    d.sort()
    for i in range(n-1, -1, -1):
        if ssum <= 0:
            break
        ssum -= d[i]
        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)
    exit()
data=[]
need=0
ans=0
for i in range(N):
    if A[i]>B[i]:
        data.append(A[i]-B[i])
    elif A[i]<B[i]:
        need+=B[i]-A[i]
        ans+=1
data.sort()
while need>0:
    ans+=1
    need-=data.pop()
print(ans)

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:
    a = []
    b = []
    for x, y in zip(A, B):
        if x >= y:
            a.append(x-y)
        else:
            b.append(y-x)
    a.sort(reverse=True)
    c = sum(b)
    ans = len(b)
    ind = 0
    while c > 0:
        c = c-a[ind]
        ind += 1
        ans += 1
    print(ans)

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

diff = []
neg = 0
count = 0

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

for a, b in zip(A, B):
    diff += [a-b]
    if a < b:
        count += 1
        neg += a - b

for d in sorted(diff, reverse=True):
    if neg >= 0:
        break
    count += 1
    neg += d

print(count)

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

sum_A = sum(A)
sum_B = sum(B)

diff = []
neg = 0
count = 0

if sum_B > sum_A:
    print(-1)
    exit()

for a, b in zip(A, B):
    diff += [a-b]
    if a < b:
        count += 1
        neg += a - b

for d in sorted(diff, reverse=True):
    if neg >= 0:
        break
    count += 1
    neg += d

print(count)

Solution (Python):

N = int(input())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]


counter = 0
overed = 0
affs = []
i = 0

for a,b in zip(a_list,b_list):
    if b>a:
        overed += b-a
        counter += 1
    elif b<a:
        affs.append(a-b)
affs = sorted(affs, reverse = True)

while overed>0:
    if i == len(affs):
        print(-1)
        exit()
    overed -= affs[i]
    counter += 1
    i += 1
print(counter)

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

D=[]
Asum=0
Bsum=0
Dnegative=0
Dnegative_n=0
Dsum=0
for i in range(N):
  D.append(A[i]-B[i])
  Asum += A[i]
  Bsum += B[i]
  if A[i]-B[i]<0:
    Dnegative += A[i]-B[i]
    Dnegative_n += 1
if Asum < Bsum:
  print(-1)
  exit()

if Dnegative_n==0:
  print(0)
  exit()
D.sort()

for i in range(N):
  Dsum -= D[N-i-1]
  if Dsum <= Dnegative:
    print(Dnegative_n+i+1)
    exit()

Solution (Python):

n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
diff = [0]*n

neg = 0
cnt = 0
for i in range(n):
    x = a[i]-b[i]
    diff[i] = x
    if x < 0:
        cnt += 1
        neg += x

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

for x in sorted(diff, reverse=True):
    if neg >= 0:
        break
    cnt += 1
    neg += x

print(cnt)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ov = []
un = 0
cnt = 0
for i in range(n):
  c = a[i]-b[i]
  if c >= 0:
    ov.append(c)
  else:
    un += c
    cnt += 1
if un == 0:
  print(0)
else:
  un *= -1
  ov.sort(reverse=True)
  ov2 = 0
  for i in range(len(ov)):
    ov2 += ov[i]
    if ov2 >= un:
      print(cnt+i+1)
      break
  else:
    print(-1)

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

mns = []
res = 0
plus = 0
for i in range(n):
    if a[i] < b[i]:
        res += 1
        plus += b[i]-a[i]
    elif a[i] > b[i]:
        mns.append(b[i]-a[i])
mns.sort()
cnt = 0
while plus > 0:
    plus += mns[cnt]
    res += 1
    cnt += 1
    if plus > 0 and cnt == len(mns):
        print(-1)
        exit()
print(res)

Solution (Python):
def redistribution(dif,minus,plus):
    while dif[minus] < 0:
        if dif[minus] + dif[plus] >= 0: 
            dif[plus] = dif[plus] + dif[minus]
            dif[minus] = 0
        else: 
            dif[minus] = dif[minus] + dif[plus]
            dif[plus] = 0
            plus -= 1
    minus += 1
    return [minus,plus]

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
check = True
for i in range(n):
    if a[i] < b[i]:
        check = False
if sum(b) > sum(a):
    print("-1")
elif check:
    print("0")
else:
    dif = []
    for i in range(n):
        dif.append(a[i]-b[i])
    dif.sort()
    minus = 0
    plus = -1
    while dif[minus] < 0:
        temp = redistribution(dif,minus,plus)
        minus = temp[0]
        plus = temp[1]
    print(abs(plus)+minus)