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):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
tarinai = 0
amari = 0

D = [0 for i in range(N)]
for i in range(N):
    D[i] = A[i] - B[i]
    if D[i] < 0:
        tarinai += -1 * D[i]
    elif D[i] > 0:
        amari += D[i]
if amari < tarinai:
    print(-1)
else:
    D = sorted(D)
    left = 0
    right = N - 1
    count = 0
    while True:
        if D[left] >= 0:
            break
        if D[right] == -1 * D[left]:
            left += 1
            right -= 1
            count += 2
        elif D[right] > - 1 * D[left]:
            D[right] -= -1 * D[left]
            left += 1
            count += 1
            if D[left] >= 0:
                count += 1
        else:
            D[left] += D[right]
            right -= 1
            count += 1
    print(count)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
win=[]
lose=[]
if sum(a)<sum(b):
  print(-1)
else:
  for i in range(n):
    if a[i]<b[i]:
      lose.append(b[i]-a[i])
    else:
      win.append(a[i]-b[i])
  if len(lose)==0:
    print(0)
  else:
    win.sort()
    win.reverse()
    x=sum(lose)
    ct=0
    for j in range(len(win)):
      ct+=win[j]
      if ct>=x:
        break
    print(j+len(lose)+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(0)

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

has_shortage = False

for i in range(N):
    if C[i] < 0:
        has_shortage = True
        break

if has_shortage is False:
    print(0)
    exit(0)

C = sorted(C, reverse=True)
i = 0
j = N-1
ans = 2
while C[j] < 0:
    if -C[j] > C[i]:
        C[j] += C[i]
        C[i] = 0
        i += 1
        ans += 1
        continue
    else:
        C[i] += C[j]
        
        j -= 1
        ans += 1
        continue

ans -= 1
print(ans)

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

short_sum = 0
exceed_diffs = []
count = 0

for i in range(0, N):
    diff = A[i] - B[i]
    if diff < 0:
        short_sum += -diff
        count += 1
    elif diff > 0:
        exceed_diffs.append(diff)

exceed_diffs.sort(reverse = True)

for diff in exceed_diffs:
    if short_sum <= 0:
        break

    short_sum -= diff
    count += 1

if short_sum > 0:
    print(-1)
else:
    print(count)

Solution (Python):
n = int(input())
Ascore = list(map(int,input().split()))
Bscore = list(map(int,input().split()))
count = 0
positive = []
nsum = 0
if sum(Ascore) < sum(Bscore):
    print(-1)
else:
    for a,b in zip(Ascore,Bscore):
        tmp = a-b
        if tmp < 0:
            count += 1
            nsum += tmp
        else:
            positive.append(tmp)
    positive = sorted(positive)[::-1]
    while(nsum<0):
        p = positive.pop(0)
        count += 1
        nsum += p
    print(count)

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

nn = []
n = []
for a, b in zip(A, B):
    d = a - b
    if d == 0:
        continue
    if d > 0:
        nn.append(d)
    else:
        n.append(-d)

ans = len(n)

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

s = sum(n)

if s > sum(nn):
    print(-1)
    exit()

t = 0
for v in sorted(nn, reverse=True):
    t += v
    ans += 1
    if t >= s:
        break
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(0,N)]
C.sort()
p = 0
ans = 0
j = len(A) - 1
for i in range(0,N):
  if C[i] < 0:
    p += C[i]
    ans += 1
  else:
    break
  if p < 0:
    if C[j] > 0:
      p += C[j]
      ans += 1
      C[j] = 0
      j -= 1

if sum(A) < sum(B):
  print(-1)
else:
  print(ans)

Solution (Python):
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b
import bisect

n = getN()
pres = getlist()
needs = getlist()

mores = []
for i in range(len(pres)):
    mores.append(pres[i] - needs[i])

ans = 0
mores.sort(reverse=False)

div = bisect.bisect_left(mores, 0)

ans += div
husoku = -(sum(mores[:div]))

amari = mores[div:]
if type(amari) == int:
    amari = [amari]
amari.sort(reverse=True)

if sum(amari) < husoku:
    ans = -1
