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:
#!/usr/bin/env python3
#ABC140 E
#類題 AGC5 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
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
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = LI()
c = sorted(a)
d = sorted(b)
lst = []
s = 0
ans = 0
for i in range(n):
    if a[i] < b[i]:
        s += b[i] - a[i]
        ans += 1
    else:
        lst.append(a[i]-b[i])
lst.sort(reverse=1)
for i in lst:
    if s > 0:
        s -= i
        ans += 1
    else:
        break
if s > 0:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np

def main():
    n = int(input())
    junbi_num = list(map(int, input().split()))
    exam_num = list(map(int, input().split()))

    min = -1

    if sum(junbi_num) < sum(exam_num):
        print(min)
        exit()

    sabun_list = np.array(junbi_num) - np.array(exam_num)

    min = np.count_nonzero(sabun_list < 0)
    
    plus_list = sabun_list[sabun_list > 0]
    plus_list = np.sort(plus_list)[::-1]

    minus = sum(sabun_list[sabun_list < 0])

    i = 0
    while(minus < 0):
        minus += plus_list[i]
        i += 1
        min += 1

    print(min)

if __name__ == '__main__':
    main()

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)]
s = sum(c)
if s < 0:
    print(-1)
    exit(0)
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 = list(map(int, input().split()))
b = list(map(int, input().split()))

if sum(a) < sum(b):
    print(-1)
    exit()
else:
    sub = []
    sub = [0 for i in range(n)]
    for i in range(n):
        sub[i] = a[i] - b[i]
    ans = 0
    sub.sort()
    pool = 0
    index = 0
    k = -1
    while sub[index] < 0:
        ans += 1
        while sub[index] + pool < 0:
            pool += sub[k]
            sub[k] = 0
            k -= 1
            ans += 1
        pool += sub[index]
        index += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
s=sum(A)-sum(B)
if s<0:
    print(-1)
    sys.exit()
D=[]
inf=float("inf")
for i in range(N):
    num=A[i]-B[i]
    if num<0:
        D.append(inf)
    else:
        D.append(num)
D.sort()
cnt=0
for i in range(N):
    s-=D[i]
    if s>=0:
        cnt+=1
    else:
        break
print(N-cnt)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    B=LI()
    amari = []
    shortage = 0
    ans=0
    for a,b in zip(A,B):
        if a<b:
            shortage+=b-a
            ans+=1
        else:
            amari.append(a-b)
    if shortage==0:
        print(0)
        return
    amari.sort(reverse=True)
    for a in amari:
        shortage -= a
        ans += 1
        if shortage<=0:
            print(ans)
            return 

    print(-1)

if __name__ == '__main__':
    main()

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

  c=[]
  for i in range(n):
    c.append(a[i]-b[i])

  c.sort()
  cnt=0
  sm=0

  for x in c:
    if x<0:
      cnt+=1
      sm+=x

  for x in c[::-1]:
    if sm>=0:
      return cnt
    if x>0:
      cnt+=1
      sm+=x

  return -1

# main()
print(main())

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.readline

N = int(read())
A = tuple(map(int, read().split()))
B = tuple(map(int, read().split()))

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()
elif sum(d) < 0:
    print(-1)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.readline

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

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()
elif sum(d) < 0:
    print(-1)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.readline

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

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.readline

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

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

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below -= i
        n += 1
    else:
        break

tmp = 0
for i in d[::-1]:
    tmp += i
    n += 1
    if tmp > below:
        print(n)
        exit()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.readline

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

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

