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

S_ba = []
S_ab = []
for i in range(N):
    if A[i] < B[i]: S_ba.append(B[i] - A[i])
    elif A[i] > B[i]: S_ab.append(A[i] - B[i])

if sum(S_ba) > sum(S_ab):
    print(-1)
    exit()
    
ans = len(S_ba)
now = 0
rem = sum(S_ba)
S_ab.sort(reverse=True)
for i in range(len(S_ab)):
    if rem <= now: break
    now += S_ab[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()))

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

amari = []
fusoku = []
for i in range(N):
  if A[i] < B[i]:
    fusoku.append(B[i]-A[i])
  elif A[i] > B[i]:
    amari.append(A[i]-B[i])
amari.sort(reverse=True)
ans = len(fusoku)
nokori = sum(fusoku)
now = 0
while nokori > 0:
  nokori -= amari[now]
  now += 1
  ans += 1
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n=int(input())
a=lnii()
b=lnii()

plus=[]
minus=0
ans=0
for i in range(n):
  if a[i]<b[i]:
    minus+=b[i]-a[i]
    ans+=1
  else:
    plus.append(a[i]-b[i])

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

plus.sort(reverse=True)
for i in range(len(plus)):
  minus-=plus[i]
  ans+=1
  if minus<=0:
    print(ans)
    exit()

print(-1)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
b = inpl()
p = []
m = 0
c = 0
for i in range(n):
    if a[i] - b[i] < 0:
        m += b[i]-a[i]
        c += 1
    elif a[i]-b[i] > 0:
        p.append(a[i] - b[i])
p.sort(reverse = True)
if m > sum(p):
    print(-1)
    quit()
for x in p:
    if m <= 0:
        break
    c += 1
    m -= x
print(c)

Submission status: Accepted

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

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

  more = []
  less = []

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

  if len(less) == 0:
    print(0)
    return

  ans = len(less)

  v = sum(less)
  more.sort(reverse=True)
  for i in range(len(more)):
    if v <= 0:
      break
    v -= more[i]
    ans += 1

  print(ans)

  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
P = sorted([A[i]-B[i] for i in range(N) if A[i]-B[i]>0],reverse=True)
C = [0 for _ in range(N+1)]
for i in range(1,len(P)+1):
    C[i] = C[i-1]+P[i-1]
M = [B[i]-A[i] for i in range(N) if B[i]-A[i]>0]
p = sum(P)
m = sum(M)
if m>p:
    ans = -1
else:
    if len(M)==0:
        ans = 0
    else:
        for i in range(1,len(P)+1):
            if C[i]>=m:
                ans = i
                break
        ans += len(M)
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
Bs = list(mapint())

if sum(As)<sum(Bs):
    print(-1)
else:
    minus = 0
    cnt = 0
    lis = []
    for a, b in zip(As, Bs):
        if a<b:
            minus += (b-a)
            cnt += 1
        else:
            lis.append(a-b)
    lis.sort(reverse=True)
    if minus==0:
        print(0)
    else:
        for l in lis:
            minus -= l
            cnt += 1
            if minus<=0:
                break
        print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque, Counter
import math

# import copy
from bisect import bisect_left, bisect_right
import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD


def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret

def solve():

    n = getN()
    anums = getList()
    bnums = getList()
    if sum(anums) < sum(bnums):
        print(-1)
        return
    husoku = 0
    ans = 0
    tari = []
    for a, b in zip(anums, bnums):
        if b > a:
            husoku += b - a
            ans += 1
        else:
            tari.append(a - b)

    tari.sort(reverse=True)
    if husoku == 0:
        print(0)
        return
    # ans = 0
    for t in tari:
        ans += 1
        husoku -= t
        if husoku <= 0:
            print(ans)
            return
def main():
    n = getN()
    for _ in range(n):
        solve()


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

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(B) > sum(A):
    print(-1)
