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()))
from heapq import heappop,heappush
q=[]
d=0
r=0
for i in range(N):
    if A[i]<B[i]:
        r+=1
        d+=B[i]-A[i]
    elif A[i]>B[i]:
        heappush(q,B[i]-A[i])
while d>0 and len(q)>0:
    d+=heappop(q)
    r+=1
print(-1 if d>0 else r)

Submission status: Accepted

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

ans = 0
msize = 0
psize = 0
minus ,plus =0,0
if sum(A)<sum(B):
   ans =-1
else:
   C = np.array(A)-np.array(B)
   minus = len([n for n in C if n <0])
   plusnum =sorted([n for n in C if n >0],reverse=True)
   msize =sum([ n for n in C if n<0])

   if minus==0:
      ans =0
   else:
      for i in range(len(plusnum)):
         psize =psize+ plusnum[i]

         if psize>=-msize:
            plus =i+1
            break
      ans = minus+plus



print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[-1]*N
for i in range(N):
    C[i]=A[i]-B[i]
C.sort()
 
count=0
num=0
#print(C)
 
if (sum(C))<0:
    print(-1)
else:
    while C[0]<0:
        count+=1
        num+=C.pop(0)    
        if num<0:
            num+=C.pop(-1)
            count+=1
        if len(C)==0:
            break
    print(count)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

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

def main():
  n=I()
  l1=LI()
  l2=LI()

  sm1=sum(l1)
  sm2=sum(l2)

  if sm1<sm2:
    return -1

  a=[]
  b=[]
  for i,x in enumerate(l1):
    y=l2[i]
    if x<y:
      a.append(y-x)
    elif x>y:
      b.append(x-y)
  b.sort()

  if len(a)==0:
    return 0

  # print(a)
  # print(b)

  ans=len(a)
  _lenb=len(b)
  _a=a.pop()
  _b=b.pop()
  while True:
    if _b>=_a:
      _b-=_a
      if len(a)<=0:
        break
      else:
        _a=a.pop()

    else:
      _a-=_b
      _b=b.pop()

  return ans+_lenb-len(b)

# main()
print(main())

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
diff=[]
d=0
ans=0
for i in range(N):
  if A[i]<B[i]:
    d+=B[i]-A[i]
    ans+=1
  elif A[i]>B[i]:
    diff.append(A[i]-B[i])

diff=sorted(diff,reverse=True)
diff.append(0)

#print(d)

f=False
for i in range(len(diff)):
  if d<=0:
    ans+=i
    f=True
    break
  d-=diff[i]
  
  

if f:
  print(ans)
else:
  print(-1)
#print(diff)

Submission status: Accepted

Solution code:
n,*d=map(int,open(0).read().split())
f=[d[i]-d[n+i]for i in range(n)]
m=[v for v in f if v<0]
p=sorted(v for v in f if v>0)
c=len(m);s=sum(m)
while p and s<0:s+=p.pop();c+=1
print(-1if s<0else c)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
ans = 0
Y = 0
D = []

if sum(A) < sum(B):
  print(-1)
  exit()
  
for i in range(N):
  heappush(D,-(A[i]-B[i]))
  if A[i] < B[i]:
    Y += B[i]-A[i]
    ans += 1

while Y > 0:
  d = -1*heappop(D)
  Y -= d
  ans += 1
print(ans)

Submission status: Accepted

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


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ca, cb = [], []
    for i in range(n):
        if b[i] - a[i] > 0:
            ca.append(b[i] - a[i])
        elif b[i] - a[i] < 0:
            cb.append(-b[i] + a[i])

    if len(ca) == n:
        print(0)
        exit()

    cb = sorted(cb, reverse=True)
    ans = 0
    casum = sum(ca)
    for i in range(len(cb)):
        if casum <= 0:
            break
        casum -= cb[i]
        ans += 1

    if casum > 0:
        print(-1)
        exit()

    print(ans + len(ca))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import insort, bisect_left
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

