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



Sample input:
3
2 3 5
3 4 1


Sample output:
3

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
# print(A,B)
dif = [ 0 for i in range(N)]
# print(dif)

if sum(A) < sum(B):
    print(-1)
else :
    for i in range(N):
        dif[i] = A[i] - B[i]
    if all( 0<=n for n in dif): 
        print(0)
    else:
        dif.sort(reverse=True)
        # print(dif)
        sum_minus = 0
        ans = 0 
        for d in dif:
            if d < 0:
                ans += 1
                sum_minus += d
        for d in dif:
            sum_minus += d
            ans += 1
            if sum_minus >= 0:
                print(ans)
                break

Submission status: Accepted

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

if sum(a) < sum(b):
    print(-1)
else:
    cnt = 0
    total = 0
    c = []
    for i in range(n):
        if a[i] < b[i]:
            cnt += 1
            total += (b[i]-a[i])
        else:
            c.append(a[i]-b[i])
    
    if cnt == 0:
        print(0)
    else:
        c.sort(reverse=True)
        d = 0
        for i in range(len(c)):
            d += c[i]
            if d >= total:
                print(cnt+i+1)
                break

Submission status: Accepted

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

    if sum_of_shortage_for_passing_mark == 0:
        return 0  # 準備度を変更するまでもない

    campensation_for_shortage.sort(reverse=True)  # A_i-B_iが大きなiから分けていく
    for pts in campensation_for_shortage:
        sum_of_shortage_for_passing_mark -= pts
        ans += 1
        if sum_of_shortage_for_passing_mark < 0:
            break
    else:
        ans = -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))

Submission status: Accepted

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

C=[a-b for a,b in zip(A,B)]

plus=[i for i in C if i>=0]
minus=[i for i in C if i<0]
plus.sort(reverse=True)

sum_minus=sum(minus)
ans=len(minus)

for i in plus:
    if sum_minus>=0:
        break
    sum_minus+=i
    ans+=1

if sum_minus<0:
    ans=-1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
count=0
if sum(A)<sum(B):
    print(-1)
else:
    dif=[A[i]-B[i] for i in range(N)]
    if min(dif)>=0:
        print(0)
    else:
        difsort=sorted(dif)
        plist=[i for i in difsort if i>=0][::-1]
        mlist=[i for i in difsort if i<0]
        mnum=len(mlist)
        msum=sum(mlist)
        for i in range(len(plist)):
            if msum>=0:
                break
            msum+=plist[i]
            count+=1
            
        
        print(count+mnum)

Submission status: Accepted

Solution code:
import operator

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

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

print(solve(A, B))

Submission status: Accepted

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

    if sum_of_shortage_for_passing_mark == 0:
        return 0  # 準備度を変更するまでもない

    campensation_for_shortage.sort(reverse=True)  # A_i-B_iが大きなiから分けていく
    for pts in campensation_for_shortage:
        if pts >= sum_of_shortage_for_passing_mark:
            ans += 1
            break

        sum_of_shortage_for_passing_mark -= pts
        ans += 1
    else:
        ans = -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))

Submission status: Accepted

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

d = list(aa - bb for aa, bb in zip(a, b))
d = sorted(d)

p = tuple(filter(lambda x: x >= 0, d))
p = sorted(p, reverse=True)
m = tuple(filter(lambda x: x < 0, d))

s = sum(m)

i = 0
while s < 0 and i < len(p):
    s += p[i]
    i += 1

print(len(m) + i if s >= 0 else -1)

Submission status: Accepted

Solution code:
def plus(x, y):
    return x+y
def minus(x, y):
    return x-y

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

can = True
if sum(a) < sum(b):
    can = False
    
dif = list(map(minus, a, b))
dif.sort()

count = 0
shortage = list((filter(lambda x: x<0, dif)))
sumofshortage = sum(shortage)
count += len(shortage)

sumofsurplus = 0
for i in dif[::-1]:
    if sumofsurplus+sumofshortage < 0:
        sumofsurplus += i
        count += 1
    else:
        break

if can:
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int,input().split()))
c = list(map(int,input().split()))
d = []
su  = 0
kk = 0
for i in range(a):
	d.append(b[i]-c[i])
	if b[i]-c[i] < 0:
		su += b[i]-c[i]
		kk += 1

d.sort()
d.reverse()
k = 0

j = 1

