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:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    B=[int(_) for _ in input().split()]
    sumA=sum(A)
    sumB=sum(B)
    if sumA<sumB:
        print(-1)
        sys.exit()
    D=[0]*n
    sum_plus=0
    sum_minus=0
    ans=0
    for i in range(n):
        diff=A[i]-B[i]
        if diff>0:
            D.append(diff)
        elif diff<0:
            sum_minus-=diff
            ans+=1
    D.sort(reverse=True)
    if sum_minus==0:
        print(0)
        sys.exit()
    for d in D:
        ans+=1
        if sum_minus<=d:
            print(ans)
            break
        sum_minus-=d


if __name__=='__main__':
    main()

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

c=[]
ans=0
count=0
for i in range(n):
    c.append(a[i]-b[i])
    if c[i]<0:
        ans+=1
        count+=c[i]
#print(count)
c.sort(reverse=True)
#print(ans)
i=0
while count<0:
    count+=c[i]
    ans+=1
    i+=1
    #print("DFGH")

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

helper = []
need, ans = 0, 0

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

for i in range(N):
  if A[i] > B[i]:
    helper.append(A[i] - B[i])
  elif A[i] < B[i]:
    need += B[i] - A[i]
    ans += 1

helper = sorted(helper, reverse=True)
i = 0
while need > 0:
  need -= helper[i]
  i += 1
  ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
from itertools import accumulate
cs = [a-b for a,b in zip(As,Bs)]
cs.sort(reverse=True)
#負の数を埋めるのに、正の数を何個使うか
p = [0]+[c for c in cs if c > 0]
m = [c for c in cs if c < 0]


ans = 0

m_all = sum(m)
acc = list(accumulate(p))
ans += len(m)
for i,p in enumerate(acc):
    if p >= -m_all:
        break
ans += i


if sum(As) < sum(Bs):
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import sys, re
def せいすう(): return int(input())
def たくさんのせいすう(): return map(int, input().split())
def せいすうのリスト(): return list(map(int, input().split()))

ようそすう = せいすう()
いま = せいすうのリスト()
もくひょう = せいすうのリスト()

def こうじゅんにならびかえ(リスト):
	return sorted(リスト, reverse=True)

def しゅつりょく(しゅつりょくしたいもじ):
	print(しゅつりょくしたいもじ)

def ごうけい(リスト):
	return sum(リスト)

def ながさ(リスト):
	return len(リスト)

def まとめる(リスト1, リスト2):
	return zip(リスト1, リスト2)

def おわり():
	exit()

def いちをたす(すうじ):
	return すうじ+1

def リストについか(リスト, あたい):
	リスト.append(あたい)

if ごうけい(いま) < ごうけい(もくひょう):
	しゅつりょく(-1)
	おわり()

ふそくリスト = []
よゆうリスト = []

for いまのじゅんびど, もくひょうのじゅんびど in まとめる(いま, もくひょう):
	if いまのじゅんびど < もくひょうのじゅんびど:
		リストについか(ふそくリスト, もくひょうのじゅんびど-いまのじゅんびど)
	else:
		リストについか(よゆうリスト, いまのじゅんびど-もくひょうのじゅんびど)

ふそくこすう = ながさ(ふそくリスト)
if ふそくこすう == 0:
	しゅつりょく(0)
	おわり()

ふそくぶん = ごうけい(ふそくリスト)
よゆうソート = こうじゅんにならびかえ(よゆうリスト)

こたえ = ふそくこすう
おぎなうぶん = 0
for よゆうぶん in よゆうソート:  # これはにぶたんでもできます
	おぎなうぶん += よゆうぶん
	こたえ = いちをたす(こたえ)
	if おぎなうぶん >= ふそくぶん:
		break

しゅつりょく(こたえ)

Submission status: Accepted

Solution code:
INF = 10 ** 9
MOD = 10 ** 9 + 7
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  math import factorial


def main():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    if sum(a) < sum(b):
        print(-1)
        return
    c = [p - q for p,q in zip(a,b)]
    c.sort(reverse=True)
    minus = 0
    ans = 0
    for i in range(n - 1,-1,-1):
        if c[i] < 0:
            minus += c[i]
            ans += 1
        else:
            break
    
    for i in range(n):
        if minus >= 0:
            break
        minus += c[i]
        ans += 1
    print(ans)
