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()))
over = []
short = 0
cnt = 0
for i in range(n):
    c = A[i] - B[i]
    if c > 0:
        over.append(c)
    elif c < 0:
        short -= c
        cnt += 1
if sum(over) < short:
    print(-1)
else:
    over.sort(reverse=True)
    s = 0
    i = 0
    while s < short:
        s += over[i]
        i += 1
    cnt += i
    print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
plus = []
sum = 0
num = 0
for i in range(n):
    diff = A[i] - B[i]
    if diff < 0:
        sum -= diff
        num += 1
    else:
        plus.append(diff)
# ここから まず、引いても全部正だったとき(負がない)つまりnum = 0の時は何もしなくていい
if num == 0:
    print(0)
    exit()
else:
    res = num
    # plusの大きい物順にする
    plus = sorted(plus,reverse=True)
    p = 0
    for i in range(len(plus)):
        p += plus[i]
        res += 1
        if p >= sum:
            break
    if p >= sum:
        print(res)
    else:
        print(-1)

Submission status: Accepted

Solution code:
import bisect

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    if sum(A) < sum(B):
        return -1
    
    C = [a - b for a, b in zip(A, B)]
    C.sort()
    
    mid = bisect.bisect_left(C, 0)
    minus = sum(C[:mid])
    if minus >= 0:
        return 0
    cnt = mid
    for i in reversed(range(N)):
        minus += C[i]
        cnt += 1
        if minus >= 0:
            return cnt
        
    return -1

print(solve())

Submission status: Accepted

Solution code:
import sys
import numpy as np

n = int(input())

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

# 総和が足りなければ合格できない
if a.sum() < b.sum():
    print(-1)
    sys.exit()

diff = a - b

# 初めから条件を満たしている場合
if np.where(diff < 0)[0].shape[0] == 0:
    print(0)
    sys.exit()

# プラス要素とマイナス要素に分離する。0はいじらなくてよい
minus = diff[np.where(diff < 0)]
plus = diff[np.where(diff > 0)]

# マイナスの要素をプラスの成分が大きい方から消化していく
ans = minus.shape[0]
sum_minus = np.abs(minus).sum()
plus.sort()
plus = plus[::-1]
plus = plus.cumsum()

# 何個目のプラスまでで消化しきれるか
ans += np.where(plus >= sum_minus)[0].min() + 1

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys

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

# print(A >= B)
# print(np.sort(A - B))
C = np.sort(A - B)
negative = C[C < 0]
positive = C[C >= 0]
positive = positive[::-1]
# print(negative, positive)
sum_neg = np.sum(negative)
cnt = 0
if len(negative) == 0:
    print(0)
    sys.exit()

for i in range(len(positive)):
    sum_neg += positive[i]
    cnt += 1
    if sum_neg >= 0:
        break

if sum_neg < 0:
    print(-1)
    sys.exit()
else:
    print(cnt + len(negative))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total = 0
ans = 0
free = []
for a, b in zip(A,B):
    if b > a:
        total += b-a
        ans += 1
    else:
        free.append(a-b)
free.sort(key=lambda x: -x)
for i in free:
    if total <= 0:
        break
    total -= i
    ans += 1
if total > 0:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ab=[]
ans=0
tmp=0
if sum(b)>sum(a):
    print(-1)
    sys.exit()
for i in range(n):
    ab.append(a[i]-b[i])
    if a[i]-b[i]<0:
        ans+=1
        tmp+=a[i]-b[i]
ab.sort()
if ans ==0:
    print(ans)
while tmp<0:
    tmp+=ab.pop()
    ans+=1
    if tmp>=0:
        print(ans)
        sys.exit

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


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