else:
    if husoku > 0:
        for a in amari:
            ans += 1
            if a >= husoku:
                break
            else:
                husoku -= a

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:
    D=[]
    d=0
    a=0
    for i in range(len(A)):
        c=A[i]-B[i]
        D.append(c)
        if c<0:
            a+=1
            d-=c
    D.sort()
    j=1
    while d>0:
        if d>D[-j]:
            a+=1
            d-=D[-j]
            j+=1
        else:
            a+=1
            break
    print(a)

Solution (Python):

N = int(input())
An = list(map(int, input().split()))
Bn = list(map(int, input().split()))

if sum(An) < sum(Bn):
    print(-1)
    exit()

fusoku = [b-a for a,b in zip(An,Bn) if a < b ]
yojo = [a-b for a, b in zip(An, Bn) if a > b]
yojo.sort(reverse=True)
sum_fusoku = sum(fusoku)
ans = len(fusoku)
sum_score = 0
for y in yojo:
    if sum_score >= sum_fusoku:
        break
    ans += 1
    sum_score += y


print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
suma,sumb=0,0
l=[0 for _ in range(n)]
for i in range(n):
    suma+=a[i]
    sumb+=b[i]
    l[i]=b[i]-a[i]
if sumb>suma:
    print(-1)
    exit()
l.sort()
cnt=0
ans=0
for i in range(n):
    if l[i]>0:
        cnt+=l[i]
        ans+=1
for i in l:
    if cnt>0:
        cnt+=i
        ans+=1
print(ans)

Solution (Python):


import sys


INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()
A = LI()
B = LI()
if sum(A) < sum(B):
    print(-1)
    exit()

cnt = 0
need = 0
mochi = []
for i in range(N):
    a = A[i]
    b = B[i]
    if a < b:
        cnt += 1
        need += b - a
    else:
        mochi.append(a - b)

mochi.sort()



while need > 0:
    p = mochi.pop()
    need -= p
    cnt += 1

print(cnt)

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

ans=0
task=0
power=[]

for i in range(N):
  if A[i]< B[i]:
    ans+=1
    task+=(B[i]-A[i])
  else:
    power.append(A[i]-B[i])

power.sort(reverse=True)
for i in range(len(power)):
  if task<=0:
    break
  else:
    task-=power[i]
    ans+=1

if task>0:
  print(-1)
else:
  print(ans)

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

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

t = 0
m = 0
L = []
for k in range(N):
    if A[k] > B[k]:
        L.append(A[k]-B[k])
    else:
        m += B[k] - A[k]
        t += 1

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

L = (sorted(L))[::-1]
for e in L:
    m -= e
    t += 1
    if m <= 0:
        break
print(t)

Solution (Python):

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

given = []
giving = []
for Ai, Bi in zip(A, B):
    if Ai < Bi: given.append(Bi - Ai)
    else: giving.append(Ai - Bi)

cost = sum(given)
if cost < 1:
    print(0)
else:
    ans = len(given)
    giving.sort(key=lambda x: -x)
    for Xi in giving:
        
        ans += 1
        cost -= Xi
        if cost <= 0:
            print(ans)
            break
        else: continue
    else:
        print(-1)

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


short=0
arr=[]
count=0
for i,j in zip(A,B):
    if i<j:
        short+=j-i
        count+=1
    else:
        arr.append(i-j)

arr.sort()

if short==0:
    print(0)
else:
    for i in arr[::-1]:
        short-=i
        count+=1
        if short<=0:
            break
    if short>0:
        print(-1)
    else:
        print(count)

Solution (Python):

"""
Created on Sun Jan 13 21:11:51 2019

@author: Yamazaki Kenichi
"""

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

d = [0 for i in range(N)]

a = 0
b = 0
C = [0 for i in range(N)]
ans = 0

for i in range(N):
    if A[i] < B[i]:
        d[i] = 1
        a += A[i]
        b += B[i]
    else:
        C[i] += A[i]-B[i]

C.sort()
c =b-a 

if sum(C) < b-a:
    ans = -1
elif d.count(1) == 0:
    ans =0
else:
    ans = d.count(1)
    i=1
    while c>=0:
       c -= C[-i]
       ans+=1
       i +=1
    
print(ans)

Solution (Python):
def inpl(): return list(map(int, input().split()))
N = int(input())
A = inpl()
B = inpl()
ans = 0

if sum(A) <= sum(B):
    print(-1)
