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: Wrong Answer

Solution code:
def main():
  n = int(input())
  a = input_list()
  b = input_list()
  
  change_count = 0
  p = 0
  shorts = 0
  amari = []
  for i, aa in enumerate(a):
    bb = b[i]
    if aa > bb:
      amari.append(aa - bb)
    elif bb > aa:
      shorts += bb - aa
      change_count += 1
      
  if shorts == 0:
    print(0)
    exit(0)
  for am in amari:
    shorts -= am
    change_count += 1
    if shorts <= 0:
      break
  if shorts > 0:
    print(-1)
    exit(0)
  print(change_count)
    
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

ans, cost, mana = 0, 0, []
for i in range(N):
    if A[i] >= B[i]:
        mana.append(A[i] - B[i])
    else:
        cost += B[i] - A[i]
        ans += 1

if cost >= sum(mana):
    print(-1)
else:
    mana.sort(reverse=True)
    ind = 0
    while cost > list(accumulate(mana))[ind]:
        ind += 1
    print(ans + ind + 1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import bisect
from itertools import accumulate
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

ans = 0
tmp = 0
tmp_loss = 0
for i in range(N):
    if A[i] >= B[i]:
        tmp += A[i] - B[i]

Flg = True
room = []
#print(tmp)
for i in range(N):
    if A[i] < B[i]:
        if tmp >= (B[i]- A[i]):
            ans += 1
            tmp -= (B[i]- A[i])
            tmp_loss += ( B[i] - A[i] )
            room.append(B[i]- A[i])
        else:
            Flg = False
            break
room.sort(reverse = True)
#print(room)
R = list(accumulate(room))
a = bisect.bisect_left(R,tmp_loss)
#print(a,R)
if a == 0:
    ans += 0
else:
    ans += a
    
if Flg:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

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

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

ans=0
difAB=A-B
negdif=difAB[difAB<0]
posdif=sorted(difAB[difAB>0], reverse=True)
ans+=len(negdif)
needs=sum(negdif)
i=0
while needs<0:
    needs+=posdif[i]
    ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
cnt = 0
if sum(a) < sum(b):
    print(-1)
    exit()
diff = []
for i in range(n):
    diff.append(a[i] - b[i])
diff.sort()
stack = 0
stackcnt = 0
for i in range(n):
    # print(i, stack, diff[i])
    if diff[i] >= 0:
        continue
    if stack < -diff[i]:
        cnt += 1
        stack += diff[-cnt]
        a[-cnt] = b[i]
    cnt += 1
    stack -= -diff[i]
    a[i] += -diff[i]
print(cnt)

Submission status: Wrong Answer

Solution code:
from math import fabs
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
tarinai = []
amari = []
for i in range(n):
    if a[i] - b[i] < 0:
        tarinai.append(a[i] - b[i])
    elif a[i] - b[i] > 0:
        amari.append(a[i] - b[i])
tarinai.sort()
amari.sort()
amari.reverse()

ans = 0
if fabs(sum(tarinai)) > sum(amari):
    print('-1')
else:
    ans += len(tarinai)
    val = fabs(sum(tarinai))
    for i in range(n+1):
        print(val)
        if val <= 0:
            break
        val -= amari[i]
        ans += 1

    print(ans)

Submission status: Wrong Answer

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()
yobun_sum = []

for i in range(N):
    if a[i] > b[i]:
        yobun_sum.append(a[i]-b[i])

yobun_sum.sort()
yobun_sum.reverse()

yobun_ind = 0
lack_num=0
use_first=False

for i in range(N):
    if a[i] < b[i]:
        use_first=True
        lack_num+=1
        lack = abs(a[i] - b[i])
        if yobun_sum[yobun_ind] >= lack: # can pay
            yobun_sum[yobun_ind] -= lack
        else: # can't pay
            yobun_sum[yobun_ind+1] -= lack - yobun_sum[yobun_ind]
            yobun_sum[yobun_ind] =0
            yobun_ind+=1

if use_first:
    yobun_ind+=1

print(yobun_ind+lack_num)

Submission status: Wrong Answer

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()
yobun_sum = []

for i in range(N):
    if a[i] > b[i]:
        yobun_sum.append(a[i]-b[i])

yobun_sum.sort()
yobun_sum.reverse()

yobun_ind = 0
lack_num=0
use_first=False

for i in range(N):
    if a[i] < b[i]:
        use_first=True
        lack_num+=1
        lack = abs(a[i] - b[i])
        if yobun_sum[yobun_ind] >= lack: # can pay
            yobun_sum[yobun_ind] -= lack
        else: # can't pay
            yobun_sum[yobun_ind+1] -= lack - yobun_sum[yobun_ind]
            yobun_sum[yobun_ind] =0
            yobun_ind+=1

yobun_ind+=1

if use_first and yobun_ind==1:
    yobun_ind+=1

print(yobun_ind+lack_num)

Submission status: Wrong Answer

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()
yobun_sum = []

for i in range(N):
    if a[i] > b[i]:
        yobun_sum.append(a[i]-b[i])

yobun_sum.sort()
yobun_sum.reverse()

yobun_cum_sum=[]
cum_sum=0
for i in yobun_sum:
    cum_sum+=i
    yobun_cum_sum.append(cum_sum)

yobun_ind = 0
lack_num=0
use_first=False

for i in range(N):
    if a[i] < b[i]:
        use_first=True
        lack_num+=1
        lack = abs(a[i] - b[i])
        if yobun_cum_sum[yobun_ind] >= lack: # can pay
            yobun_cum_sum[yobun_ind] -= lack
        else: # can't pay
            yobun_cum_sum[yobun_ind+1] -= lack - yobun_cum_sum[yobun_ind]
            yobun_cum_sum[yobun_ind] =0
            yobun_ind+=1

if use_first:
    yobun_ind+=1

print(yobun_ind+lack_num)

Submission status: Wrong Answer

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()
yobun_sum = []

for i in range(N):
    if a[i] > b[i]:
        yobun_sum.append(a[i]-b[i])

yobun_sum.sort()
yobun_sum.reverse()

yobun_cum_sum=[]
cum_sum=0
for i in yobun_sum:
    cum_sum+=i
    yobun_cum_sum.append(cum_sum)

yobun_ind = 0
lack_num=0
use_first=False

for i in range(N):
    if a[i] < b[i]:
        use_first=True
        lack_num+=1
        lack = abs(a[i] - b[i])
        if yobun_cum_sum[yobun_ind] > lack: # can pay
            yobun_cum_sum[yobun_ind] -= lack
        else: # can't pay
            yobun_cum_sum[yobun_ind+1] -= lack - yobun_cum_sum[yobun_ind]
            yobun_cum_sum[yobun_ind] =0
            yobun_ind+=1

if use_first:
    yobun_ind+=1

print(yobun_ind+lack_num)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import accumulate
import bisect

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))
b = list(map(int,stdin.readline().rstrip().split()))