if __name__ == '__main__':
    main()

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:
    before = [A[i] - B[i] for i in range(N)]
    before.sort()
    diff = [A[i] - B[i] for i in range(N)]
    # print(diff)
    diff.sort()
    left = 0
    right = N - 1
    while True:
        if diff[left] < 0:
            temp = diff[left]
            diff[left] = min(0, diff[left] + diff[right])
            diff[right] -= diff[left] - temp
        if diff[left] == 0:
            if diff[left + 1] < 0:
                left += 1
        if diff[right] == 0:
            if diff[left] < 0:
                right -= 1
        if diff[left] == 0:
            break
    # print(diff)
    cnt = 0
    for i in range(N):
        if before[i] != diff[i]:
            cnt += 1
    print(cnt)

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[]
cnt=0
fusoku=0 #正の値とする
for i in range(N):
  C.append([A[i],B[i],A[i]-B[i]])
  if A[i]-B[i]<0:
    cnt += 1
    fusoku += abs(A[i]-B[i])
    
#print(C)
#print(fusoku)
  
if sum(A)<sum(B):
  print(-1)
else:
  C.sort(key=lambda x:x[2],reverse=True)
  for t in C:
    if fusoku<=0:
      break
    else:
      cnt +=1
      fusoku -= t[2]
  print(cnt)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    c=[]
    minus=0
    ans=0
    if sum(a)<sum(b):
        print(-1)
    else:
        for i in range(n):
            dif=a[i]-b[i]
            if dif>0:
                c.append(dif)
            elif dif<0:
                ans+=1
                minus+=dif
        c.sort(reverse=True)
        i=0
        while minus<0:
            minus+=c[i]
            i+=1
        print(ans+i)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
suma=sum(a)
sumb=sum(b)
if suma<sumb:
  print(-1)
  exit()
d=[]
for i in range(n):
  d.append(a[i]-b[i])
d.sort()
import bisect
minus=bisect.bisect_left(d,0)
sum_minus=sum(d[:minus])
sum_plus=0
ans=minus
i=1
while sum_plus < -sum_minus:
  sum_plus+=d[-i]
  ans+=1
  i+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))

if sum(a_li)<sum(b_li):
    print(-1)
    sys.exit()

amari_li = []
tarinai_li = []

for  a,b in zip(a_li,b_li):
    if a >= b:
        amari_li.append(a-b)
    else:
        tarinai_li.append(b-a)

t = sum(tarinai_li)

#print(amari_li,tarinai_li)

if len(tarinai_li) == 0:
    print(0)
    sys.exit()

amari_li.sort(reverse=True)
count = 0
for a in amari_li:
    t -= a
    count += 1
    if t <= 0:
        break
print(count+len(tarinai_li))

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)
else:
    x = sorted([a[i]-b[i] for i in range(n)], reverse=True)
    s = -sum(d for d in x if d < 0)
    ans = sum(1 for d in x if d < 0)
    i = 0
    while s > 0:
        s -= x[i]
        ans += 1
        i += 1
    print(ans)

Submission status: Accepted

Solution code:
from queue import Queue

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

D = [a - b for a, b in zip(A, B)]
Dm = [d for d in D if d < 0]
sum_Dm = abs(sum(Dm))

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

Dp = [d for d in D if d > 0]
Dp.sort(reverse=True)

que = Queue()
for d in Dp:
    que.put(d)

cnt = 0
while not que.empty():
    p = que.get()
    cnt += 1
    sum_Dm -= p
    if sum_Dm <= 0:
        print(len(Dm) + cnt)
        exit(0)

print(-1)

Submission status: Accepted

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

ans = 0
asum = sum(a)
c = []
csum = 0
num = []
for i,j in zip(a,b):
	if i<j:
		i = i + (j-i)
		ans += 1
	if i>j:
		heapq.heappush(num, -1*(i-j))
	csum += i


while csum>asum and len(num):
	i = heapq.heappop(num)*-1
	csum -= i
	ans += 1

if csum>asum: ans = -1
	
print(ans)

Submission status: Accepted