else:
    NG = 0
    OK = []
    for i in range(N):
        if A[i] - B[i] > 0:
            OK.append(A[i] - B[i])
        elif A[i] - B[i] < 0:
            NG += A[i] - B[i]
            ans += 1
    OK = sorted(OK)
    while NG < 0:
        ans += 1
        NG += OK.pop()
    print(ans)

Solution (Python):

n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

cl = [0]*n
solved = True
for i in range(n):
    cl[i] = al[i]-bl[i]
    if cl[i]<0:
        solved = False

if solved:
    print(0)
    exit()

cl.sort(reverse=True)


last = n-1
i = 0
res = 0
used = [False for _ in range(n)]
while True:
    if cl[last]>0:
        break

    while cl[i]>0 and cl[last]<0:
        if cl[i]+cl[last]>=0:
            cl[i] += cl[last]
            cl[last] = 0
            if not used[last]:
                res += 1
            used[last] = True
            last -= 1
        else:
            cl[last] += cl[i]
            cl[i] = 0
        if not used[i]:
            res += 1
        used[i] = True
    i += 1
    if i==last:
        break

if cl[last]<0:
    res = -1
print(res)

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

    diffs = []
    rest = 0
    ans = 0
    for i in range(n):
        if a[i] < b[i]:
            rest += b[i] - a[i]
            ans += 1
        if a[i] > b[i]:
            diffs.append(a[i] - b[i])
            
    diffs.sort()
    while rest > 0:
        diff = diffs.pop()
        rest -= diff
        ans += 1

    return ans


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

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

isho = 0
tarinai = []
amari = []

for i in range(N):
    if A[i] == B[i]:
        isho += 1
    elif A[i] - B[i] > 0 :
        amari.append(A[i]-B[i])
    else:
        tarinai.append(B[i] - A[i])

if len(tarinai) == 0:
    print(0)
    exit()
elif len(amari) == 0:
    print(-1)
    exit()

amari = sorted(amari,reverse=True)

ind = 0
tmp = 0

sum_tari = sum(tarinai)

while sum_tari > tmp:
    tmp += amari[ind]
    ind += 1

ans = ind + len(tarinai)
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()

diff = [A[i] - B[i] for i in range(N)]
diff = sorted(diff)
j = 0
k = N - 1
num = 0
tmp = 0
while diff[j] < 0:
    tmp += diff[j]
    j += 1
    num += 1
    while tmp < 0:
        tmp += diff[k]
        k -= 1
        num += 1

print(num)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
import re
sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B):
    if sum(A) < sum(B):
        return -1

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

    P.sort()
    M.sort()

    c = 0
    ans = 0
    for m in M:
        while -m > c:
            c += P.pop()
            ans += 1
        c += m
        ans += 1

    return ans


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    print(slv(N, A, B))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = []
d = 0
dc = 0
for I in range(n):
 x = a[I] - b[I]
 if x >= 0:
  c.append(x)
 else:
  d -= x
  dc += 1  
c.sort(reverse = True)
m = len(c)
k = 0
while d >0 and k < m :
 d -= c[k]
 k += 1
if d >0:
 print(-1)
else:
 print(dc + k)

Solution (Python):
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
a=i2()
b=i2()
c=[]
e=0
for i in range(n):
 c.append([a[i]-b[i],i])
c.sort()
d=[c[n-1][0],n-1]
if c[0][0]<0:
   e+=1
for i in range(n):
 if c[i][0]<0:
    d[0]+=c[i][0]
    e+=1
    if d[0]<0:
       d[1]-=1
       d[0]+=c[d[1]][0]
       e+=1
 else:
    break
if (sum(a)-sum(b))<0:
   print(-1)
else:
   print(e)

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)]
diff.sort()
r = sum(A) - sum(B)
ans = N
for i in range(N):
    if diff[i] < 0:
        continue
    if r < 1:
        break
    if diff[i] <= r:
        ans -= 1
        r -= diff[i]

print(ans)