pra = []
mai = 0
kosuu = 0
atari = 0

for i in range(n):
    if a[i]-b[i] < 0:
        mai += b[i]-a[i]
        kosuu += 1
        compile
    elif a[i]-b[i] > 0:
        pra.append(a[i]-b[i])
    atari += 1

if sum(pra) < mai:
    print(-1)
    exit()
if atari == n:
    print(0)
    exit()
pra.sort(reverse=True)

pra = list(accumulate(pra))
print(bisect.bisect(pra,mai)+kosuu+1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import accumulate
import bisect

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))
b = list(map(int,stdin.readline().rstrip().split()))

pra = []
mai = 0
kosuu = 0
atari = 0

for i in range(n):
    if a[i]-b[i] < 0:
        mai += b[i]-a[i]
        kosuu += 1
    elif a[i]-b[i] > 0:
        pra.append(a[i]-b[i])
        atari += 1

if sum(pra) < mai:
    print(-1)
    exit()
if atari == n:
    print(0)
    exit()
pra.sort(reverse=True)

pra = list(accumulate(pra))
print(bisect.bisect(pra,mai)+kosuu+1)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split(' ')))
B=list(map(int,input().split(' ')))
plus=[]
minus = 0
ans = 0
if sum(A)<sum(B):
    print(-1)
    exit()