d = [A[i] - B[i] for i in range(N)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below -= i
        n += 1
    else:
        break

tmp = 0
for i in d[::-1]:
    tmp += i
    n += 1
    if tmp > below:
        print(n)
        exit()

Submission status: Accepted

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

diff.sort()

for i in range(N):
    if need<=0:
        break
    if diff[i]>0:
        ans=-1
        break
    need+=diff[i]
    ans+=1
    
print(ans)

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()
husai=0
genkai=0
if sum(C)<0:
    print(-1)
else:
    for i in range(n):
        if C[i]<0:
            husai+=C[i]
        else:
            genkai=i
            break
    if husai==0:
        genkai2=0
    else:
        for j in range(1,n+1):
            husai+=C[-j]
            if husai>=0:
                genkai2=j
                break
    print(genkai+genkai2)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
S=[A[i]-B[i] for i in range(N)]
S.sort()
if S[0]>=0:
  print(0)
  exit()
if S[-1]<0:
  print(-1)
  exit()
  
x=0
y=N-1
while y-x>1:
  if S[(y+x)//2]<0:
    x=(y+x)//2
  else:
    y=(y+x)//2

minus=-sum(S[:y])

z=N-1
s=S[N-1]
while z>y and s<minus:
  z-=1
  s+=S[z]
  
if s<minus:
  print(-1)
else:
  print(N-(z-y))

Submission status: Accepted

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

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

if dsm < 0:
    print(-1)
else:
    dif.sort(reverse=True)
    i = 0
    while tmp > 0:
        tmp -= dif[i]
        i += 1
        ans += 1
    print(ans)

Submission status: Accepted

Solution code:
# C 
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n=ni()
a=nl()
b=nl()
c=[]
d=[]
for i in range(n):
    if a[i]-b[i]<0:
        c.append(a[i]-b[i])
    elif a[i]-b[i]>0:
        d.append(a[i]-b[i])

if len(c)==0 and len(d)>=0:
    print(0)
    exit()

if len(c)>0 and len(d)==0:
    print(-1)
    exit()

d.sort(reverse=True)
sum_c=sum(c)
i=0
while sum_c<0 and i<len(d):
    sum_c+=d[i]
    i+=1

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

print(i+len(c))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
up = []
down = []
down_sum = 0
for i,j in zip(A,B):
    if i - j > 0:
        up.append(i - j)
    elif i - j < 0:
        down.append(j - i)
        down_sum += j - i
if down_sum == 0:
    ans = 0
else:
    up_sum = 0
    ans = len(down)
    up.sort(reverse=True)
    for i in up:
        up_sum += i
        ans += 1
        if up_sum >= down_sum:
            break
    if up_sum < down_sum:
        ans = -1
print(ans)

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p=[]
m=[]
for i,j in zip(a,b):
    k=i-j
    if k < 0:
        m.append(k)
    else:
        p.append(k)
if sum(p)+sum(m) < 0:
    print(-1)
    sys.exit()
p.sort(reverse=True)
ans=0
P=0
M=abs(sum(m))

for i in range(n):
    if P >= M:
        print(ans+len(m))
        sys.exit()
    P+=p[i]
    ans+=1

Submission status: Accepted

Solution code:
import sys
N = int(input())

A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]

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

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

minus = []
plus = []
for i in range(0,N):
    if gap[i]<0:
        minus.append(gap[i])

    else:
        plus.append(gap[i])

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

summinus= sum(minus)

plus.sort(reverse=True)

j = 0
while True:
    summinus += plus[j]
    if summinus >= 0:
        print(N-len(plus)+j+1)
        break

    else:
        j += 1

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(b) > sum(a):
    print(-1)
    exit()

d = [a[i]-b[i] for i in range(n)]
d = sorted(d)

minus_sum = 0
minus_count = 0
for i in range(n):
    if d[i] < 0:
        minus_sum += d[i]
        minus_count += 1

ans = 0
for i in range(n)[::-1]:
    if minus_sum >= 0:
        print(ans+minus_count)
        exit()
    minus_sum += d[i]
    ans += 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(0)
t=sum(a)-sum(b)
l=[]
for i in range(n):
  if a[i] >= b[i]:
    l.append(a[i]-b[i])
l.sort()
ans=0
for x in l:
  t-=x
  if t<0:
    break
  ans+=1
print(n-ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
OV = [a-b for a, b in zip(A, B) if a-b>0]
OV.sort(reverse = True)
UD = [a-b for a, b in zip(A, B) if a-b<0]
ans = len(UD)
if ans != 0:
  OVC = [sum(UD)]*(len(OV)+1)
  for i in range(1,len(OV)+1):
    OVC[i]=OVC[i-1]+OV[i-1]
    if OVC[i]>=0:
      ans += i
      OVC[-1]=0
      break
  if OVC[-1]<0:
    print(-1)
  else:
    print(ans)
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)
	    exit()
c= 0
for i in range(n):
	    if a[i]>=b[i]:c+=1
if c == n:
	    print(0)
	    exit()
chcnt = 0
needpoint = 0
for i in range(n):
	    a[i]-=b[i]
	    if a[i]<0:
	    	    chcnt+=1
	    	    needpoint-=a[i]
a.sort(reverse = True)
if a[0]== 0:
	    print(0)
	    exit()
givepoint = 0
for i in range(n):
	    givepoint += a[i]
	    if givepoint >= needpoint:
	    	    chcnt+=1
	    	    break
	    chcnt+=1
print(chcnt)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    under, over = [], []
    for i in range(N):
        d = A[i] - B[i]
        if d < 0:
            under.append(d)
        elif 0 < d:
            over.append(d)
    over.sort(reverse=True)
    
    ans = len(under)
    lack = -sum(under)
    for i in range(len(over)):
        if lack <= 0:
            break
        lack -= over[i]
        ans += 1
    
    if 0 < lack:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

for i in range(n):
    if e>=d:
        break
    else:
        e+=c[i]
        ans+=1
if not flag:
    print(ans)

Submission status: Accepted

Solution code:
import heapq
import sys

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

short = 0
marge = []
ans = 0
for i in range(n):
	dif = a[i] - b[i]
	if dif < 0:
		ans += 1
		short += dif
	else:
		heapq.heappush(marge,dif*(-1))

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

while marge != []:
	add = heapq.heappop(marge)*(-1)
	ans += 1
	short += add
	if short >= 0:
		print(ans)
		sys.exit()

print(-1)

Submission status: Accepted

Solution code:
import math
import bisect
import collections
import itertools
import heapq
import string
import sys
sys.setrecursionlimit(10**9)
def gcd(a,b):return math.gcd #最大公約数
def lcm(a,b):return (a*b) // math.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n + 1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def sieve_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N = iin()
A = iln()
B = iln()
A_sum = sum(A)
B_sum = sum(B)

if A_sum < B_sum:
    print(-1)
    exit()

cnt = 0
minus = 0
plus = []
for i in range(N):
    diff = A[i] - B[i]
    if diff < 0:
        cnt += 1
        minus += abs(diff) 
    elif diff > 0:
        plus.append(diff)
plus.sort(reverse=True)
if minus == 0:
    print('0')
else:
    for i in range(len(plus)):
        minus -= plus[i]
        cnt += 1
        if minus <= 0:
            break
    print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, A: "List[int]", B: "List[int]"):

    enough = []
    husoku = 0
    counter = 0
    for a, b in zip(A, B):
        if a < b:
            husoku += b-a
            counter += 1
        else:
            enough.append(a-b)
    enough.sort(reverse=True)
    i = 0
    while husoku > 0 and i < len(enough):
        husoku -= enough[i]
        counter += 1
        i += 1

    if husoku > 0:
        print(-1)
    else:
        print(counter)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, B)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