else:
    donor = []
    acceptance = []
    for a, b in zip(A, B):
        if a > b:
            donor.append(a-b)
        elif a < b:
            acceptance.append(b-a)
    donor.sort(reverse=True)

    ans = len(acceptance)
    ans += bisect.bisect_left([0]+list(accumulate(donor)), sum(acceptance))
    print(ans)

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)
else:
    donor = []
    acceptance = []
    for a, b in zip(A, B):
        if a > b:
            donor.append(a-b)
        elif a < b:
            acceptance.append(b-a)
    donor.sort()

    ans = 0
    stock = 0
    for x in acceptance:
        ans += 1
        while stock < x:
            stock += donor.pop()
            ans += 1
        else:
            stock -= x
    print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n, *AB = map(int, open(0).read().split())
A, B = AB[:n], AB[n:]
P = []
N = []
for a, b in zip(A, B):
    if b-a > 0:
        P.append(b-a)
    elif b-a < 0:
        N.append(a-b)
N.sort(reverse=1)
s = sum(P)
if s == 0:
    print(0)
else:
    for i, x in enumerate(accumulate(N), 1):
        if x >= s:
            print(len(P) + i)
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
import numpy as np
from bisect import bisect_right

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

diff = [0 for i in range(N)]
need = 0
ans = 0

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

diff = sorted(diff, reverse=True)  # 大きい順
cumsum = np.cumsum(diff)

if need != 0:
    ans += bisect_right(cumsum, need)+1
if ans > N:
    ans = -1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
p=[]
m=[]
def app(num1):
    p.append(num1)
def apm(num2):
    m.append(num2)
if sum(a)<sum(b):
    ans=-1
else:
    for i in range(n):
        if a[i]>b[i]:
            app(a[i]-b[i])
        elif a[i]<b[i]:
            apm(b[i]-a[i])
    num=sum(m)
    ans=len(m)
    p.sort(reverse=True)
    num1=0
    for i in range(len(p)):
        if num1>=num:
            break
        num1+=p[i]
        ans+=1
        if num1>=num:
            break
print(ans)

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

D = []
for i in range(N):
  D += [A[i]-B[i]]
D.sort()
M = 0
for i in range(N):
  if D[i] < 0:
    M += D[i]
    ans += 1
  else:
    break
for i in reversed(range(N)):
  if M >= 0:
    print(ans)
    break
  else:
    M += D[i]
    ans += 1

Submission status: Accepted

Solution code:
def main():
    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)
    else:
        D = sorted([d for a, b in zip(A, B) if (d := a - b) > 0])
        c = sum([a != b for a, b in zip(A, B)])
        for d in D:
            if d <= s:
                c -= 1
                s -= d
            else:
                break
        if s > 0 and len(A) == len(D):
            c += 1
        print(c)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
d = [0]*n
e = 0
t = 0
for i in range(n):
    d[i] = a[i] - b[i]
    if d[i] < 0:
        e += 1
        t -= d[i]
d = sorted(d,reverse=True)
if sum(d) < 0:
    ans = -1
else:
    s = 0
    for i in range(n):
        if s >= t:
            break
        else:
            s += d[i]
    ans = i + e
print(ans)

Submission status: Accepted

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


def log(*args):
    print(*args, file=sys.stderr)


def main():
    n = int(input())
    a = list(map(int, input().rstrip().split()))
    b = list(map(int, input().rstrip().split()))
    minus_sum = 0
    pluses = []
    ans = 0
    for i in range(n):
        if a[i] < b[i]:
            minus_sum += b[i] - a[i]
            ans += 1
        else:
            pluses.append(a[i] - b[i])
    pluses.sort(reverse=True)
    for v in pluses:
        if minus_sum <= 0:
            break
        minus_sum -= v
        ans += 1
    if minus_sum > 0:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 余剰と不足に分ける 余剰の多いやつから使って不足の多いやつに振る

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a) < sum(b):
    print(-1)
    exit()