for a,b in zip(A,B):
    if a-b >=0:
        plus.append(a-b)
    elif a-b<0:
        minus += a-b
        ans += 1
if minus==0:
    print(0)
    exit()
sorted(plus,reverse=True)
for i in sorted(plus,reverse=True):
    print(i)
    minus += i
    ans += 1
    if minus >=0:
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split(' ')))
B=list(map(int,input().split(' ')))
plus=[]
minus = 0
ans = 0
if sum(A)<sum(B):
    print(-1)
    exit()
for a,b in zip(A,B):
    if a-b >=0:
        plus.append(a-b)
    elif a-b<0:
        minus += a-b
        ans += 1
sorted(plus,reverse=True)
if minus==0:
    print(0)
    exit()
for i in plus:
    minus += i
    ans += 1
    if minus >=0:
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
import numpy as np
from sys import exit
N = int(input())
Alist = np.array(list(map(int, input().split())), dtype=np.int64)
Blist = np.array(list(map(int, input().split())), dtype=np.int64)
sbn = Alist - Blist

if sum(sbn) <= 0:
    print(-1)
    exit()

hu = sbn[sbn < 0]
if len(hu) == 0:
    print(0)
    exit()
ans = len(hu)
cost = sum(hu)

sei = np.sort(sbn[sbn > 0])[::-1].cumsum()
print(ans + (np.argmax(sei >= cost) + 1))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = map(int, input().split())
    B = map(int, input().split())

    sum = 0
    ans = 0
    rest = []
    for a, b in zip(A, B):
        if b > a:
            sum += b - a
            ans += 1
        else:
            rest.append(a - b)
    rest.sort(reverse=True)

    for r in rest:
        sum -= r
        ans += 1
        if sum <= 0:
            break
    if sum > 0:
        print(-1)
    else:
        print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = map(int, input().split())
    B = map(int, input().split())

    sum = 0
    ans = 0
    rest = []
    for a, b in zip(A, B):
        if b > a:
            sum += b - a
            ans += 1
        else:
            rest.append(a - b)
    rest.sort(reverse=True)

    for r in rest:
        sum -= r
        ans += 1
        if sum <= 0:
            break
    if sum > 0:
        print(-a)
    else:
        print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
if sum(A) < sum(B):
    print(-1)
    quit()
C = []
c = 0
for i in range(N):
    if A[i] < B[i]:
        c += B[i] - A[i]
    else:
        C.append(A[i] - B[i])
C.sort()
while c > 0:
    x = C.pop()
    c -= x
print(len(C))

Submission status: Wrong Answer

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)
for i in range(n):
    if a[i] < b[i]:
        break
else:
    print(0)
    quit()
for i in range(n):
    if c[i] < d[i]:
        print(-1)
        quit()

lst = []
for i in range(n):
    r = br(d,c[i])
    lst.append(r)
lst = list(Counter(lst).items())
print(len(lst))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
ans=0
d=0

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

print(ans)

Submission status: Wrong Answer

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: Wrong Answer

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: Wrong Answer

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)
p = 0
for m in minus:
    if p < m:
        p += plus.popleft()
        ans += 1
    p -= m
if p < m:
  ans += 1
print(ans)

Submission status: Wrong Answer

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)
p = 0
for m in minus:
    if p < m:
        p += plus.popleft()
        ans += 1
    p -= m
print(ans)

Submission status: Wrong Answer

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
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: Wrong Answer

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
for i in range(N):
    if a[i] >= b[i]:
        T += a[i] - b[i]
        cnt += 1
    if T >= S:
        break
if S > T:
    print(-1)
    exit()
print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if sum(a) < sum(b):
  print(-1)
else:
  ans = -1

  diff = [ ai - bi for ai, bi in zip(a, b)]

  diff_plus = sorted([di for di in diff if di > 0], reverse=True)
  diff_minus = [di for di in diff if di < 0]
  total_minus = sum(diff_minus)

  for i in range(len(diff_plus) + 1):
    if abs(total_minus) < sum(diff_plus[:i+1]):
      ans = i + 1 + len(diff_minus)
    if ans != -1:
      break

  print(ans)

Submission status: Wrong Answer

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

ans = -1