def resolve():
    def main():

        N = int(input())
        A = list(map(int, input().split()))
        B = list(map(int, input().split()))
        pluslist = []
        minuscnt = 0
        cnt = 0

        for i in range(N):
            if A[i] < B[i]:
                minuscnt += B[i]-A[i]
                cnt += 1
            elif A[i] > B[i]:
                pluslist.append(A[i]-B[i])
        if minuscnt == 0:
            return 0
        pluslist.sort(reverse=True)
        val = 0
        i = 0
        while True:
            val += pluslist[i]
            i += 1
            if val >= minuscnt:
                return i+cnt
            elif i >= len(pluslist):
                return -1
        return i+1+cnt

    print(main())

resolve()

Submission status: Accepted

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

C = [0]*N

for i in range(N):
  C[i] = A[i] - B[i]
C.sort()
if sum(C) < 0:
  ans = -1
else:
  b = bisect_left(C,0)
  C_minus = C[:b]
  sum_C_minus = sum(C_minus)
  if sum_C_minus == 0:
    ans = 0
  else:
    count = len(C_minus)
    C_plus = C[b:]
    C_plus.sort(reverse = True)
    for j in range(len(C_plus)):
      count += 1
      if abs(sum_C_minus) <= C_plus[j]:
        ans = count
        break
      else: sum_C_minus += C_plus[j]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
diff_po = []
diff_ne = []
if sum(a) < sum(b):
    print(-1)
else:
    ans = 0
    for i in range(n):
        if a[i] - b[i] < 0:
            diff_ne.append(a[i]-b[i])
        elif a[i] - b[i] > 0:
            diff_po.append(a[i]-b[i])
    diff_ne.sort(reverse = True)
    diff_po.sort()
    tmp_po = 0
    while len(diff_ne):
        tmp_ne = diff_ne.pop()
        ans += 1
        while tmp_ne < 0:
            if tmp_po <= 0:
                tmp_po = diff_po.pop()
                ans += 1
            #print(tmp_po,tmp_ne)
            if abs(tmp_po) > abs(tmp_ne):
                tmp_po += tmp_ne
                tmp_ne = 0
            else:
                tmp_ne += tmp_po
                tmp_po = 0
    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()
s_ab = sorted(list(i-j for i,j in zip(A,B)))
s = 0
c = 0
for i in s_ab:
    if i<0:
        s+=i
        c+=1
    else:
        break
s_ab = s_ab[::-1]
for i in s_ab:
    if s<0:
        s+=i
        c+=1
    else:
        break
print(c)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
cnt = 0
need = 0
sup = []
for i in range(n):
    k = B[i] - A[i]
    if k > 0:
        need += k
        cnt += 1
    else:
        sup.append(-k)
sup.sort()
while need > 0:
    if sup:
        need -= sup.pop()
        cnt += 1
    else:
        print(-1)
        break
else:
    print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.stdin.readline

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
sa = []
sa_a = 0
sa_b = []
sa_c = 0
count = 0

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

sa.sort()

if sum(sa) <= 0:
    print(-1)
    sys.exit()
else:
    for i in range(N):
        if sa[i] < 0:
            count += 1
            sa_a += abs(sa[i])
        else:
            sa_b.append(sa[i])

while sa_a > sa_c:
    sa_c += sa_b.pop()
    count += 1

print(count)

Submission status: Accepted

Solution code:
import bisect as bi
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a)<sum(b):print(-1)
else:
    add=[]
    req=0
    ans=0
    for i in range(n):
        if a[i]>b[i]:
            add.append(a[i]-b[i])
        elif a[i]<b[i]:
            req+=b[i]-a[i]
            ans+=1
    add.sort(reverse=True)
    sadd=[0]*(len(add)+1)
    for i in range(len(add)):
        sadd[i+1]=sadd[i]+add[i]
    print(ans+bi.bisect_left(sadd,req))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(hoge) for hoge in input().split()]
B = [int(hoge) for hoge in input().split()]
#Aiの総和を変えずに、なるべく少ない量のAiを変えて全部合格を目指す
if sum(B) > sum(A):
    print(-1)