surplus = []
lack = []
for i in range(n):
    if a[i] > b[i]:
        surplus.append(a[i] - b[i])
    elif a[i] < b[i]:
        # 同値は含まない
        lack.append(b[i] - a[i])
if len(lack) == 0:
    print(0)
    exit()

surplus = sorted(surplus, reverse=True)
lack = sorted(lack, reverse=True)

# print(surplus, lack)

right = 0
ans = 1
for left in range(len(surplus)):
    while right < len(lack) and surplus[left] - lack[right] >= 0:
        surplus[left] -= lack[right]
        ans += 1
        right += 1
    if right == len(lack):
        break
    # ここまではギリギリ条件を満たしている
    if right < len(lack):
        # 次にleftを使わなきゃいけない
        ans += 1
        # あまったぶんを回す
        lack[right] -= surplus[left]
print(ans),

Submission status: Accepted

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()
b = LIST()

if sum(a) < sum(b):
    print(-1)
else:
    c = sorted([a[i]-b[i] for i in range(n)])
    ans = 0
    reg = 0

    for x in c:
        if x < 0:
            ans += 1
            reg += x
        else:
            break

    for x in c[::-1]:
        if reg < 0:
            reg += x
            ans += 1
        else:
            break

    print(ans)

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

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

    for p in P:
        if v <= 0:
            break
        v -= p
        ans += 1
    if v <= 0:
        print(ans)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if(sum(a)<sum(b)):
    print(-1)
else:
    d=[a[i]-b[i] for i in range(n)]
    d.sort()

    ans=0
    minus_num=bisect.bisect_left(d, 0)
    ans+=minus_num
    minus_sum=sum(d[:minus_num])

    i=n-1
    while(minus_sum<0 and minus_num<=i):
        minus_sum+=d[i]
        i-=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()))

shortage = [b - a for a, b in zip(A, B) if a < b]
over = sorted([a - b for a, b in zip(A, B) if a > b], reverse=True)

sum_shortage = sum(shortage)
sum_over = 0
count = -1
if sum_shortage == 0:
    count = 0
elif sum_shortage > sum_over:
    for i, v in enumerate(over):
        sum_over += v
        if sum_shortage <= sum_over:
            count = len(shortage) + i + 1
            break
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
dis = []
count = 0
for i,j in zip(a,b):
    if j > i:
        ans += 1
        count += j-i
    else:
        dis.append(i-j)
dis.sort(reverse=True)
if count > 0 and dis == []:
    print(-1)
else:
    now = 0
    while now < len(dis) and count > 0:
        ans += 1
        count -= dis[now]
        now += 1
    if count > 0:
        print(-1)
    else:
        print(ans)

Submission status: Accepted

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

diff = [0] * N
neg = []
pos = []
for i in range(N):
    diff[i] = A[i] - B[i]
    if diff[i] < 0:
        neg.append(diff[i])
    else:
        pos.append(diff[i])

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

tobe_supplied = sum(neg)
pos.sort(reverse=True)

cnt = len(neg)

for p in pos:
    if tobe_supplied >= 0:
        break
    cnt += 1
    tobe_supplied += p    

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(A) < sum(B):
  print(-1)