diff = [ ai - bi for ai, bi in zip(a, b)]

diff_plus = sorted([di for di in diff if di > 0], reverse=True)
diff_minus = [di for di in diff if di < 0]
total_minus = sum(diff_minus)

for i in range(len(diff_plus) + 1):
  if abs(total_minus) < sum(diff_plus[:i+1]):
    ans = i + 1 + len(diff_minus)
  if ans != -1:
    break
    
print(ans)

Submission status: Wrong Answer

Solution code:
# 2019-11-28 00:55:58(JST)
import sys
import numpy as np

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = np.array(sys.stdin.read().split(), dtype=np.int64)
    a = ab[:n]
    b = ab[n:]

    res = np.sort(a - b)
    if res.sum() < -1:
        print(-1)
        sys.exit()

    negative_cnt = np.sum(res < 0)
    if not negative_cnt:
        print(0)
        sys.exit()

    negative_sum = np.sum(res[:negative_cnt])
    positive_cnt = 0
    for i in range(n-1, -1, -1):
        negative_sum += res[i]
        positive_cnt += 1
        if negative_sum >= 0:
            break
    cnt = positive_cnt + negative_cnt

    print(cnt)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
import sys 
N=int(input())
A=np.array([int(i) for i in input().split()])
B=np.array([int(i) for i in input().split()])
C = A-B
minus_idx=np.where(C<0)
plus_idx=np.where(C>=0)
minus = np.sum(C[minus_idx])
plus = deque(np.sort(C[plus_idx]))
len_plus = len(plus)

if len(minus_idx[0]) == 0:
    print(0)
    sys.exit()
if len_plus == 0:
    print(-1)
    sys.exit()
count = 0
while minus<0:
    minus += plus.pop()
    count += 1
    len_plus -= 1
    if len_plus == 0:
        print(-1)
        sys.exit()
print(len(minus_idx[0])+count)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
import sys 
N=int(input())
A=np.array([int(i) for i in input().split()])
B=np.array([int(i) for i in input().split()])
C = A-B
minus_idx=np.where(C<0)
plus_idx=np.where(C>=0)
minus = np.sum(C[minus_idx])
plus = deque(np.sort(C[plus_idx]))
len_plus = len(plus)
if len_plus == 0:
    print(-1)
    sys.exit()
count = 0
while minus<0:
    minus += plus.pop()
    count += 1
    len_plus -= 1
    if len_plus == 0:
        print(-1)
        sys.exit()
print(len(minus_idx[0])+count)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
import sys 
N=int(input())
A=np.array([int(i) for i in input().split()])
B=np.array([int(i) for i in input().split()])
C = A-B
minus_idx=np.where(C<0)
plus_idx=np.where(C>0)
minus = np.sum(C[minus_idx])
plus = deque(np.sort(C[plus_idx]))
len_plus = len(plus)
if len_plus == 0:
    print(-1)
    sys.exit()
count = 0
while minus<0:
    minus += plus.pop()
    count += 1
    len_plus -= 1
    if len_plus == 0:
        print(-1)
        sys.exit()
print(len(minus_idx[0])+count)

Submission status: Wrong Answer

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)]
orderdiff = sorted(diff, reverse=True)

# 浮いている分からマイナスを補填、何個使ったかを管理するためのリスト
plsdiffcnt = [0] * (N + 1)
plsdiffcnt[0] = orderdiff[0]
for i in range(len(orderdiff)-1):
  if i > 0:
    plsdiffcnt[i+1] = plsdiffcnt[i] + orderdiff[i+1]

#マイナス値の合計とその個数を取得
mnscnt = 0
mnssum = 0
for i in diff:
  if i < 0:
    mnscnt += 1
    mnssum -= i
    
if all([i >= 0 for i in diff]):
  print(0)
  exit()
    
if sum(diff) < 0:
  print(-1)
  exit()
     
for i, v in enumerate(plsdiffcnt):
  if mnssum <= v:
    ans = i + 1
    break