Solution code:
n= int(input())
*a,=map(int, input().split( ))
*b,=map(int, input().split( ))
ab = [a[i]-b[i] for i in range(n)]
if sum(ab)<0:
    print(-1)
    exit()

ab.sort()
import bisect
j = bisect.bisect_left(ab,0)
minus = sum(ab[:j])

ans = 0
while minus<0:
    abi = ab.pop()
    minus += abi
    ans += 1
print(ans+j)

Submission status: Accepted

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

from bisect import bisect_left

def solve(n, A, B):
  if sum(A) < sum(B):
    print(-1)
    return

  under = []
  over = []
  for a, b in zip(A, B):
    if a < b: under.append(b-a)
    elif a > b: over.append(a-b)

  over_acc = [0]
  for v in sorted(over, reverse=True):
    over_acc.append(over_acc[-1] + v)

  ans = len(under)
  ans += bisect_left(over_acc, sum(under))
  print(ans)

solve(n, A, B)

Submission status: Accepted

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

if sumA < sumB:
    print(-1)
    exit(0)

# 条件確認する
flag = True
ok = []
not_ok = []
not_ok_sum = 0
for i in range(n):
    if A[i] < B[i]:
        not_ok.append(i)
        not_ok_sum += A[i]
        flag = False
    else:
        ok.append(A[i] - B[i])

if flag:
    print(0)
    exit(0)

ok.sort()
ok.reverse()

# print(n)
# print(A)
# print(B)
# print(not_ok)
# print(not_ok_sum)
# print(sumA - not_ok_sum)
# print(ok)

# 残っている余力の大きい方から順番に使って，足りていない分を埋めていく
count = 0
current = 0
now_prev = 0
rem = 0
while True:
    now = now_prev + ok[current]
    count += 1
    # print(current, now)
    while rem < len(not_ok):
        j = not_ok[rem]
        # print("  ", j, B[j] - A[j])
        if B[j] - A[j] < now:
            rem += 1
            now -= (B[j] - A[j])
            # print(now)
            count += 1
        else:
            break
    if rem == len(not_ok):
        break
    else:
        now_prev = now
        current += 1
print(count)

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()
D = []
pre_ans = 0  # 確実に変えなくてはいけないやつ
for i in range(N):
    if A[i] != B[i]:
        D.append(A[i] - B[i])
        if A[i] < B[i]:
            pre_ans += 1
D = sorted(D)

used = 0
tmp = 0
# print(D)
l, r = 0, len(D)
while r - l > 1:
    # print("now tmp: {}".format(tmp))
    if tmp + D[l] < 0:
        # 新たに借りる
        r -= 1
        used += 1
        tmp += D[r]
    tmp += D[l]
    l += 1