ans = 10**12
flg = False


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = []
    ca, cb = [], []
    for i in range(n):
        c.append(b[i] - a[i])
        if b[i] - a[i] > 0:
            ca.append(b[i] - a[i])
        elif b[i] - a[i] < 0:
            cb.append(-b[i] + a[i])
    if len(ca) == n:
        print(0)
        exit()

    cb = sorted(cb, reverse=True)
    ans = 0
    casum = sum(ca)
    #print(c, ca, cb)
    for i in range(1, len(cb)+1):
        if casum <= 0:
            break
        casum -= cb[i - 1]
        ans += 1
    if casum > 0:
        print(-1)
        exit()
    print(ans + len(ca))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
#Ai<Biを満たすものを数える、このときsum(Ai)<sum(Bi)であれば、Ai>BiのもののうちAi-Biが最大のものを探し出して
#sum(Ai)+Ai-bi >= sum(Bi) になるか確かめる
smallA = []
equalA = []
largeA = []

for i,a in enumerate(A):
    if a < B[i]:
        smallA.append(B[i] - a)#差を入れる
    elif a == B[i]:
        equalA.append(a)
    else:
        largeA.append(a - B[i])#差を入れる

if smallA == []:
    print(0)
else:
    sumsmallA = sum(smallA)
    largeA.sort(reverse=True)#降順
    c = 0
    enable = True
    count = len(smallA)

    while True:
        if sumsmallA - c > 0:
            if len(largeA) > 0:
                c += largeA.pop(0)
                count+=1
            else:
                enable=False
                break
        else:
            break
    if enable==True:
        print(count)
    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:
    P = []
    m, ans = 0, 0
    for a, b in zip(A, B):
        if a > b:
            P.append(a-b)
        elif a < b:
            m += (b-a)
            ans += 1
    P = sorted(P, reverse=True)
    if ans > 0:
        for p in P:
            m -= p
            ans += 1
            if m <= 0:
                break
    print(ans)

Submission status: Accepted

Solution code:
# keyence2019C - Exam and Wizard
def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, shortage, pos = 0, 0, []
    for i, j in zip(A, B):
        x = i - j
        if x >= 0:
            pos += [x]
        else:
            ans += 1
            shortage += x
    if sum(pos) + shortage < 0:
        print(-1)
        return
    for i in sorted(pos, reverse=1):
        if shortage >= 0:
            break
        ans += 1
        shortage += i
    print(ans if shortage >= 0 else -1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# keyence2019C - Exam and Wizard
def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, shortage, pos = 0, 0, []
    for i, j in zip(A, B):
        x = i - j
        if x >= 0:
            pos += [x]
        else:
            ans += 1
            shortage += x
    for i in sorted(pos, reverse=1):
        if shortage >= 0:
            break
        ans += 1
        shortage += i
    print(ans if shortage >= 0 else -1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

import numpy as np

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

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

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

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

print(answer)

Submission status: Accepted

Solution code:
from collections import defaultdict
import heapq

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

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

print(ans)

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)],reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#キーエンス C

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

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

from bisect import bisect_left
from itertools import accumulate

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

a_larger = []
lack = 0
ans = 0

# ai > bi のものを降順にならべ、累積和
# ai < bi のものは総和
for ai, bi in zip(a, b):
    if ai > bi:
        a_larger.append(ai - bi)
    elif ai < bi:
        lack += (bi - ai)
        ans += 1

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

# 二分探索
idx = bisect_left(a_larger_cum, lack)
if idx >= len(a_larger_cum):
    print(-1)
else:
    print(ans + idx)

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

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

# print (A) #しないといけない準備量　負 = 準備が足りない 正 = すでに十分準備している
A.sort()
# print (A)

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

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

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


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

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



r  = sorted(sa[:idx], reverse=True)
#print("ans: ", ans)
#print("tmp",tmp)
#print(r)
for i in range(idx):
    if tmp > r[i]:
        ans += 1
        tmp -= r[i]
    else:
        ans += 1
        #print("i= ",i)
        break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