for i in range(a):
	if d[i] > 0:
		su += d[i]
		while kk:
			if d[-j] < 0:
				if abs(d[-j]) <= d[i]:
					d[i] +=  d[-j]
					d[-j] = 0
					j += 1
					k += 1
					kk -= 1
					if d[i] == 0 or d[-j] > 0:
						k += 1
						break
					if i == a - j:
						k += 1
						break
				else:
					d[-j] += d[i]
					d[i] = 0
					k += 1
					break
			else:
				break
	if su >= 0:
		break

if su < 0:
	print(-1)
else:
	print(k)

Submission status: Accepted

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

sumBA = 0
sumAB = []
cntBA = 0

for i in range(n):
    if b[i] > a[i]:
        sumBA += b[i] - a[i]
        cntBA += 1
    elif b[i] < a[i]:
        sumAB.append(a[i] - b[i])

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

if sumBA > sum(sumAB):
    print(-1)
    exit()

sumAB.sort(reverse = True)
count = 0
check = 0
for i in range(len(sumAB)):
    count += 1
    check += sumAB[i]
    if check >= sumBA:
        print(count + cntBA)
        exit()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Cm = []
Cp = []
for i in range(N):
	if(A[i]-B[i]<0):
		Cm.append(A[i]-B[i])
	elif(A[i]-B[i]>0):
		Cp.append(A[i]-B[i])
Cm.sort()
Cp.sort()


if(len(Cm) == 0):
	print(0)
	exit(0)
if(sum(Cp)<abs(sum(Cm))):
	print(-1)
	exit(0)

j=0
i = 0
nowCp = int(Cp[len(Cp)-i-1])


while(j<len(Cm)):
	if(nowCp>=abs(Cm[j])):
		nowCp -= abs(Cm[j])
		j += 1
	elif(nowCp<abs(Cm[j]) and i<len(Cp)):
		i += 1
		nowCp += int(Cp[len(Cp)-i-1])

	else:
		break
	

if(j == len(Cm)):
	print(j+i+1)
else:
	print(-1)

Submission status: Accepted

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

if sum(A)<sum(B):
    print(-1)
else:
    C = [B[i]-A[i] for i in range(N)]
    C.sort()
    ans=0
    task=0
    i=-1
    while C[i]>0:
        ans+=1
        task+=C[i]
        i-=1

    i=0
    while task>0:
        ans+=1
        task+=C[i]
        i+=1
    print(ans)

Submission status: Accepted

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

mi = []
pl = []

for i in range(N):
    s = A[i] - B[i]
    if s < 0:
        mi.append(s)
    elif s > 0:
        pl.append(s)

            
pl.sort(reverse=True)
p = np.sum(mi)



if (len(pl)>0)and(len(mi)>0):
    count = 0
    for i in pl:
        if p + i < 0:
            p += i
            count += 1
            continue
        else:
            count += 1
            break
    else:
        print('-1')
        exit()

    print(count + len(mi))
    
        
elif len(mi) == 0:
    print('0')
    exit()
else:
    print('-1')
    exit()

Submission status: Accepted

Solution code:
n = int(input())
score = list(map(int, input().split()))
target = list(map(int, input().split()))

sub = [score[i] - target[i] for i in range(n)]
ans = 0

if sum(score) < sum(target): ans = -1
else:
    minus = []
    plus = []
    for i in range(n):
        if(sub[i] < 0): minus.append(-sub[i])
        elif(sub[i] > 0): plus.append(sub[i])
    plus = sorted(plus, reverse = True)
    sum_minus = sum(minus)

    ans += len(minus)
    while sum_minus > 0:
        if sum_minus <= plus[0]:
            plus[0] = 0
            sum_minus = 0
        else:
            sum_minus -= plus.pop(0)
        ans += 1

print(ans)

Submission status: Accepted

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

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

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

  for i in l[::-1]:
    if t+i>=0 and i<0:
      t=t+i
      ans+=1
    elif t:
      t=0
      ans+=1
    else:
      break

  print(ans)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    if sum(A) < sum(B):
        return -1
    AB = [a - b for a, b in zip(A, B)]
    plus = [x for x in AB if x > 0]
    minus = [x for x in AB if x < 0]
    t = sum(minus)
    cnt = 0
    for m in sorted(plus, reverse=True):
        if t >= 0:
            break
        t += m
        cnt += 1
    return cnt + len(minus)
    ans = 0
    return ans
print(solve())

Submission status: Accepted

Solution code:
N=input()
A=map(int,raw_input().split())
B=map(int,raw_input().split())
need=0
Ans=0

for a,b in zip(A,B):
    if b>a:
        need+=b-a
        Ans+=1
        
index=0