X = sorted([a-b for a, b in zip(A,B)])
Y = [x for x in X if x<0]
ans = len(Y)
Z = sum(Y)

for x in reversed(X):
    if Z>=0: break
    ans += 1
    Z += x

print(ans if Z>=0 else -1)

Submission status: Accepted

Solution code:
N = int(input())
a = [i for i in map(int,input().split())]
b = [i for i in map(int,input().split())]
diff = [a[i] - b[i] for i in range(N)]
diff_origin = [a[i] - b[i] for i in range(N)]
if sum(a) < sum(b):
  print("-1")
else:
  diff.sort()
  diff_origin.sort()
  ans = 0
  upper = len(diff)-1
  lower = 0
  while True:
    if diff[lower] >= 0:
      break
    if abs(diff[upper]) == abs(diff[lower]):
      diff[upper] = 0
      diff[lower] = 0
      upper -= 1
      lower += 1
    elif abs(diff[upper]) < abs(diff[lower]):
      diff[lower] += diff[upper]
      diff[upper] = 0
      upper -= 1
    else:
      diff[upper] += diff[lower]
      diff[lower] = 0
      lower += 1
  for i in range(len(diff)):
    if diff[i] != diff_origin[i]:
      ans += 1
  print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
d=[]
s=0
for i in range(n):
  if a[i]-b[i]>0:
    c.append(a[i]-b[i])
  if a[i]-b[i]<0:
    d.append(a[i]-b[i])
  s+=a[i]-b[i]
c.sort(reverse=True)
if s<0:
  print(-1)