print(mnscnt+ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
AB=sorted([i-m for i,m in zip(A,B)])

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

data=[i for i in AB if i<0]
count=len(data)
value=sum(data)
_N=N-1

while value<0:
    print(_N)
    value+=AB[_N]
    count+=1
    _N-=1
print(count)

Submission status: Wrong Answer

Solution code:
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    neg = 0
    pos = []
    for ai, bi in zip(a, b):
        if ai < bi:
            neg += bi - ai
            ans += 1
        else:
            pos.append(ai - bi)
    if neg > sum(pos):
        print(-1)
        return 0
    for pi in pos:
        if neg <= 0:
            break
        neg -= pi
        ans += 1
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

ng = []
po = []

if sum(A) < sum(B): print(-1)
else:
    ans = 0
    for i in range(N):
        d = A[i] - B[i]
        if d < 0:
            ans += 1
            ng.append(d)
        elif d > 0: po.append(d)
    ng.sort()
    po.sort(reverse=True)
    tmp = 0
    while ng:
        if not po:
            print(-1)
            break
        if tmp <= 0:
            tmp += po.pop()
            ans += 1
        while tmp >= 0:
            if not ng: break
            tmp += ng.pop()
    print(ans)

Submission status: Wrong Answer

Solution code:
# 2019/10/22

n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a)<sum(b):
    print(-1)
    exit()
plus=[]
minus=[]
for aa,bb in zip(a,b):
    if aa==bb:continue
    if aa>bb:plus.append(aa-bb)
    else:minus.append(bb-aa)

plus.sort()
minus.sort(reverse=True)
p=0

cnt=len(minus)
while minus:
    m=minus.pop()
    if p<m:
        p+=plus.pop()
        cnt+=1
    p-=m
print(cnt)

Submission status: Wrong Answer

Solution code:
# 2019/10/22

n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a)<sum(b):
    print(-1)
    exit()
cnt=0
plus=[]
minus=[]
for aa,bb in zip(a,b):
    if aa==bb:continue
    if aa>bb:plus.append(aa-bb)
    else:minus.append(bb-aa)

plus.sort()
minus.sort(reverse=True)
p=0
while minus:
    m=minus.pop()
    cnt+=1
    if p<m:
        p+=plus.pop()
        cnt+=1
    p-=m
print(cnt)

Submission status: Wrong Answer

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


magic_power = 0
for a, b in zip(A, B):
    if a > b:
        magic_power += (a-b)

ans = 0

for a, b in zip(A, B):
    if a < b:
        magic_power -= (b-a)
        ans += 1
        if magic_power < 0:
            print(-1)
            exit()

print(ans)

Submission status: Wrong Answer

Solution code:

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

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

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

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

diff.sort()
ans = 0
total = 0
for i in range(N):
	if diff[i] >= 0:
		break
	total += diff[i]
	ans += 1

for i in range(N - 1, -1, -1):
	while total < 0:
		ans += 1
		total += diff[i]

print(ans)

Submission status: Wrong Answer

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

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

idx=-1
for i in range(N):
  while(diff[i]<0):
    if diff[idx]< 0:
      print("-1")
      exit(0)
    if abs(diff[i]) >= diff[idx]:
      diff[i]+=diff[idx]
      diff[idx]=0
      idx-=1
    else:
      diff[idx]+=diff[i]
      diff[i]=0
      
C=[B[i]+diff[i] for i in range(N)]
ans=0
for i in range(N):
  if A[i]!=C[i]:
    ans+=1
print(ans)

Submission status: Wrong Answer

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)

#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: Wrong Answer

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))
    print(c)
    i=0
    while d>0:
        d-=c[i]
        cnt+=1
        i+=1
    print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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()))
c = []
d = []
for i in range(n):
    if a[i] / b[i] < 1:
        c.append([a[i],b[i],a[i]/b[i]])
    else:
        d.append([a[i],b[i],a[i]/b[i]])
c.sort(key = itemgetter(2))
d.sort(key = itemgetter(2),reverse = True)
c = c + d
x = 0
ans = 0
for i in range(n):
    if c[i][0] < c[i][1]:
        x += c[i][1] - c[i][0]
        ans += 1
    else:
        if x > 0:
            x -= min(x,c[i][0]-c[i][1])
            ans += 1
if x != 0 or sum(a) < sum(b):
    print(-1)
else:
    print(ans)