else:
    diff = 0
    points = []
    ans = 0
    for i in range(N):
        a = A[i]
        b = B[i]
        if a < b:
            diff += b - a
            ans += 1
        elif a > b:
            points.append(a-b)
    for p in sorted(points,reverse = True):
        if diff <= 0:
            print(ans)
            exit()
        diff -= p
        ans += 1 
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
K = 0
S = 0
T = []
if sum(A) < sum(B):
    K = -1
else:
    for i in range(N):
        if A[i] < B[i]:
            K += 1
            S = S + B[i] - A[i]
        elif A[i] > B[i]:
            T.append(A[i] - B[i])
        else:
            continue
T.sort()
L = 0
while S > L:
    L += T.pop()
    K += 1
print(K)

Submission status: Accepted

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

ans = 0
if sum(a) < sum(b):
    print(-1)
    exit()
s = 0
num = []
ok = True
for i in range(n):
    if a[i] < b[i]:
        s += b[i] - a[i]
        ans += 1
        ok = False
    else:
        num.append(a[i]-b[i])
if ok:
    print(0)
    exit()
num.sort()
t = 0
while s > t:
    t += num.pop()
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
s = 0
m = []
l = 0
if sum(B)<=sum(A):
    for i in range(N):
        if A[i] < B[i]:
            k += 1
            s += (B[i]- A[i])
        elif A[i] > B[i]+1:
            m.append(A[i]- B[i])
        elif A[i] == B[i]+1:
            l += (A[i]- B[i])
    if len(m)>1:
        n = sorted(m,reverse = True)
    else:
        n = m
    for j in range(len(m)):
        if s > 0:
            s -= n[j]
            k += 1
        if s <0:
            break
    if (s > 0) and(k > 0):
         k += s
else:
    k = -1
print(k)

Submission status: Accepted

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

ans = 0

if sum(a) >= sum(b):
    diff = [a[i]-b[i] for i in range(n)]
    diff.sort()
    shortage = 0
    for i in diff:
        if i < 0:
            ans += 1
            shortage += (-i)
        else:
            break
    idx = n-1
    while shortage > 0:
        shortage -= diff[idx]
        idx -= 1
        ans += 1
else:
    ans = -1
print(ans)

Submission status: Accepted

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

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

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

d.sort()

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

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

if cnt_neg==0:
    print(0)
    exit()
else:
    ans = cnt_neg

last=n-1
for neg in range(n):
    if d[neg]>=0:break

    while d[neg]<0:
        tmp = min(-d[neg], d[last])
        d[neg] += tmp
        d[last] -= tmp
        if d[last] == 0:
            last-=1
            ans+=1

if d[last] != a[last]-b[last] and d[last]!=0 : ans+=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l_a = list(map(int,input().split()))
l_b = list(map(int,input().split()))
if sum(l_a)<sum(l_b):
  print(-1)
  exit()
l_sa = sorted(list(i-j for i,j in zip(l_a,l_b)))
s = 0
cou =0
#print(l_sa)
for i in l_sa:
  if i<0:
    s += i
    cou +=1
  else:
    break
#print(s,cou)
l_sb = l_sa[::-1]
for i in l_sb:
  if s<0:
    s += i
    cou += 1
    #print(s,cou)
  else:
    break
print(cou)

Submission status: Accepted

Solution code:
from collections import deque,Counter

import numpy as np

N = int(input())
arrA = np.array(list(map(int,input().split())))
arrB = np.array(list(map(int,input().split())))

arrQ = arrA - arrB
arrQ.sort()
# num = arrQ.sum()
# if num < 0:
#     print(-1)
#     exit()