elif len(d)==0:
  print(0)
else:
  ans=len(d)
  i=0
  while len(d)>0:
    if c[i]>=abs(d[0]):
      c[i]-=abs(d[0])
      d.remove(d[0])
    else:
      d[0]+=c[i]
      i+=1
  print(ans+i+1)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
#A = list(map(int,input().split()))
A = np.array(list(map(int,input().split())))
B = np.array(list(map(int,input().split())))
if sum(A) < sum(B):
    print(-1)
    exit()
d = A-B
sd = np.sort(d)[::-1]
negsum = np.sum(sd[sd < 0])
negcnt = np.sum(sd < 0)
cnt = 0
for i in sd:
    if i <= 0:
        break
    if negsum >= 0:
        break
    negsum += i
    cnt += 1
ans = negcnt + cnt
print(ans)

Submission status: Accepted

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

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

diff = 0
ct = 0
for i in range(n):
  if b[i] > a[i]:
    ct += 1
    diff += b[i] - a[i]

while diff > 0:
  ct += 1
  diff = diff - d[-1]
  del d[-1]

print(ct)

Submission status: Accepted

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

li = []
li2 = []

for i in range(n):
    if a[i] > b[i]:
        li.append(a[i] - b[i])
    elif a[i] < b[i]:
        li2.append(b[i] - a[i])

li.sort(reverse = True)
sum1 = sum(li2)

tmp = 0
count = 0

while tmp < sum1:
    if count < len(li):
        tmp += li[count]
        count += 1
    else:
        print(-1)
        exit()

print(count + len(li2))

Submission status: Accepted

Solution code:
from sys import exit, stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
B = [int(_) for _ in stdin.readline().rstrip().split()]
if (sum(A) < sum(B)):
    print(-1)
else:
    ans = 0
    diff = [a-b for a, b in zip(A,B)]
    plus = sorted([d for d in diff if d > 0])
    minus = [d for d in diff if d < 0]
    smms = sum(minus)
    while smms < 0:
        smms += plus.pop()
        ans += 1
    print(ans + len(minus))

Submission status: Accepted

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

rest=[]
minussum=0
minuscnt=0
for i in range(N):
  diff=A[i]-B[i]
  if diff<0:
    minussum+=diff*(-1)
    minuscnt+=1
  elif diff==0:
    continue
  else:
    rest.append(diff)

rest=sorted(rest)[::-1]
if minussum>sum(rest):
  print(-1)
  exit(0)
if minuscnt==0:
  print(0)
  exit(0)

ind=0
while minussum>0:
  if minussum>rest[ind]:
    minussum-=rest[ind]
    ind+=1
  else:
    break
print(minuscnt+ind+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
difAB = [a-b for a,b in zip(A,B)]
sum_difAB = sum(difAB)
if sum_difAB<0:
  print(-1)
elif sum_difAB == 0:
  print(N)
else:
  difAB.sort()
  neg_num = 0
  neg_sum = 0
  for dif in difAB:
    if dif < 0:
      neg_num += 1
      neg_sum += dif
    else:
      break
  
  pos_num = 0
  pos_sum = 0
  for dif in difAB[::-1]:
    if pos_sum + neg_sum < 0:
      pos_num += 1
      pos_sum += dif
    else:
      break
  print(neg_num+pos_num)
  print()

Submission status: Accepted

Solution code:
import sys

N = int(input())

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

up = []
dsum = 0
dnum = 0

for i in range(N):

    if A[i] > B[i]:
        up.append(A[i] - B[i])
    elif B[i] > A[i]:
        dsum += (B[i] - A[i])
        dnum += 1

up.sort()
up.reverse()

if sum(up) < dsum:
    print (-1)
    sys.exit()
elif dnum == 0:
    print (0)
    sys.exit()

usum = 0

for i in range(len(up)):

    usum += up[i]

    if usum >= dsum:
        print (dnum + i + 1)
        sys.exit()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
plus = []
total_minus = 0
for i in range(n):
    diff = a[i] - b[i]
    if diff >= 0:
        plus.append(diff)
    else:
        total_minus += (-1) * diff
plus.sort(reverse=True)
if total_minus == 0:
    print(0)
elif sum(plus) < total_minus:
    print(-1)
else:
    ans = n - len(plus)
    for i in range(n):
        if total_minus <= 0:
            break
        total_minus -= plus[i]
        ans += 1
    print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
plus = []
minus = []
for i in range(n):
    diff = a[i] - b[i]
    if diff >= 0:
        plus.append(diff)
    else:
        minus.append((-1) * diff)
plus.sort(reverse=True)
plus = collections.deque(plus)
if len(minus) == 0:
    print(0)
    exit()
if sum(plus) < sum(minus):
    print(-1)
    exit()
ans = len(minus)
total = sum(minus)
while total > 0:
    total -= plus.popleft()
    ans += 1
print(ans)

Submission status: Accepted

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

minus = 0
cnt = 0
for i, j in enumerate(zip(A, B)):
    d = j[0] - j[1]
    D[i] = d
    if d < 0:
        minus += d
        cnt += 1

if minus >= 0:
    print(0)
    exit()

D.sort(reverse=True)
for idx, d in enumerate(D):
    if d > 0:
        minus = minus + d
        if minus >= 0:
            print(idx + 1 + cnt)
            exit()
    else:
        break

print(-1)

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)]
c.sort()
chsum = 0
ans = 0
if sum(c)<0:
  print(-1)
  exit()
