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()))
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])
        
    debt = sorted(debt)
    deposit = sorted(deposit,reverse=True)
    
    debt_duplicated = debt.copy()
    deposit_duplicated = deposit.copy()
    
    deposit_trash = []
    
    #print(debt)
    #print(deposit)
    
    for i in range(len(debt)) :
        
        while True :
            if debt[i] == 0 :
                break
            else :
                if debt[i] <= deposit[0] :
                    deposit[0] -= debt[i]
                    debt[i] = 0
                else :
                    debt[i] -= deposit[0]
                    deposit[0] = 0
                    deposit_trash.append(deposit.pop(0))
        
            
        
#        for j in range(len(deposit)) :
#            if debt[i] <= deposit[j] :
#                deposit[j] -= debt[i]
#                debt[i] = 0
#                break
#            else :
#                debt[i] -= deposit[j]
#                deposit[j] = 0
    deposit = deposit_trash + deposit
    for i in range(len(debt)) :
        if debt[i] != debt_duplicated[i] :
            ans += 1
    for i in range(len(deposit)) :
        if deposit[i] != deposit_duplicated[i] :
            ans += 1

print(ans)
#print(debt)
#print(deposit)

Submission status: Accepted

Solution code:
n=int(input())
Alist=[int(i) for i in input().split(" ")]
Blist=[int(i) for i in input().split(" ")]
sumA=sum(Alist)
sumB=sum(Blist)
Dlist=[]
muinus=0
muinus_num=0
count=0
if sumA<sumB:
    print(-1)
else:
    for i in range(n):
        AB=Alist[i]-Blist[i]
        Dlist.append(AB)
        if AB<0:
            muinus+=AB
            muinus_num+=1
    Dlist.sort(reverse=True)
    while muinus<0:
        muinus+=Dlist[count]
        count+=1
    print(muinus_num+count)

Submission status: Accepted

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(10**8)

def nibutan(r, target): # r:sorted list
    l = len(r)
    if l==2:
        if r[0]<target<=r[1]:
            return 1
        elif r[0]>=target:
            return 0
        else:
            return -2
    if r[0]==target:
        return 0
    elif r[-1]==target:
        return l-1
        
    tmp = l//2
    if r[tmp]<target<=r[tmp+1]:
        return tmp+1
    elif target <= r[tmp]:
        return nibutan(r[:tmp+1], target)
    elif target > r[tmp+1]:
        return tmp+nibutan(r[tmp:], target)


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

x = a-b
negative = x[x<0]
positive = sorted(x[x>0], reverse=True)
negative_sum = -negative.sum()

if negative_sum<=0:
    print(0)
    exit()

if len(positive)==0:
    print(-1)
    exit()

sub_sum = []
tmp = 0
for i in range(len(positive)):
    tmp += positive[i]
    sub_sum.append(tmp)

#print(positive, negative, sub_sum)

if negative_sum > sub_sum[-1]:
    print(-1)
    exit()
if negative_sum <= sub_sum[0]:
    print(1+len(negative))
    exit()

print(nibutan(sub_sum, negative_sum)+len(negative)+1)

# l = len(positive)
# s = 0
# f = l

# for i in range(l):
#     #print(f,s)
#     tmp = (f+s)//2
#     if sub_sum[max(0,tmp-1)]<negative_sum<=sub_sum[tmp]:
#         print(tmp-1+len(negative))
#         exit()
#     # elif sub_sum[tmp] < negative_sum <= sub_sum[min(tmp+1, l-1)]:
#     #     print(tmp+1+len(negative),tmp)
#     #     exit()
#     elif sub_sum[tmp]>negative_sum:
#         f = min(tmp+1, l)
#     elif sub_sum[tmp]<negative_sum:
#         s = tmp

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
 
if sum(a) < sum(b):
    print('-1')
    exit()
 
count = 0
sabun = [a[i]-b[i] for i, _ in enumerate(a)]
husoku = sum([x for x in sabun if x<0])
sabun.sort(reverse=True)

for i in sabun:
    if husoku >= 0:
        break
    husoku += i
    count += 1
 
print(len([x for x in sabun if x<0]) + count)

Submission status: Accepted

Solution code:
import copy
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
X=[A[n]-B[n] for n in range(N)]
if sum(X)<0:
    print(-1)
else:
    Y=copy.copy(X)
    minus_list=[y for y in Y if y<0]
    plus_list=sorted([y for y in Y if y>0])[::-1]
    count=len(minus_list)
    minus=sum(minus_list)
    plus=0
    for z in plus_list:
        if plus+minus>=0:
            break
        count+=1
        plus+=z
    print(count)

Submission status: Accepted

Solution code:
def qSort(array):
    left = []
    right = []

    if len(array) <= 1:
        return array

    ref = array[0]
    array = array[1:]
    for a in array:
        if a < ref:
            left.append(a)
        else:
            right.append(a)

    return qSort(left) + [ref] + qSort(right)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C_minus = []