que = deque(sorted(arrQ))
tmp = 0
ans = 0
while len(que)>0:
    low = que.popleft()
    if low <0:
        tmp += low
        ans += 1
        while tmp < 0 and len(que)>0:
            tmp += que.pop()
            ans += 1
    else:
        break

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

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

    surplus.sort(reverse=True)
    cusum = [0] * (len(surplus) + 1)
    for i, sur in enumerate(surplus):
        cusum[i+1] = cusum[i] + sur

    ans += bisect_left(cusum, need)
    if ans > N:
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

    surplus = []
    need = 0
    ans = 0
    for a, b in zip(A, B):
        diff = a - b
        if diff > 0:
            surplus.append(diff)
        if diff < 0:
            need += -diff
            ans += 1

    surplus.sort(reverse=True)
    cusum = [0] * (len(surplus) + 1)
    for i, sur in enumerate(surplus):
        cusum[i+1] = cusum[i] + sur

    plus = bisect_left(cusum, need)
    ans += plus
    if need and plus == len(cusum):
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    A = np.array(readline().split(), np.int64)
    B = np.array(readline().split(), np.int64)
    C = A-B
    pos = C[C>0]
    neg = C[C<0]
    Lpos = len(pos)
    Lneg = len(neg)
    pos = np.sort(pos)[::-1]
    total = np.sum(neg)
    if Lneg == 0:
        print(0)
        sys.exit()
    for i in range(Lpos):
        total += pos[i]
        if total>=0:
            print(Lneg+i+1)
            break
        else:
            continue
    else:
        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()))
total = 0
li = []
c = 0
for aa, bb in zip(a, b):
    if bb>aa:
        total += bb-aa
        c += 1
    elif bb<aa:
        li.append(aa-bb)
if c==0:
    print(0)
    exit()
li.sort(reverse=True)
for ans, i in enumerate(li):
    total -= i
    if total<=0:
        print(ans+c+1)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(int, input().split()))
datb = list(map(int, input().split()))
datc = [data[i] - datb[i] for i in range(n)]
datc.sort(reverse=True)
#print(datc)
if sum(datc) < 0:
    print(-1)
else:
    l, r = 0, n-1
    res = 0
    total = 0
    import collections
    dat = collections.deque(datc)
    while len(dat) > 0:
        if total >= 0:
            v = dat.pop()
            if v >= 0:
                break
            res += 1
            total += v
        else:
            total += dat.popleft()
            res += 1
    print(res)

Submission status: Accepted

Solution code:
def solve(N, A, B):
    if sum(A) < sum(B):
        print(-1)
        return

    diff = [a - b for a, b in zip(A, B)]
    changed = [False for _ in range(N)]
    diff.sort()
    l, r = 0, N - 1

    while diff[l] < 0:
        if diff[l] + diff[r] >= 0:
            changed[l] = True
            changed[r] = True
            diff[r] += diff[l]
            l += 1
        else:
            changed[l] = True
            changed[r] = True
            diff[l] += diff[r]
            r -= 1
    print(sum(changed))
    return


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split(' ')))
    B = list(map(int, input().split(' ')))

    solve(N, A, B)

Submission status: Accepted

Solution code:
n=int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
be = list()
ab = list()
for i in range(n):
    j,k=a[i],b[i]
    if j<k:
        be.append(k-j)
    elif j>k:
        ab.append(j-k)
p=len(be)
be=sum(be)
ab.sort(reverse=True)
k=0
if be==0:
    print(0)
    exit()
for i in range(len(ab)):
    k+=ab[i]
    if k >= be:
        print(p+i+1)
        exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
lack=0
num=[]
count=0
c=0
if sum(a)<sum(b):
    print(-1)
    
else:
    for i in range(n):
        if b[i]>a[i]:
            lack+=b[i]-a[i]
            count+=1
        if b[i]<a[i]:
            num.append(a[i]-b[i])
    if count!=0:
       num.sort(reverse=True)
       for i in range(len(num)):
           c+=num[i]
           count+=1
           if c>=lack:
            break
    print(count)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

import copy

# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = na()
    b = na()

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

    d = []
    for ai, bi in zip(a, b):
        d.append(ai - bi)

    d.sort()
    poor = 0
    ans_cnt = 0
    for di in d:
        if di < 0:
            ans_cnt += 1
            poor += di

    d.sort(reverse=True)
    for di in d:
        if poor < 0:
            poor += di
            ans_cnt += 1
        else:
            break

    print(ans_cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

    sub = []
    t = 0
    cnt = 0
    for i in range(n):
        if a[i] < b[i]:
            cnt += 1
            t += b[i] - a[i]
        else:
            sub.append(a[i]-b[i])

    if cnt == 0:
        print(0)
        return

    ans = cnt
    i = 0
    sub.sort(reverse=True)
    while t > 0:
        ans += 1
        t -= sub[i]
        i += 1
    print(ans)








if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(_) for _ in input().split()]
    B = [int(_) for _ in input().split()]
    if sum(A) < sum(B):
        print(-1)
        return
    tariruhito = []
    tarinai = 0
    output = 0
    for a, b in zip(A, B):
        if a >= b: tariruhito.append(a-b)
        else:
            output += 1
            tarinai += b - a
    tariruhito.sort(reverse=True)
    i = 0
    while tarinai > 0:
        output += 1
        tarinai = max(0, tarinai-tariruhito[i])
        i += 1
    print(output)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

S = [b - a for a, b in zip(A, B) if a < b]

if sum(S) == 0:
    print(0)
    exit()

T = sorted([a - b for a, b in zip(A, B) if a > b], key=lambda x: -x)

cnt = 0
tmp = sum(S)
for t in T:
    tmp -= t
    cnt += 1

    if tmp <= 0:
        break

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

print(len(S) + 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()]

mass_prepare = []
change_count = 0
total_minus = 0
for i in range(N):
    diff = A[i] - B[i]
    if diff < 0:
        change_count += 1
        total_minus += diff
    elif diff > 0:
        mass_prepare.append(diff)
mass_prepare.sort()
mass_prepare.reverse()
total = 0
for i in range(len(mass_prepare)+1):
    if total >= abs(total_minus):
        print(i+change_count)
        break
    if i == len(mass_prepare):
        print(-1)
        break
    total += mass_prepare[i]

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

d = a - b
s = np.sum(d)


if(s < 0):
    print(-1)
else:
    d = np.sort(d)
    d = d[d != 0]
    d2 = np.copy(d)
    i = -1
    for j in range(len(d2)):
        if(d2[j] >= 0):break
        while(d2[j] < 0):
            t = min(-d2[j],d2[i])
            d2[j] += t
            d2[i] -= t
            if(d2[i] == 0):i -= 1
    c = 0
    for i in range(len(d2)):
        if(d[i] != d2[i]):c += 1
    print(c)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    minus = 0
    minus_count = 0
    plus_list = []
    for i in range(N):
        diff = A[i] - B[i]
        if diff < 0:
            minus += diff
            minus_count += 1
        else:
            plus_list.append(diff)
    plus_list.sort(reverse=True)
    if minus == 0:
        print(0)
    else:
        ans = 0
        for v in plus_list:
            minus += v
            ans += 1
            if minus >= 0:break
        if minus < 0:
            print(-1)
        else:
            print(ans+minus_count)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    diff = sum(A) - sum(B)
    if diff < 0:
        print(-1)
    else:
        C = []
        for i in range(N):
            tmp = A[i] - B[i]
            if tmp >= 0:
                C.append(tmp)
        C.sort()
        # print(C,diff)
        j = 0
        while C[j] <= diff:
            diff -= C[j]
            j += 1
            if j == N:
                break
        print(N-j)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if sum(a)<sum(b):
    print(-1)
    exit()
dif = []
for i in range(n):
    dif.append(a[i]-b[i])
dif.sort()
dif += [0]
ans = 0
i = n
for x in dif:
    if x<0:
        ans += 1
        while x<0:
            if dif[i]<-x:
                x += dif[i]
                i -= 1
                ans += 1
            else:
                dif[i] += x
                x = 0
    else:break