Solution (Python):
def c_exam_and_wizard(N, A, B):
    ans = 0
    short_val = 0  
    can_give = []
    for a, b in zip(A, B):
        if a < b:
            ans += 1
            short_val += b - a
        elif a > b:
            can_give.append(a - b)

    if short_val == 0:
        return 0
    can_give.sort()

    idx = len(can_give) - 1
    while True:
        if not can_give:
            ans = -1
            break

        if can_give[idx] >= short_val:
            ans += 1
            break

        short_val -= can_give[idx]
        can_give.pop()
        ans += 1
        idx -= 1
    return ans

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print(c_exam_and_wizard(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()

T = [x-y for x,y in zip(A,B)]
M = [t for t in T if t < 0]
P = [t for t in T if t >= 0]

ans = 0
ans += len(M)
b = sum(M)
P.sort()
P=P[::-1]
for p in P:
	if b >= 0:
		break
	b+=p
	ans += 1



print(ans)

Solution (Python):

import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    input_rawA = read_func().strip().split()
    input_rawB = read_func().strip().split()
    A = []
    B = []
    sub_AB = []
    sumA = 0
    sumB = 0
    count = 0
    sum_fusoku = 0
    for i in range(N):
        A.append(long(input_rawA[i]))
        B.append(long(input_rawB[i]))
        sub_AB.append((max(A[i] - B[i],0), i))
        sumA += A[i]
        sumB += B[i]
        if A[i] < B[i]:
            count += 1
            sum_fusoku += B[i] - A[i]


    if sumA < sumB:
        print -1
        return

    sub_AB.sort(reverse = True)

    for i in range(len(sub_AB)):
        if sum_fusoku > 0:
            sum_fusoku -= sub_AB[i][0]
            count += 1
        else:
            break
    print count



if __name__ == '__main__':
    main()

Solution (Python):
import bisect
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i,j in zip(a,b):
  c.append(i-j)
c.sort()
if min(c) >= 0:
  print(0)
  exit()
else:
  zero = bisect.bisect_left(c,0)
  minus = sum(c[:zero])
  
  d = c[zero:]
  ans = zero
  for i in d[::-1]:
    minus += i
    ans += 1
    if minus >= 0:
      print(ans)
      exit()
print(-1)

Solution (Python):
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

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

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

T.sort()
r = N-1
l = 0

flag = True
cnt = 0

if T[l] >= 0:
    print(0)
else:
    while T[l] < 0:
        if l >= r:
            flag = False
            break
        elif T[r] >= -T[l]:
            T[r] += T[l]
            l += 1
        else:
            T[l] += T[r]
            r -= 1
                


    ans = l + (N - r)
    if flag:
        print(ans)
    else:
        print(-1)

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

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

if sumA < sum(B):
    print('-1')
    exit()
ans = 0
totalMinus = 0
for a, b in zip(A, B):
    if a-b > 0:
        plusSubject.append(a-b)
    elif a-b < 0:
        totalMinus += a-b
        ans += 1

plusSubject = sorted(plusSubject, reverse=True)

for plus in plusSubject:
    if totalMinus >= 0:
        break
    totalMinus += plus
    ans += 1
print(ans)

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 sum(A) < sum(B):
        print(-1)
        return

    
    few = A < B
    few_cnt = few.sum()
    few_sum = (B - A)[few].sum()
    if few_cnt == 0:
        print(0)
        return

    over = (A >= B)
    overs = (A - B)[over]
    overs.sort()
    overs_cumsum = overs[::-1].cumsum()
    for i, v in enumerate(overs_cumsum):
        if v >= few_sum:
            print(few_cnt + i + 1)
            return


if __name__ == "__main__":
    solve()

Solution (Python):
def solve():
    _ = input()
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    d = [x-y for x, y in zip(a, b)]

    

    if sum(d) < 0:
        return -1

    neg = [d for d in d if d < 0]
    pos = sorted([x for x in d if x > 0], reverse=True)

    ans = len(neg)

    i = 0
    n = sum(neg)
    while n< 0:
        n+= pos[i]
        i+=1
    ans += i
    return ans


print(solve())

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
D = []
luck = []
raku = []
for i in range(N):
    d = A[i]-B[i]
    if d < 0:
        luck.append(d)
    else:
        raku.append(d)
    D.append(d)
Luck = sum(luck)
raku.sort(reverse=True)
R = len(raku)
L = len(luck)
flag = 0
for j in range(R):
    if Luck >= 0:
        print(0)
        flag = 1
        break
    Luck+=raku[j]
    if Luck >= 0:
        print(j+1+L)
        flag = 1
        break
    else:
        pass
if flag == 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):
    counter = 0
    array = []
    num = 0
    for i in range(N):
        if A[i] >= B[i]:
            array.append(A[i] - B[i])

        else:
            counter += 1
            num += (B[i] - A[i])

    array.sort()
    while num > 0:
        counter += 1
        num -= array[-1]
        del array[-1]

    print(counter)
    
else:
    print(-1)

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):
    ans = -1