Gap=[a-b for a,b in zip(A,B)]
Gap.sort(reverse=True)

while need>0:
    if Ans+index>=N:
        break
    need-=Gap[index]
    index+=1
    
if need<=0:
    print Ans+index
else:
    print "-1"

Submission status: Accepted

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

L = [A[i] - B[i] for i in range(N)]
ans = 0

if sum(A) < sum(B) :
    ans = -1
else :
    debt = []
    deposit = []
    
    for i in range(N) :
        if L[i] < 0 :
            debt.append(-L[i])
        elif L[i] > 0 :
            deposit.append(L[i])
    
    deposit = sorted(deposit,reverse=True)
    debt_sum = sum(debt)
    
    ans += len(debt)
    
    while debt_sum > 0 :
        if debt_sum <= deposit[0] :
            deposit[0] = 0
            debt_sum = 0
        else :
            debt_sum -= deposit.pop(0)
        ans += 1
        
print(ans)
#print(debt)
#print(deposit)

Submission status: Accepted

Solution code:
#C
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
B = np.array(list(map(int, input().split())))

if sum(A) < sum(B):
    re = -1

else:
    diff = A - B
    diff.sort()

    i = 0
    j = -1
    a = diff[j]
    while diff[i] < 0:
        a += diff[i] 
        while a<0:
            j -= 1
            a+= diff[j]
        i += 1

    if i == 0:
        re = 0
    else:
        re = i - j

print(re)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

a_arr = np.array(a)
b_arr = np.array(b)
c_arr = a_arr - b_arr
c_arr.sort()

if sum(a_arr) < sum(b_arr) :
    x=-1
elif c_arr[0]>=0:
    x=0
else:
    minus_arr= c_arr[c_arr<0]
    m_=len(minus_arr)
    pls_arr= c_arr[c_arr>0]
    m=sum(minus_arr)
    pls=pls_arr.tolist()
    pls.sort()
    pls.reverse()
    i=int(0)
    pls_=int(0)
    while i <= -m :
        i = i + pls.pop(0)
        pls_=pls_+1
    x=m_+pls_

print(x)

Submission status: Accepted

Solution code:
n=int(input())
*a,=map(int,input().split())
*b,=map(int,input().split())
c,d=[],[]
for s,t in zip(a,b):
    if s<t:
        c.append(t-s)
    elif s>t:
        d.append(s-t)
c.sort()
d.sort(reverse=1)
i=0
j=0
f=0
while i<len(c) and j<len(d):
    if c[i]<=d[j]:
        d[j]-=c[i]
        i+=1
        f=1
    elif c[i]>d[j]:
        c[i]-=d[j]
        j+=1
if i<len(c):
    print(-1)
else:
    print(i+j+f)

Submission status: Accepted

Solution code:
import numpy as np

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

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

if e<0:
    print(-1)

else:

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

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

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

        if sumup>e:
            break

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
#c=[]
d=[]
e=[]
dif=0
nsum=0
psum=0
cnt=0


for i in range(n):
  dif=a[i]-b[i]
#  c.append(dif)
  if dif<0:
    d.append(dif)
    nsum+=dif
  elif dif>0:
    e.append(dif)
#c.sort()
#d.sort()
e.sort()
#print(nsum)
if abs(nsum)>sum(e):
  print(-1)
else:
  i=-1
  while abs(nsum)>psum:
    cnt+=1
    psum+=e[i]
    i-=1
  print(len(d)+cnt)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

if sum(a) < sum(b):
  print(-1)
else:
  c_p=[]
  c_m=[]
  for i in range(len(a)):
    if a[i] > b[i]:
      c_p.append(a[i]-b[i])
    elif a[i] <b[i]:
      c_m.append(a[i]-b[i])
  #print(c_p,c_m)
  sum_c_m = sum(c_m)
  result = len(c_m)
  c_p.sort(reverse=True)
  #print("c_p",c_p)
  index=0
  #print("sum_c_m", sum_c_m)
  while sum_c_m<0:
    sum_c_m += c_p[index]
    index+=1
  result += index
  print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""
#
# Author:   Noname
# URL:      https://github.com/pettan0818
# License:  MIT License
# Created: 日  1/13 22:32:34 2019

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

    return A, B


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


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


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

Submission status: Accepted

Solution code:
# 2019/01/13
# KEYENCE Programming Contest 2019 - C

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

# Init
sum = 0
minuscnt = 0
summinus = 0

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

c.sort(reverse=True)

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

# Output
print(ans)

Submission status: Accepted

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

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

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

        if m_sum>=0:
            ans = m_count + i + 1