C_plus = []
cnt = 0
if sum(A) < sum(B):
    print("-1")

else:
    for i, a in enumerate(A):
        tmp = A[i] - B[i]
        if tmp > 0:
            C_plus.append(tmp)
        elif tmp < 0:
            C_minus.append(tmp)

    C_plus = list(reversed(qSort(C_plus)))
    C_minus = qSort(C_minus)

    if len(C_minus) == 0:
        print(len(C_minus))
    else:
        start = 0
        judge = 0
        for c_m in C_minus:
            judge = c_m
            for i in range(start, len(C_plus)):
                judge += C_plus[i]
                if judge > 0:
                    start = i
                    C_plus[i] = judge
                    break

        print(len(C_minus)+start+1)

Submission status: Accepted

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

plus_list = []
ans = plus = minus = 0
for a, b in zip(A, B):
    if a - b >= 0:
        plus += (a - b)
        plus_list.append(a - b)
    else:
        ans += 1
        minus += (b - a)

if len(plus_list) == N:
    print(0)
elif minus > plus:
    print(-1)
elif minus == plus:
    print(N)
else:
    plus_list.sort(reverse=True)
    for plus_num in plus_list:
        minus -= plus_num
        ans += 1
        if minus <= 0:
            break
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int, input().split()))
b = list(map(int, input().split()))
plus = []
minus = []
for ai, bi in zip(a,b):
    point = ai-bi
    if point > 0:
        plus.append(point)
    elif point < 0:
        minus.append(point)

total_minus = abs(sum(minus))
# print("total minus is ", total_minus)


plus.sort(reverse=True)
# print(plus, minus, sep='\n')

ans = 0
if sum(plus) + sum(minus) < 0:
    print(-1)
else:
    sum = 0
    for i in range(len(plus)):
        if sum >= total_minus:
            break
        sum+=plus[i]
        ans+=1

    ans+=len(minus)
    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()] # 必要な準備

needs = []
need = 0
ans = 0
if sum(A) < sum(B):
    print(-1)
else:
    for i in range(N):
        diff = A[i] - B[i]
        if diff < 0:
            ans += 1
            need += (-diff)
        elif diff > 0:
            needs.append(diff)
    if need != 0:
        needs.sort(reverse=True)
        for item in needs:
            need -= item
            ans += 1
            if need <= 0:
                break
    print(ans)

Submission status: Accepted

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

for i in range(N):
    if A[i] < B[i]:
        amin.append(B[i]-A[i])
    elif A[i] > B[i]:
        bmin.append(A[i]-B[i])

if amin == []:
    print(0)
    exit()
#amin.sort()
bmin.sort()

#print(bmin)

if sum(amin) > sum(bmin):
    print(-1)
    exit()

num = sum(amin)
for i in range(len(bmin)):
    #print(bmin[len(bmin)-i-1])
    num -= bmin[len(bmin)-i-1]
    if num < 0:
        print(len(amin)+i+1)
        break
else:
    print(N)

Submission status: Accepted

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

diff = (list)(map(lambda a, b: a - b, A, B))
sum_diff = sum(diff)


def check(a):
    for i in range(n):
        if a[i] < 0:
            return False
    return True


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

elif check(diff):
    print(0)
else:
    ans = 0
    pos_diff = (list)(filter(lambda x: x > 0, diff))
    pos_diff = sorted(pos_diff)
    for i in range(len(pos_diff)):
        rm = pos_diff[i]
        if sum_diff-rm > 0:
            ans += 1
            sum_diff -= rm

        else:
            print(n-ans)
            exit()

Submission status: Accepted

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

tmp=0
if sum(A)>=sum(B):
    D=[A[i]-B[i] for i in range(N)]
    D.sort(reverse=True)
    t_ind = [i for i, d in enumerate(D) if d<0]
    if len(t_ind)!=0:
        t_sum=sum([D[t] for t in t_ind])
        for i in range(len(D)-len(t_ind)):
            tmp+=D[i]
            if tmp>=-t_sum:
                print((i+1)+len(t_ind))
                break
    else:
        print(0)
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
h=0
c=0
minl=[]
for i in range(n):
  a=al[i]
  b=bl[i]
  if a<b:
    h+=b-a
    c+=1
  else:
    minl.append(a-b)
minl.sort()
minl.reverse()
if c==0:
  print(c)
  exit()
if len(minl)==0:
  print(-1)
  exit()
for i in minl:
  h-=i
  c+=1
  if h<=0:
    print(c)
    exit()