else:
    ans = 0
    
    d = [a[i]-b[i] for i in range(n)]
    plus = []
    minus = []
    for i in range(n):
        if d[i] > 0:
            plus.append(d[i])
        elif d[i] < 0:
            minus.append(d[i])

    sum_minus = -sum(minus)
    ans = len(minus)
    plus.sort(reverse=True)
    while sum_minus > 0:
        num_plus = plus.pop(0)
        sum_minus -= num_plus
        ans += 1
print(ans)

Solution (Python):



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

pos = [a[i] - b[i] for i in range(n) if a[i] - b[i] >= 0]
neg = [a[i] - b[i] for i in range(n) if a[i] - b[i] < 0]
pos.sort()

pval = sum(pos)
nval = sum(neg)



if pval + nval < 0:
    print(-1)
else:
    ans = 0
    while(nval < 0):
        nval += pos.pop()
    
    print(n-len(pos))

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

d_pls = []
d_mns = []
s = 0
for i in range(N):
    t = a[i] - b[i]
    s += t
    if t > 0:
        d_pls.append(t)
    elif t < 0:
        d_mns.append(t)


if s < 0:
    print(-1)
    exit()

d_pls = sorted(d_pls, reverse=True)

d_mns = sorted(d_mns)
m_sum = sum(d_mns)
ans = len(d_mns)

i = 0
while m_sum < 0:
    m_sum += d_pls[i]
    ans += 1
    i += 1
print(ans)

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])
C.sort()
C.reverse()
ans = 0
count = 0
for i in range(N):
    if A[i] < B[i]:
        ans += 1
        count += B[i] - A[i]
for i in range(N):
    if count > 0:
        count -= C[i]
        ans += 1
    else:
        break
        
if sum(A) < sum(B):
    ans = -1
print(ans)

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

A_origin = A[:]

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

diff = [[i, a - b] for i, a, b in zip(range(N), A, B)]

order = sorted([[i, x] for i, x in diff if x > 0], key=lambda d: d[1])
C = []

charge = order.pop()
for i in range(N):
    while A[i] - B[i] < 0:
        if charge[1] > B[i] - A[i]:
            A[charge[0]] -= (B[i] - A[i])
            charge[1] -= (B[i] - A[i])
            A[i] = B[i]
        else:
            A[charge[0]] = B[charge[0]]
            A[i] += charge[1]
            charge = order.pop()

ans = 0
for a, ao in zip(A, A_origin):
    if a != ao:
        ans += 1

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
D = [A[i]-B[i] for i in range(N)]
D.sort()
s = 0
for i in range(N):
    if D[i] >= 0:
        break
    s -= D[i]
if s == 0:
    print(0)
else:
    D = [x for x in D if x>0]
    D.sort()
    D.reverse()
    pos = False
    for j in range(len(D)):
        if s-D[j] <= 0:
            pos = True
            break
        s -= D[j]
    if pos:
        print(i+j+1)
    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()
c = []
for i in range(n):
    c.append(a[i]-b[i])
c.sort()

i = 0
j = n-1
ans = 0
while c[i] < 0:
    if i == 0:
        ans += 1
    if c[j] + c[i] > 0:
        c[j] += c[i]
        i += 1
        ans += 1
    else:
        c[i] += c[j]
        ans += 1
        j -= 1

print(ans)

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
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 itertools import accumulate
from bisect import bisect_left

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

if sum(a) < sum(b):
    print(-1)
    
else:
    over = []
    lack = 0
    ans = 0
    for ai, bi in zip(a,b):
        if ai < bi:
            ans += 1
            lack += (bi-ai)
        else:
            over.append(ai-bi)
            
    over.sort(reverse=True)
    over = [0] + over
    over = list(accumulate(over))
    cnt = bisect_left(over, lack)
    
    ans += cnt
    print(ans)

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

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

C = [0 for i in range(N)]

ans = 0
al_C = 0

for i in range(N):
    C[i] = A[i]-B[i]
    if C[i]<0:
        ans = ans+1
        al_C = al_C-C[i]
    
C.sort()
C.reverse()

if ans==0:
    print(0)