else:
  diffs = [a - b for a, b in zip(A, B)]
  diffs.sort()
  changed = set()
  l = 0
  r = n - 1
  while l < r:
    if diffs[l] >= 0:
      break
    if diffs[r] + diffs[l] == 0:
      changed.add(l)
      changed.add(r)
      l += 1
      r -= 1
    elif diffs[r] + diffs[l] > 0:
      changed.add(l)
      changed.add(r)
      diffs[r] += diffs[l]
      l += 1
    else:
      changed.add(l)
      changed.add(r)
      diffs[l] += diffs[r]
      r -= 1
  print(len(changed))

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)
else:
    c = [a[i]-b[i] for i in range(n) if a[i]-b[i]>=0]
    d = [a[i]-b[i] for i in range(n) if a[i]-b[i]<0]
    d.append(0)
    c.sort(reverse=True)
    from itertools import accumulate
    c = list(accumulate(c))
    from bisect import bisect_left
    index= bisect_left(c,-1*sum(d))
    if d == [0]:
        d.pop(0)
    print(len(d)+index)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    cnt, nega, posi = 0, 0, []
    for x, y in zip(a, b):
        if x < y:
            nega += y - x
            cnt += 1
        else:
            posi.append(x - y)
    if sum(posi) - nega < 0:
        print(-1)
        exit()
    posi.sort(reverse=True)
    i = 0
    while nega > 0:
        nega -= posi[i]
        cnt += 1
        i += 1
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt, nega, posi = 0, 0, []
for x, y in zip(a, b):
    if x < y:
        nega += y - x
        cnt += 1
    else:
        posi.append(x - y)
if nega > 0:
    posi.sort(reverse=True)
    for i, x in enumerate(posi):
        nega -= x
        if nega <= 0:
            cnt += i + 1
            break
    else:
        cnt = -1
print(cnt)

Submission status: Accepted

Solution code:
import sys

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

sum_a=sum(a)
sum_b=sum(b)
c=[]
cnt=0
ans=0
d=0
if(sum_a<sum_b):
    print(-1)
else:
    for i in range(n):
        if a[i]<b[i]:
            cnt+=1
            d+=(b[i]-a[i])
        else:
            c.append(a[i]-b[i])
    if cnt==0:
        print(cnt)
        sys.exit()
    cc=sorted(c,reverse=True)
    cc_sum=0
    for i in range(n):
        cc_sum+=cc[i]
        if cc_sum>d:
            cnt+=(i+1)
            break
    print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    arr = [ai - bi for ai, bi in zip(a, b)]
    arr.sort(reverse=True)
    tmp_1 = sum([-ai for ai in arr if ai < 0])
    tmp_2 = sum([ai for ai in arr if ai > 0])
    ans = 0
    for ai in arr:
        if ai < 0:
            ans += 1
    if tmp_2 < tmp_1:
        print(-1)
    else:
        for ai in arr:
            if ai > 0:
                if tmp_1 > 0:
                    tmp_1 -= ai
                    ans += 1

        print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

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)]
if sum(diff) < 0:
  print(-1)
else:
  diff.sort()
  count = 0
  ans = 0
  for i in diff:
    if i<0:
      count += i
      ans += 1
    else:
      break
  for i in diff[::-1]:
    if count>=0:
      break
    else:
      count += 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()))

plus_l=[]
minus_l=[]
cnt=0
for i in range(n):
    if a[i]<b[i]:
        minus_l.append(b[i]-a[i])
        cnt+=1
    elif a[i]>b[i]:
        plus_l.append(a[i]-b[i])

if sum(minus_l)>sum(plus_l):
    print(-1)
    exit()

sum_minus=sum(minus_l)
tmp_sum_plus=0

plus_l.sort(reverse=True)

for p_i in plus_l:
    if tmp_sum_plus>=sum_minus:
        break
    tmp_sum_plus+=p_i
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
if sum(alst) - sum(blst) < 0:
    print(-1)
    exit()


ans = 0
res = []
plus = 0
for a, b in zip(alst, blst):
    if a < b:
        ans += 1
        plus += b - a
    else:
        res.append(a - b)
res.sort(reverse = True)
pos = 0
while plus > 0:
    plus -= res[pos]
    ans += 1
    pos += 1
print(ans)

Submission status: Accepted

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

c = sorted([ai - bi for ai, bi in zip(a, b)])
if sum(c) < 0:
    print(-1)
    exit()

ans, cnt = 0, 0
for i in c:
    if i >= 0:
        break
    ans += 1
    cnt += i
for i in c[::-1]:
    if cnt >= 0:
        print(ans)
        break
    ans += 1
    cnt += i