print(used + pre_ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    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]
    enough = diff[diff > 0]
    x = lack.sum()
    enough.sort()
    enough = reversed(enough)
    s = 0
    ans = 0
    for e in enough:
        if s < x:
            s += e
            ans += 1
        else:
            break
    if s < x:
        print(-1)
    else:
        print(len(lack) + ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

from copy import deepcopy

c = deepcopy(b)

ans = 0

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

diff = 0
ad = []

for i, j in zip(a, b):
    if i == j:
        pass
    elif i > j:
        ad.append(i - j)
    else:
        diff += (j - i)
        ans += 1

ad.sort()

while diff > 0:
    diff -= ad.pop()
    ans += 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(A) < sum(B):
    print(-1)
    exit()
BA = sorted(i - j for i, j in zip(A, B))

not_enough = 0
ans = 0
for ba in BA:
    if ba < 0:
        not_enough += ba
        ans += 1
    else:
        break
for ba in BA[::-1]:
    if not_enough >= 0:
        break
    not_enough += ba
    ans += 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(A)<sum(B):
    print(-1)
else:
    a=0
    s=0
    l=[]
    for i in range(N):
        if A[i]<B[i]:
            a+=1
            s+=B[i]-A[i]
        else:
            l.append(A[i]-B[i])
    l.sort(reverse=1)
    for i in range(len(l)):
        if s<=0:
            break
        s-=l[i]
        a+=1
    print(a)

Submission status: Accepted

Solution code:
#1st: Code Festival 2015 Qualifying B (0408)
if False:
    N,M=map(int,input().split())
    A=[int(i) for i in input().split()]
    B=[int(i) for i in input().split()]
    A.sort()
    B.sort()
    ans='YES'
    if N<M:
        ans='NO'
    else:
        while B:
            a=A.pop()
            b=B.pop()
            if b>a:
                ans='NO'
                break
    print(ans)

#2nd: CF2014QA-C
if False:
    A,B=map(int,input().split())
    def uruu(n):
        ret=0
        ret+=n//4
        ret-=n//100
        ret+=n//400
        return ret
    print(uruu(B)-uruu(A-1))

#3rd: Code Formula 2014 - C
if False:
    S=input()
    stack=list(S[::-1])
    # print(stack)
    user=[]
    s=stack.pop()
    while stack:
        # print(s)
        if s=="@":
            tmp=''
            x=stack.pop()
            while not(x in ('@',' ')):
                # print(x)
                tmp+=x
                if not stack:
                    break
                x=stack.pop()
            user.append(tmp)
            s=x
        else:
            s=stack.pop()
    user=list(set(user))
    user.sort()
    for u in user:
        if u:
            print(u)

#4th: Hitachi Programming Contest 2020 - A    
if False: 
    S=input()
    hi=['hi'*i for i in range(1,10)]
    print('Yes' if S in hi else 'No')

#5th: Keyence Programming Contest 2019 - C
if True:
    N=int(input())
    A=[int(i) for i in input().split()]
    B=[int(i) for i in input().split()]
    S=list(zip(A,B))
    if sum(A)<sum(B):
        print(-1)
    else:
        C=[]
        for s in S:
            a,b=s
            if a<b: C.append(s)
        CA=sum(C[i][0] for i in range(len(C)))
        CB=sum(C[i][1] for i in range(len(C)))
        S.sort(key=lambda x: x[0]-x[1])
        while CA<CB:
            c,d=S.pop()
            CA+=c
            CB+=d
            C.append((c,d))
        print(len(C))

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):
    result = -1
else:
    count = 0
    over = [0]
    result = 0
    for i in range(n):
        if a[i]>b[i]:
            over.append(a[i]-b[i])
        elif a[i]<b[i]:
            count += b[i]-a[i]
            result += 1
    over.sort(reverse=True)
    i = 0
    while count > 0:
        result += 1
        count -= over[i]
        i += 1
print(result)

Submission status: Accepted

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

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

ans = 0
C = []
D = []
for i in range(n):
    if B[i] > A[i]:
        C.append(B[i] - A[i])
        ans += 1
    elif A[i] > B[i]:
        D.append(A[i] - B[i])

s = sum(C)
# print(s, C, D)

D.sort(reverse=True)
i = 0
while i < len(D) and s > 0:
    s -= D[i]
    ans += 1
    i += 1
# print(s)
if s > 0:
    ans = -1
print(ans)

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""