for i in range(n):
  if c[i] < 0:
    chsum -= c[i]
    c[i] = 0
    ans += 1
for i in range(n-1,-1,-1):
  if chsum > 0:
    chsum -= c[i]
    c[i] -= (min(chsum,c[i]))
    ans += 1
print(ans)

Submission status: Accepted

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

suf = []
insuf = []
S1 = 0
S2 = 0

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

if len(insuf) == 0:
    print(0)
    exit()
else:
    S1 = sum(insuf)
    suf.sort(reverse=True)
    for i in range(len(suf)):
        S2 += suf[i]
        if S2 >= S1:
            ans = len(insuf) + i + 1
            break

    if S2 < S1:
        ans = -1

    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
S = 0
cnt = 0
for i in range(N):
    if a[i] < b[i]:
        S += (b[i] - a[i])
        cnt += 1
if S == 0:
    print(0)
    exit()
T = 0
sumlst = []
for i in range(N):
    if a[i] >= b[i]:
        sumlst.append(a[i] - b[i])

sumlst.sort(reverse=True)
for i in sumlst:
    T += i
    cnt += 1
    if T >= S:
        break
if S > T:
    print(-1)
    exit()
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.buffer.readline

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

c = a - b
m = np.sum(c[c < 0])
p = np.sort(c[c >= 0])[::-1]
cnt = n - p.size

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

for i in p:
    m += i
    cnt += 1
    if m >= 0:
        print(cnt)
        exit()
print(-1)

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    D = []
    short = 0
    count = 0
    for i in range(N):
        now = A[i]-B[i]
        D.append(now)
        if now < 0:
            short += now
            count += 1

    if sum(D)<0:
        print(-1)
        exit()
    
    D.sort(reverse=True)
    
    for i in range(N):
        if short>=0:
            print(count)
            exit()    
        short += D[i]
        count += 1
    print(N)
    

if __name__ == "__main__":
    main()

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 :
  l = []
  ret = 0
  rest = 0
  for a, b in zip(A, B) :
    if a <= b :
      if a != b :
        ret += 1
      rest += b - a
    else :
      l.append(a - b)

  l.sort()
  while rest > 0 :
    ret += 1
    rest -= l.pop()

  print(ret)

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
ans=0
m=0
p=0
pl=deque()
for a,b in zip(al,bl):
    if a<b:
        m+=b-a
        ans+=1
    else:
        p+=a-b
        pl.append(a-b)
if m==0:
    print(0)
    exit()
if m>p:
    print(-1)
    exit()
pl=sorted(pl)
t=0
while t<m:
    t+=pl.pop()
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect
n = int(input())
d = []
c = 0
co = 0
for i,j in zip(map(int,input().split()),map(int,input().split())):
  if i - j > 0:
    d.append(i-j)
  elif i - j < 0:
    c += i-j
    co += 1
d.sort(reverse=1)
if sum(d) + c < 0:
  print(-1)
elif c == 0:
  print(0)
else:
  d = list(accumulate(d))
  print(bisect.bisect_left(d,-c)+co+1)