print(ans)

Submission status: Accepted

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

if sum(B) > sum(A):
    print(-1)
else:
    diff = [a-b for a,b in zip(A,B)]
    plus = []
    minus_num = 0
    lack = 0
    for d in diff:
        if d >= 0:
            plus.append(d)
        elif d < 0:
            minus_num += 1
            lack += d
    plus.sort(reverse=True)
    i = 0
    while lack < 0:
        lack += plus[i]
        i += 1
        
    print(N-len(plus)+i)

Submission status: Accepted

Solution code:
N=int(input())
X=input()
A1=[]
A1=X.split(" ") #数値YをリストXに格納
A=[int(i) for i in A1] #リストXの要素を"文字"から"数値"に変換
Y=input()
B1=[]
B1=Y.split(" ") #数値YをリストXに格納
B=[int(i) for i in B1] #リストXの要素を"文字"から"数値"に変換
W=[]
Z=[]
for j in range(0,N):
    w=A[j]-B[j]
    W.append(w)
for n in range(0,N):
    z=A[n]-B[n]
    Z.append(z)
Z=sorted(Z,reverse=True) #リスト内の要素を大→小にソート
x2=0
x=0
if sum(B)>sum(A):
    print("-1")
else:
    x=0
    x1=0
    x2=0
    x3=0
    for i in range(0,N):#A[i]<B[i]となるリストの要素の個数を数える
        if A[i]<B[i]:
            x=x+1 #xは条件を満たすリストの個数
            x1=x1+W[i]#x1は"いくら足りないか"の積算


    for k in range(0,N): #埋め合わせにZの要素がいくつ必要か(=x3)求める
        if x1<0:
            x1=int(x1)
            p=int(Z[k])
            x1=x1+p
            x3=x3+1
        else:
            break
    x2=x3
    print(x2+x)

Submission status: Accepted

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

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

Submission status: Accepted

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

if Asum < Bsum:
    flg = "NO"

else:
    flg = "YES"
    for n in range(N):
        a = A[n]
        b = B[n]
        if a >= b:
            test.append(a-b)
            yoy.append(a-b)
        else:
            test.append(a-b)
            mnu.append(a-b)
    min_num = len(mnu)#結果に足す
    min_sum = sum(mnu)#これより大きく

    yoy.sort(reverse=True)

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

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

Submission status: Accepted

Solution code:
# -*- coding utf-8 -*-
import sys
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = [A[i] - B[i] for i in range(N)]
D.sort()

if D[0] >= 0:
    print(0)
    sys.exit()

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

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

print(count)

Submission status: Accepted

Solution code:
import sys

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

    new_p = sorted(p, reverse=True)

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

    if p_sum + m_sum < 0 :
        return -1

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

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

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


if __name__ == '__main__' :

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

    print(check(n,a,b))

Submission status: Accepted

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

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

    for i in plus_sort:
        if minus_sum == 0:
            print(ans)
            break
        minus_sum -= i
        ans += 1
        if minus_sum < 0:
            print(ans)
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = sorted([a[i] - b[i] for i in range(n)])
k = sum(x < 0 for x in c)
neg = -sum(c[:k])
pos = c[len(c) - 1]

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

Submission status: Accepted

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

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


C_less=[]
C_more=[]

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

C_more2=list(sorted(C_more, reverse=True))
# print(C_less)
# print(C_more2)
ans=len(C_less)
s=sum(C_less)
if s==0:
	print(0)
	exit(0)
# print(s)
s_m=0
for i in range(len(C_more2)):
	s_m+=C_more2[i]
	# print(i,s_m)
	if s_m>=s:
		d=i+1
		break
print(ans+d)

Submission status: Accepted

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

diff_m = 0
diff_p = []
mcount = 0

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

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

    if diff_m == 0:
        print(0)

    else:
        diff_p.sort(reverse = True)

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

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

            else:
                continue

Submission status: Accepted

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


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


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

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

print(m())

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
import sys

n = int(input())

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

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


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

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

buf = pos[j]
i = 0

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]
ABs = sorted([a - b for a, b in zip(As, Bs)])
# print(As, Bs, ABs)
if sum(As) < sum(Bs):
    print(-1)
    exit()
if min(ABs) > 0:
    print(-1)
    exit()
mABs = [ab for ab in ABs if ab < 0]
msum, ans = sum(mABs), len(mABs)
# print(mABs, msum, ans)
now = -1
while msum < 0:
    msum += ABs[now]
    ans += 1
    now -= 1
print(ans)