print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# C
# 
import numpy as np
# input
n = int(input())
a = np.array(list(map(int,input().split())))
b = np.array(list(map(int,input().split())))
l = a-b
l_fu = np.where(l<0,l,0).sum() * (-1)
fu = np.sum(l<0)
z = np.sum(l==0)
m = np.sort(l)[::-1]
o = x = 0
# calc
if sum(l) < 0:
  o = -1
else:
  if fu == 0:
    o = 0
  else:
    for i in range(n):
      if m[i] > 0:
        x += m[i]
        if x >= l_fu:
          o = i+1+fu
          break
      else:
        break
# output
print(o)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

N = int(input())
A = [int(s) for s in input().split(' ')]
B = [int(s) for s in input().split(' ')]
D = []
cnt = 0
t_diff = 0

if sum(A) < sum(B):
    print(-1)
    sys.exit()
    
for i in range(len(A)):
    if A[i] < B[i]:
        cnt = cnt + 1
        t_diff = t_diff + (B[i] - A[i])
    if A[i] > B[i]:
        D.append(A[i] - B[i])

D.sort(reverse=True)

for i in range(len(D)):
    
    if t_diff <= 0:
        break
    t_diff = t_diff - D[i]
    cnt = cnt + 1
    
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 12 18:50:46 2019
KeyenceC
@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = [0]*n
ok = False

if sum(a) >= sum(b):
    ok = True
    sum_fusoku = 0
    cnt_fusoku = 0
    larger = []
    for i in range(n):
        ab = a[i] - b[i]
        if ab < 0:
            sum_fusoku += -ab
            cnt_fusoku += 1
        else:
            larger.append(ab)
                
    larger.sort(reverse=True)
    cnt_large = 0
    for i in larger:
        if sum_fusoku <= 0:
            break
        sum_fusoku -= i
        cnt_large += 1

if ok == False:
    print(-1)
else:
    ans = cnt_fusoku + cnt_large
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in input().split():
	A.append(int(i))
B = []
diff = []
Ind = 0
zero_or_positive_diff = []
negative_diff = []
for i in input().split():
	b = int(i)
	B.append(b)
	d = A[Ind] - b
	diff.append(d)
	if d >= 0:
		zero_or_positive_diff.append(d)
	else:
		negative_diff.append(d)
	Ind += 1

zero_or_positive_diff.sort()
zero_or_positive_diff.reverse()
sum_negative_diff = sum(negative_diff)

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

Ind = 0
part_sum_zero_or_positive_diff = 0
while part_sum_zero_or_positive_diff + sum_negative_diff < 0:
	sum_negative_diff += zero_or_positive_diff[Ind]
	Ind += 1

print(Ind+len(negative_diff))

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total_A = sum(A)
total_B = sum(B)
if total_A < total_B:
    print(-1)
d = [a_i - b_i for a_i, b_i in zip(A, B)]
d.sort(reverse=True)
minus = [i for i in d if i < 0]
len_d = len(minus)
total_d = sum(minus)
n = 0
for i in d:
    if total_d >= 0:
        print(n + len_d)
        break
    total_d += i
    n += 1

Submission status: Accepted

Solution code:
import bisect


def read():
    N = int(input().strip())
    A_iter = map(int, input().strip().split())
    B_iter = map(int, input().strip().split())
    return N, A_iter, B_iter


def index(a, x):
    i = bisect.bisect_left(a, x)
    return i


def solve(N, A_iter, B_iter):
    C = []
    for a, b in zip(A_iter, B_iter):
        C.append(a - b)
    C = sorted(C)
    zero_index = index(C, 0)
    few = C[:zero_index]
    many = C[zero_index:]

    few_size = len(few)
    few_sum = -sum(few)

    if few_sum == 0:
        return few_size
    for i, m in enumerate(reversed(many)):
        few_sum -= m
        if few_sum <= 0:
            return few_size + i + 1
    return -1


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

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:
    num, fix = 0, 0
    for i in range(N):
        if A[i] - B[i] <  0:
            num += 1
            fix += B[i] - A[i]
    if num != 0:
        ret = []
        for i in range(N):
            if A[i] - B[i] > 0:
                ret.append(A[i] - B[i])
        ret.sort(reverse=True)
        su = 0
        for i in range(len(ret)):
            su += ret[i]
            num += 1
            if su >= fix:
                break
    print(num)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Diff = [None]*N

for i in range(N):
  Diff[i] = A[i] - B[i]
  
sort_Diff= sorted(Diff,reverse=True)

flag=0
plus_sum=0
ans=0
minus=N+1

for i in range(N):
  if sort_Diff[i]<0:
    minus = i
    break
  plus_sum += sort_Diff[i]

if sum(sort_Diff)<0:
  print(-1)
else:
  ans = N - minus
  minus_sum = sum(sort_Diff)-plus_sum