else:
    f = 1
    
    for i in range(N):
        if C[i]<=0:
            print(-1)
            break
        else:
            al_C = al_C-C[i]
            ans = ans +1
        
        if al_C<=0:
            print(ans)
            break
    else:
        print(-1)

Solution (Python):
import sys

N = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
dist = [B[i] - A[i] for i in range(N)]
distp = [[i, B[i] - A[i]] for i in range(N)]
if sum(dist) > 0:
    print(-1)
    sys.exit()
distp.sort(key=lambda x: x[1], reverse=True)
C = [v for v in A]
ans = 0
for i, dist in distp:
    if dist <= 0:
        break
    while dist > 0:
        v = min(dist, -distp[-1][1])
        dist -= v
        distp[-1][1] += v
        if distp[-1][1] == 0:
            distp.pop()
            ans += 1
    ans += 1
if len(distp):
    i, v = distp[-1]
    if B[i] - A[i] != v:
        ans += 1
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
over = []
under = 0
uc = 0

if sum(A) < sum(B):
  print(-1)
else:
  for a, b in zip(A, B):
    if a > b:
      over.append(a-b)
    elif a < b:
      uc += 1
      under += b-a
  for o in sorted(over, key=lambda x: -x):
    if under <= 0:
      break
    else:
      under -= o
      uc += 1
  print(uc)

Solution (Python):
import sys

N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

if sum(As) < sum(Bs):
    print(-1)
    sys.exit()

ans = 0
sumShortage = 0
Surpluss = []
for A, B in zip(As, Bs):
    if A < B:
        ans += 1
        sumShortage += B - A
    else:
        Surpluss.append(A - B)

Surpluss.sort(reverse=True)

for Surplus in Surpluss:
    if sumShortage <= 0:
        break
    sumShortage -= Surplus
    ans += 1

print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = 0
ans = True
if sum(A) < sum(B):
    res = -1
    ans = False
    print(res)
if ans:
    N = [A[i]-B[i] for i in range(n) if A[i] < B[i]]
    P = [A[i]-B[i] for i in range(n) if A[i] >= B[i]]
    res = len(N)
    P.sort(reverse=True)
    dif = abs(sum(N))
    i = 0
    while dif>0:
        dif -= P[i] 
        res += 1
        i += 1
    print(res)

Solution (Python):
import sys
def inpl(): return [int(i) for i in input().split()]
N = int(input())
A = inpl()
B = inpl()
if sum(A) < sum(B):
    print(-1)
    sys.exit()
sB = [j for i, j in zip(A, B) if i < j]
sA = [i for i, j in zip(A, B) if i < j]
ans = len(sA) 
k = sum(sB) - sum(sA)
C = sorted([i-j for i, j in zip(A,B) if i >= j])
while k > 0:
    c = C.pop()
    k -= c
    ans += 1
print(ans)

Solution (Python):
import sys

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

enough=[]
lack=[]
for i in range(n):
    if a[i]-b[i]<0:
        lack.append(a[i]-b[i])
    else:
        enough.append(a[i]-b[i])

change=(-1)*sum(lack)
enough.sort()
enough=enough[::-1]

i=0
while change>0:
    if i>=len(enough):
        print(-1)
        sys.exit()
    change-=enough[i]
    i+=1

print(i+len(lack))

Solution (Python):
n = int(input())
a = list(int(i) for i in input().split())
b = list(int(i) for i in input().split())
d = []
for i in range(n):
    d.append(a[i]-b[i])
d = sorted(d)
if sum(d)<0:
    print(-1)
else:
    dif = 0
    count = 0
    for i in range(n):
        if d[i]<0:
            dif -= d[i]
            count += 1
        else:
            break
    if dif>0:
        for i in range(n):
            dif -= d[n-1-i]
            count += 1
            if dif <=0:
                break
    print(count)

Solution (Python):
from operator import itemgetter

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a) < sum(b):
    print(-1)
else:
    ab = []
    husoku = 0
    ans = 0
    for i in range(n):
        sa = a[i] - b[i]
        ab.append((a[i], b[i], sa))
        if sa < 0:
            husoku += sa
            ans += 1
    ab = sorted(ab, key=itemgetter(2), reverse=True)
    count = 0
    i = 0
    while i < n and count < -husoku:
        count += ab[i][2]
        i += 1
    print(i + ans)