import sys


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    D = []
    S = 0
    cnt = 0
    for i in range(N):
        d = B[i] - A[i]
        if d > 0:
            S += d
            cnt += 1
        else:
            D.append(-d)
            if len(D) == N:
                print(0)
                sys.exit()
    D.sort()
    ans = 0
    for i in range(len(D)-1, -1, -1):
        ans += 1
        if D[i] < S:
            S -= D[i]
            if i == 0:
                if S > 0:
                    print(-1)
                    sys.exit()
        else:
            break
    print(ans + cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
p = []
ans = 0
x = sum(A)
k = 0
for i in range(n):
    if A[i] < B[i]:
        ans += 1
        k += A[i] - B[i]
    elif A[i] > B[i]:
        p.append(A[i] - B[i])
        
p.sort(reverse=True)
for i in p:
    if k >= 0:
        break
    k += i
    ans += 1

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
p, s, ans = [], 0, 0
for i, (a, b) in enumerate(zip(A, B)):
    if a > b:
        p.append(a-b)
    elif a < b:
        s += b-a
        ans += 1
p.sort(reverse=1)
for x in p:
    if s > 0:
        s -= x
        ans += 1
else:
    if s > 0:
        ans = -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(A) < sum(B):
    print(-1)
else:
    minus = 0
    plus_list = []
    ans = 0
    for a, b in zip(A, B):
        if a - b < 0:
            ans += 1
            minus += b - a
        elif a - b > 0:
            plus_list.append(a - b)

    plus_list.sort(reverse=True)
    for p in plus_list:
        if minus <= 0:
            break
        ans += 1
        minus -= p
    if minus > 0:
        print(-1)
    else:
        print(ans)

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:
    minus = 0
    plus_list = []
    ans = 0
    for a, b in zip(A, B):
        if a - b < 0:
            ans += 1
            minus += b - a
        elif a - b > 0:
            plus_list.append(a - b)

    plus_list.sort(reverse=True)
    for p in plus_list:
        if minus <= 0:
            break
        ans += 1
        minus -= p

    print(ans)

Submission status: Accepted

Solution code:
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=sorted(c,reverse=1)
if sum(c)<0:
    print(-1)
    exit()
d=0
e=0
f=0
for i in range(n):
    if c[i]<0:
        d+=c[i]
        e+=1
for i in range(n):
    if f>=abs(d):
        break
    f+=c[i]
print(e+i)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

def main():
    N = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    m,p = [],[]
    
    for x,y in zip(a,b):
        if x > y:
            p.append(x-y)
        elif x < y:
            m.append(y-x)

    ch = sum(m)
    p.sort(reverse=True)
    ans = len(m)
    l = 0
    while ch > 0:
        if l == len(p):
            print(-1)
            exit()
        else:
            ch -= p[l]
            ans += 1
            l += 1
            
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = LI()
    b = LI()
    if sum(a) < sum(b):
        print(-1)
    else:
        tmp = 0
        ans = 0
        ab = list(map(lambda x: x[0] - x[1], zip(a, b)))
        ab.sort()
        for i in ab:
            if i < 0:
                ans += 1
                tmp += i
            else:
                break
        for i in ab[::-1]:
            if tmp < 0:
                if i > 0:
                    ans += 1
                    tmp += i
        print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

if sum(A) < sum(B):
    print(-1)
else:
    need = 0
    ans = 0
    for i in range(N):
        aff[i] = A[i] - B[i]
    
    aff = sorted(aff)
    affrev = aff[::-1]
    
    for i in range(N):
        if aff[i] < 0:
            ans += 1
            need -= aff[i]
        else:
            break
    
    j = 0
    while need > 0:
        need -= affrev[j]
        ans += 1
        j += 1
    
    print(ans)

Submission status: Accepted

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

s=0
ans=0
for g in range(n):
    if a[g]<b[g]:
        s+=b[g]-a[g]
        ans+=1
    elif a[g]>b[g]:
        dif.append(a[g]-b[g])
dif.sort(reverse=True)

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

for l in range(len(dif)):
    s-=dif[l]
    ans+=1
    if s<0:
        break

if s<=0:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
pluslist=[]
minuslist=[]
cnt=0
ms=0
for i in range(N):
    if A[i]-B[i]<0:
        minuslist.append(A[i]-B[i])
    elif A[i]-B[i]>0:
        pluslist.append(A[i]-B[i])
#print(minuslist,pluslist)
ms=sum(minuslist)
cnt=cnt+len(minuslist)
pluslist.sort(reverse=True)
idx=0
#print(ms,cnt)
while (ms<0)and(idx<len(pluslist)):
    ms=ms+pluslist[idx]
    cnt=cnt+1
    idx=idx+1
if ms<0:
    print("-1")
else:
    print(cnt)

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
check = [0] * n
for i in range(n):
    check[i] = a[i] - b[i]
check.sort()
m_check = sorted(check, reverse=True)
m_cnt = 0
p = 0
ans = 0
for i in range(n):
    if check[i] < 0:
        p += check[i]
        ans += 1
    else:
        if p >= 0:
            print(ans)
            exit()
        else:
            p += m_check[m_cnt]
            m_cnt += 1
            ans += 1
if p >= 0:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()));b=list(map(int,input().split()))
if sum(a)<sum(b): print(-1),sys.exit()
x=sorted([a[i]-b[i] for i in range(n)],reverse=True)
s=-sum(d for d in x if d<0)
ans=sum(1 for d in x if d<0)
i=0
while s>0:
    s-=x[i]
    ans+=1
    i+=1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
SA=sum(A)
SB=sum(B)
if SA<SB:
    print(-1)
    exit()