Submission status: Accepted

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

if sum(a_li) < sum(b_li):
    print(-1)
    exit()
    
check = [0]*n
cnt = 0
SUM_ma = 0

for i in range(n):
    check[i] = a_li[i] - b_li[i]
    
    if check[i] < 0:
        cnt += 1
        SUM_ma += check[i]

if SUM_ma < 0:
        
    check.sort(reverse=True)
    
    for i in check:
        SUM_ma += i
        cnt += 1
        
        if SUM_ma >= 0:
            break
        
print(cnt)

Submission status: Accepted

Solution code:
import sys
 
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
 
import numpy as np
 
N = int(readline())
A = np.array(readline().split(), dtype=np.int64)
B = np.array(readline().split(), dtype=np.int64)
 
diff = A-B
lack = -diff[diff<0]
remain = diff[diff>0]
 
x = lack.sum()
remain.sort()
cum = remain[::-1].cumsum()
 
y = 0 if x == 0 else np.searchsorted(cum,x) + 1
 
answer = len(lack) + y if y <= len(remain) else -1
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
pos = [10**10]+[a[i]-b[i] for i in range(n) if a[i] > b[i]]
neg = [b[i]-a[i] for i in range(n) if a[i] < b[i]]
pos.sort(reverse=True)
pos[0] = 0
for i in range(len(pos)-1): pos[i+1] += pos[i]
need = sum(neg)
ans = len(neg)
for i in range(len(pos)):
    if pos[i] >= need:
        ans += i
        break
else:
    ans = -1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

D=[]

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

D.sort()

k=0
need=0
ans=0
while D[k]<0 and k!=N:
    need+=D[k]
    k+=1
    ans+=1

l=-1
while need<0 and D[l]>0:
    need+=D[l]
    ans+=1
    l-=1

if need<0:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
C = np.array([a-b for a, b in zip(A, B)], dtype='int64')
C.sort()
cnt_neg = np.sum(C < 0)
cnt_pos = np.sum(C > 0)
sum_neg = -1 * np.sum(C[C < 0])
sum_pos = np.sum(C[C > 0])
if sum_pos - sum_neg < 0:
    print(-1)
elif sum_neg == 0:
    print(0)
else:
    C = C[C>0]
    C.sort()
    C = C[::-1]
    C = np.cumsum(C)
    print(cnt_neg + np.count_nonzero(C < sum_neg) + 1)

Submission status: Accepted

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

c = 0
tmp = 0
m = []
for i in range(n):
    if a[i] < b[i]:
        c += 1
        tmp -= (b[i]-a[i])
    else:
        m.append(a[i]-b[i])

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

m.sort(reverse=True)
i = 0
while i < len(m):
    tmp += m[i]
    i += 1
    if tmp >= 0:
        print(c+i)
        exit()
        break
print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[]
SA=sum(A)
SB=sum(B)
S=SA-SB
cnt=0
ns=0
if SB>SA:
    print(-1)
    exit()
for a,b in zip(A,B):
    if a>b:
        C.append(a-b)
    elif a<b:
        ns+=b-a
        cnt+=1
C.sort(reverse=True)
for i in range(len(C)):
    if ns>0:
        cnt+=1
        ns-=C[i]
    else:break
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = [a-b for a,b in zip(A,B)]
A.sort()
if sum(A)<0:
    print(-1)
else:
    cnt = sum([a<0 for a in A])
    max_i = -1
    for i in range(n):
        while A[i]<0:
            if A[max_i] > abs(A[i]):
                A[max_i] += A[i]
                A[i] = 0
            else:
                A[i] += A[max_i]
                A[max_i] = 0
                max_i -= 1
    print(cnt-max_i if cnt!=0 else 0)

Submission status: Accepted

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

plist=[]
nval=0
ncounter=0