As = list(map(int,input().split(" ")))
Bs = list(map(int,input().split(" ")))
over = []
under = []
for a,b in zip(As,Bs):
    if a < b:
        under.append(b-a)
    if a > b:
        over.append(a-b) #1個
#overの要素を幾つ使えばunderを包めるか
if len(under) == 0:
    print(0)
    exit()

over.sort(reverse=True)
under_sum = sum(under)
l = len(over)
for i in range(l):
    under_sum -= over[i]
    if under_sum  <= 0:
        break
if under_sum  > 0:
    print(-1)
else:
    print(i+ 1 + len(under)) #これは少なくとも全部書き換える
# +overで使った分の長さ

Submission status: Accepted

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

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

Submission status: Accepted

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

BA = [b-a for a, b in zip(A, B) if b > a]
AB = sorted([a-b for a, b in zip(A, B) if a > b], reverse=True)
if len(BA) == 0:
  print(0)
else:
  S = sum(BA)
  Sd = 0
  for i, ab in enumerate(AB):
    Sd += ab
    if Sd >= S:
      break
  if Sd >= S:
    print(len(BA)+i+1)
  else:
    print(-1)

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())))
c=a-b
c.sort()
g=0
p=-1
for i in range(n):
    if c[i]<0:
        g+=c[i]
    else:
        break
if g==0:
    print(0)
else:
    for j in range(1,n+1):
        if g>=0:
            p=0
            break
        if c[-j]>0:
            g+=c[-j]
        else:
            break
    if p==-1:
        print(-1)
    else:
        print(i+j-1)

Submission status: Accepted

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

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

offset.sort()

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

difn.sort()

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

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

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

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

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

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

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

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

diffs = A - B

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mp = 0
ans = 0
l = []
for i in range(n):
    if a[i] < b[i]:
        mp -= b[i] - a[i]
    else:
        ans += 1
        l.append(a[i] - b[i])
l.sort(reverse=True)
for p in l:
    if mp == 0:
        break
    mp += min(p, -mp)
    ans -= 1
if mp < 0:
    print(-1)
else:
    print(n - ans)

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 = sorted([a - b for a, b in zip(A, B)], reverse=True)
D_orig = D[:]
l = 0
r = N - 1
while D[r] < 0:
    if D[l] + D[r] >= 0:
        D[l] += D[r]
        D[r] = 0
        r -= 1
    else:
        D[r] += D[l]
        D[l] = 0
        l += 1
print(N - sum([d == o for d, o in zip(D, D_orig)]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from bisect import*
from numpy import*
n=int(input())
*a,=map(int,input().split())
*b,=map(int,input().split())
x=array(a)-array(b)
y=x[x<0]
z=sorted(x[x>0],reverse=1)
#print(y,z)
if len(y)==0:
    print(0)
elif sum(y)+sum(z)<0:
    print(-1)
else:
    ans=len(y)
    ans+=bisect_left(cumsum(z),-sum(y))+1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(solve(A,B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Exam and Wizard
https://atcoder.jp/contests/keyence2019/tasks/keyence2019_c

"""
import sys


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


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


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

Submission status: Accepted

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

d = []

p = 0
m = 0

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

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

d.sort()
d.reverse()

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

Submission status: Accepted

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

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 = []
s = 0
cnt = 0
for i in range(n):
    if a[i]-b[i]>=0:
        c.append(a[i]-b[i])
    else:
        s+= a[i]-b[i]
        cnt+=1
c = sorted(c)[::-1]
for i in c:
    if s>=0:
        break
    s+=i
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[]
count,ans=0,0
for i in range(n):
  if A[i]<B[i]:
    count+=B[i]-A[i]
    ans+=1
  elif A[i]>B[i]:
    C.append(A[i]-B[i])
C.sort(reverse=True)
c=0
while count>0 and c<len(C):
  count-=C[c]
  c+=1
if count<=0:
  print(ans+c)
else:
  print(-1)