ans=0
p=[]
m=[]
for i in range(N):
    a=A[i]-B[i]
    if a>=0:
        p.append(a)
    else:
        m.append(a)
if len(m)==0:
    print(0)
    exit()
M=sum(m)
M=abs(M)
p.sort(reverse=True)
for i in range(len(p)):
    if M<=0:
        break
    ans+=1
    M=M-p[i]
ans+=len(m)
print(ans)

Submission status: Accepted

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

diff = [a - b for a, b in zip(A, B)]
diff.sort()
if sum(A) < sum(B):
    print(-1)
else:
    minus_sum = 0
    ans = 0
    for i in range(N):
        if diff[i] < 0:
            minus_sum += diff[i]
            ans += 1
        else:
            break

    for i in range(N - 1, 0, -1):
        if minus_sum >= 0:
            break
        else:
            ans += 1
            minus_sum += diff[i]

    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
"""
先頭からなめていって、
A. ai < biなら対処が必要⇒変更確定
B. ai == biなら変更不要で確定
C. ai > biなら他に融通する可能性あり⇒保留
Aのケースの不足数の総和を計算し、Cの差分の大きいものから順に使っていく。
Cのheapqを作成するのかな？いったんリストでやってみるか
"""
if sum(aa) < sum(bb):
    print(-1)
else:
    total_diff = 0
    margin = []
    modified = 0
    for i in range(n):
        if aa[i] < bb[i]:
            modified += 1
            total_diff += bb[i] - aa[i]
        elif aa[i] > bb[i]:
            margin.append(aa[i]-bb[i])
    margin.sort(reverse=True)
    while total_diff > 0:
        modified += 1
        t = margin.pop(0)
        total_diff -= t
    print(modified)

Submission status: Accepted

Solution code:
import heapq
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a) < sum(b) :
  print(-1)
else :
  ans = 0
  lack = 0
  que = [0]
  heapq.heapify(que)
  for i in range(n) :
    a[i] -= b[i]
    if a[i] < 0 :
      ans += 1
      lack -= a[i]
    else :
      heapq.heappush(que,-a[i])
  while lack > 0 :
    m = heapq.heappop(que)
    lack += m
    ans += 1
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
taka = list(map(int,input().split()))
exam = list(map(int,input().split()))

sa = list()
#　高橋くんがマイナスになっている部分を変更することでうまくいくかどうかを検索する
for t,e in zip(taka,exam):
  sa.append(t-e)
sa = sorted(sa)

minus = list(filter(lambda x:x<0,sa))
plus = list(filter(lambda x:x>0,sa))

sum_plus = 0
sum_minus = sum(minus)
if sum_minus == 0:
  print(0)
else:
  for idx,p in enumerate(plus[::-1]):
    sum_plus += p
    if sum_plus>=-sum_minus:
      print(idx+1+len(minus))
      break
  else:
    print(-1)

Submission status: Accepted

Solution code:
#temprate
from sys import setrecursionlimit
setrecursionlimit(10**6)
from collections import Counter
def inputlist(): return [int(j) for j in input().split()]
#temprate
N = int(input())
A = inputlist()
B = inputlist()
C = [A[i]-B[i] for i in range(N)]
s = sum(C)
if s < 0:
    print(-1)
    exit()
C.sort()
ans = 0
for i in range(N):
    if C[i] <0:
        pass
    elif C[i] <= s:
        ans +=1
        s -= C[i]
    else:
        break
print(N-ans)

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

les = 0
ans = 0
Over = []
for i in range(N) :
    Over.append(A[i]-B[i])
    if A[i] < B[i] :
        les += B[i] - A[i]
        ans += 1

Over.sort(reverse = True)
for ov in Over :
    if les <= 0 :
        print(ans)
        break
    les -= ov
    ans += 1

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [ai - bi for ai, bi in zip(a, b)]
if sum(c) < 0:
    print(-1)
    exit()
c.sort()
minus = deque()
for ci in c:
    if ci >= 0:
        break
    minus.append(ci)
ans = len(minus)
s = sum(minus) * (-1)
plus = 0
idx = -1
while s > plus:
    plus += c[idx]
    ans += 1
    idx -= 1
print(ans)