print(ans)

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

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

from collections import deque
C = deque(C)
c = 0
while C:
  nc = C.popleft()
  if nc >=0:
    break
  c+= nc
  ans += 1
  while c<0:
    c+=C.pop()
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
pos,neg=[],0
if sum(a)<sum(b):print(-1);exit()
ne=0
for i in range(n):
    if a[i]==b[i]:continue
    if a[i]<b[i]:neg+=-a[i]+b[i];ne+=1
    else:pos.append(a[i]-b[i])
s=0
pos.sort(reverse=1)
ans=0


for i in pos:
    if s>=neg:print(ans+ne);exit()
    s+=i
    ans+=1

print(ans+ne)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
m = 0
ans = 0
P = []
for a,b in zip(A,B):
    if a - b < 0:
        m += b-a
        ans += 1
    elif a - b > 0:
        P.append(a-b)
P.sort(reverse = True)
if m <= 0:
    print(ans)
else:
    for p in P:
        m -= p
        ans += 1
        if m <= 0:
            print(ans)
            break
    else:
        print(-1)

Submission status: Accepted

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

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

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

Diff = sorted(Diff)

res = 0
for i in range(n):
	if Diff[i] < 0:
		res += 1
	else:
			if Diff[i] <= diff_AB:
				diff_AB -= Diff[i]
			else:
				diff_AB = 0
				res += 1

if diff_AB > 0:
	res += 1

print(res)

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

Submission status: Accepted

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

dif=sum(a)-sum(b)
if dif<0:
    print(-1)
    
elif dif==0:
    cnt=0
    for i in range(n):
        if a[i]!=b[i]:
            cnt+=1
    print(cnt)
    
else:
    difference=[]
    count_0=0
    for i in range(n):
        if a[i]-b[i]>0:
            difference.append(a[i]-b[i])
        elif a[i]==b[i]:
            count_0+=1
    difference=sorted(difference)
    
    cnt=0
    keep=0
    for i in range(len(difference)):
        if cnt+difference[i]<=dif:
            cnt+=difference[i]
            keep+=1
        else:
            break
    print(n-count_0-keep)

Submission status: Accepted

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

ABdiff = sorted([A[i]-B[i] for i in range(N)], reverse = True)
if sum(ABdiff) < 0:
    print(-1)
    exit()

ABminus = [A[i]-B[i] for i in range(N) if A[i]-B[i] < 0]
if len(ABminus) == 0:
    print(0)
    exit()

all_minus = sum(ABminus)
plus = 0

for i in range(N):
    plus += ABdiff[i]
    if plus >= abs(all_minus):
        print(i+1 + len(ABminus))
        break

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=[]
X=0
count=0
for i in range(N):
    a=A[i]
    b=B[i]
    if a>b:
        D.append(a-b)
    elif b>a:
        X+=b-a
        count+=1

D=sorted(D, reverse=True)
for d in D:
    if X==0:
        break
    Y=min(d,X)
    X-=Y
    count+=1
    
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(b)>sum(a):
    print(-1)
    exit()
p=[]
m=[]
sub=[]
for i in range(n):
    s=a[i]-b[i]
    if s>0:
        p.append(s)
    elif s<0:
        m.append(s)
p.sort(reverse=True)
ans=len(m)+min(len(m),1)
cnt=sum(m)
j=0
while cnt<0:
    if cnt+p[j]>=0:
        cnt+=p[j]
    else:
        cnt+=p[j]
        j+=1
        ans+=1
print(ans)

Submission status: Accepted

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

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

c = []
t = 0
ans = 0
for x, y in zip(a, b):
    if x < y:
        t += y - x
        ans += 1
    else:
        c.append(x - y)

if ans == 0:
    print('0')
    exit()

c.sort(reverse=True)
for p in c:
    t -= p
    ans += 1
    if t < 0:
        break
print(ans)

Submission status: Accepted

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

if __name__=='__main__':
    main()