#print(plus_sum)
#print(minus_sum)
    
  for i in range(N):
    if minus_sum + abs(sort_Diff[i]) < 0:
      minus_sum = minus_sum + abs(sort_Diff[i]) 
      if minus_sum ==0:
        break
      ans += 1
    else:
      ans = ans+1
      break

  print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    if sum(A) < sum(B):
        return -1

    d = [0]*N
    ans = 0
    fusoku = 0
    for i in range(N):
        d[i] = A[i] - B[i]
        if d[i] < 0:
            ans += 1
            fusoku -= d[i]
    d.sort()
    for dd in d[::-1]:
        if fusoku <= 0:
            break
        fusoku -= dd
        ans += 1
    return ans
        

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

Submission status: Accepted

Solution code:
def magic(n, a, b):
    if sum(a) < sum(b):
        return(-1)

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

    em = 0
    cm = 0
    for i in range(n):
        if d[i] < 0:
            em += d[i]
            cm += 1
    d.sort(reverse=True)
    ep = 0
    cp = 0
    for i in range(n):
        ep += d[i]
        cp += 1
        if ep >= abs(em):
            break
    return(cm + cp)

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

print(magic(n, a, b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = [A[i]-B[i] for i in range(n)]

plus = []
minus = []

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

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

for i in range(len(plus)):
    f += plus[i]
    if f>=0:
        print(i+1+len(minus))
        break
else:
    print(-1)

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

isgoukaku = True
fusokus = []
yobun = []

for a, b in zip(A, B):
    if a < b:
        # 不足点系列
        fusokus.append(b - a)
        isgoukaku = False
    else:
        # 余分な点系列
        yobun.append(a - b)
# print(fusokus, yobun)


if isgoukaku:
    print(0)
    exit()

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

henkou = len(fusokus)
# print(henkou)
tmp = 0
for y in yobun:
    # print(tmp, fusoku)
    if tmp >= fusoku:
        break
    tmp += y
    henkou += 1
    # print(henkou)

print(henkou)

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
A_list = np.array(list(map(int, input().split()))) # "1 2 3"と入力
B_list = np.array(list(map(int, input().split())))# "1 2 3"と入力

D_list= A_list-B_list

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect
# N = input()   # 単純な入力
# input = list(input()) # ひとかたまりの文字列をリスト化
# N = int(input())  # 整数一つ
# input = input().split()   # スペース区切りを文字としてリスト化
# X,Y = map(int,input().split())  # ２個や３個の整数をそれぞれ取得
# input = list(map(int,input().split()))
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

sa = [A[i] - B[i] for i in range(len(A))]
sa.sort()
# print(sa)
insert_index = bisect.bisect_left(sa,0)
# print(insert_index)
Sum = sum(sa[0:insert_index])
Sumlist = [0 for i in range(N)]
isum = 0

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

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

Submission status: Accepted

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


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

    return ans


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

Submission status: Accepted

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

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


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

    
    print(count)

Submission status: Accepted

Solution code:
from itertools import accumulate
import numpy as np

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

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

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

Submission status: Accepted

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

M = []
P = []

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

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

P.sort(reverse=True)

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

print(count + len(M))

Submission status: Accepted

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


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

c = a[:]

if a_sum< b_sum:
    print(-1)

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

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

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

yobun = sum_A - sum_B

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

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

        A_B_plus = np.sort(A_B_array[A_B_array >= 0])[::-1]
        A_B_plus_ruiseki = np.cumsum(A_B_plus)
        #print("ruiseki", A_B_plus_ruiseki)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

    A_B = A_B[::-1]

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

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

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

Submission status: Accepted

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


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

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

Submission status: Accepted

Solution code:
#!/Users/takahiro.kida/git/atcorder/venv/bin/python

import numpy
import scipy

from collections import defaultdict
from pprint import pprint

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

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

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

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

Submission status: Accepted

Solution code:
from pprint import pprint
import sys
input = sys.stdin.readline

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

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

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

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

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

d.sort()
m = 0

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



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

    print(j + k)

Submission status: Accepted

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

diff = [None] * N

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

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

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
L = [i - j for (i,j) in zip(a,b)]
check = np.sum(x < 0 for x in L)
if check == 0:
  print(0)
else:
  neg = np.sum(x for x in L if x < 0)
  pos = sorted([x for x in L if x > 0], reverse=True)
  sum = 0
  finish = 0
  for i in range(len(pos)):
    sum += pos[i]
    if neg + sum >= 0:
      print(i + 1 + check)
      finish = 1
      break
  if finish == 0:
    print(-1)

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()
diffl = [0]*len(a)
for i in range(len(a)):
    diffl[i] = a[i] - b[i]
diffl.sort()
if diffl[0] >= 0:
    print(0)
    exit()
ret = 0
msum = 0
for num in diffl:
    if num < 0:
        msum -= num
        ret += 1
for num in diffl[::-1]:
    if num >= msum:
        ret += 1
        break
    else:
        ret += 1
        msum -= num
print(ret)