for i in range(n):
	if alist[i] > blist[i]:
		plist.append(alist[i]-blist[i])
	elif alist[i] < blist[i]:
		nval += blist[i] - alist[i]
		ncounter += 1

plist.sort(reverse = True)
i=0
maxi=len(plist)
pcounter = 0

while nval != 0 and i < maxi:
	pcounter += 1
	nval = max(0,nval-plist[i])
	i += 1

if nval == 0:
	print(pcounter + ncounter)
else:
	print(-1)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""
def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# a^n
def power(a,n,mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod

# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n-1
    while(tmp >= l):
        a = a*tmp%mod
        tmp -= 1
    return a

inf = 10**18
mod = 10**9+7

n = ir()
a = lr()
b = lr()

yoyu = []
muri = []

for i in range(n):
    if a[i] > b[i]:
        yoyu.append(a[i]-b[i])
    if a[i] < b[i]:
        muri.append(b[i]-a[i])

if sum(yoyu) < sum(muri):
    print(-1)
else:
    if muri:
        yoyu.sort(reverse=True)
        muri.sort()
        yoyu_ind = 0
        muri_ind = 0
        flag = False
        while muri_ind < len(muri):
            ret = muri[muri_ind]
            while ret > 0:
                if yoyu[yoyu_ind] > ret:
                    yoyu[yoyu_ind] -= ret
                    ret = 0
                    flag = False
                elif yoyu[yoyu_ind] == ret:
                    yoyu[yoyu_ind] -= ret
                    ret = 0
                    yoyu_ind+=1
                    flag = True
                else:
                    ret -= yoyu[yoyu_ind]
                    yoyu[yoyu_ind] = 0
                    yoyu_ind+=1
            muri_ind+=1
        if flag:
            print(muri_ind + yoyu_ind)
        else:
            print(muri_ind + yoyu_ind+1)
    else:
        print(0)

Submission status: Accepted

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

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

ans = 0
memo = []
tmp = 0

for i in range(n):
    if a[i] >= b[i]:
        memo.append(a[i]-b[i])
    else:
        tmp += b[i]-a[i]
        ans += 1

memo.sort(reverse = True)

if len(memo) != len(a):
    for j in memo:
        if j < tmp:
            tmp -= j
            ans += 1
        else:
            ans += 1
            break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split()))
Bs = list(map(int,input().split()))

Cs = []

if sum(As) < sum(Bs):
  print(-1)
  exit()

cnt = 0
ms = 0
ps = 0
for i in range(N):
  Cs.append(As[i]-Bs[i])
  if As[i]-Bs[i] < 0:
    cnt += 1
    ms += Bs[i] - As[i]
    
if cnt == 0:
  print(cnt)
  exit()
  
Cs.sort()
i = 0
while ps < ms:
  i += 1
  ps += Cs[-i]
  
print(cnt+i)

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)

dif=0
cnt=0
asis=0
#足りない項には足してあげる
for i in range(n):
    if a[i]<b[i]:
        dif-=(b[i]-a[i])
        a[i]=b[i]
        cnt+=1

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

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

c=sorted(c,reverse=True)

#余裕があるところから引いていく
for k in range(n):
    if c[k]>=dif:
        dif+=c[k]
        cnt+=1

    if dif>=0:
        break

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b_a = [0] * n
for i in range(n):
    b_a[i] = a[i] - b[i]
b_a.sort()
total = sum([-1 * i for i in b_a if i < 0])  # 負の要素のsum * -1
lim = len([-1 * i for i in b_a if i < 0])  # 負の個数 3　なら0,1,2 が負
#print(b_a, total, lim)
num = 0
if total == 0:
    print(0)
    exit()
while total > 0:  # 全部正の時にtotal=0でここには入らなくて良い。
    total -= b_a[-num - 1]
    num += 1
    if num == n - 1:  # 無限るーぷしないように、n-num-1 がlim-1まできたらbreak
        print(-1)
        exit()
print(num + lim)