Problem:


Score : 400 points

Problem Statement

A university student, Takahashi, has to take N examinations and pass all of them.
Currently, his readiness for the i-th examination is A_{i}, and according to his investigation, it is known that he needs readiness of at least B_{i} in order to pass the i-th examination.

Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness.

For Takahashi, find the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the following conditions:

The sum of the sequence A_1, A_2, ..., A_{N} and the sum of the sequence C_1, C_2, ..., C_{N} are equal.

For every i, B_i \leq C_i holds.

If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

A_i and B_i are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N}
B_1 B_2 ... B_{N}

Output

Print the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the conditions.
If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Sample Input 1

3
2 3 5
3 4 1

Sample Output 1

3

(A_1, A_2, A_3) = (2, 3, 5) and (B_1, B_2, B_3) = (3, 4, 1). If nothing is done, he cannot pass the first and second exams.
The minimum possible number of indices i such that A_i and C_i are different, 3, is achieved when:

(C_1, C_2, C_3) = (3, 5, 2)

Sample Input 2

3
2 3 3
2 2 1

Sample Output 2

0

In this case, he has to do nothing in order to pass all the exams.

Sample Input 3

3
17 7 1
25 6 14

Sample Output 3

-1

In this case, no matter what is done, he cannot pass all the exams.

Sample Input 4

12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604
74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212

Sample Output 4

5



Sample input:
3
2 3 5
3 4 1


Sample output:
3

Submission status: Accepted

Solution code:
#! /usr/bin/env python
import sys
from functools import reduce

def main():
    line = sys.stdin.readlines()
    index = int(line[0].strip())
    line1 = line[1].strip()
    line2 = line[2].strip()
    listA = [int(x) for x in line1.split(' ')]
    listB = [int(x) for x in line2.split(' ')]
    
    maxA = max(listA)
    maxB = max(listB)

    if maxA < maxB:
        print(-1)
        return

    diff = [listA[i] - listB[i] for i in range(index)] 
    diff.sort()

    if not checkBeMinus(diff):
        print(0)
        return

    # Split minus and plus item
    plus = [x for x in diff if x >= 0]
    plus.sort(reverse=True)
    minus = [x for x in diff if x < 0]

    changeCount = len(minus)
    allMinus = reduce((lambda x, y: x + y), minus)
    allPlus = reduce((lambda x, y: x + y), plus)
    if allPlus + allMinus < 0:
        print(-1)
        return
    accm = 0
    for p in plus:
        accm += p
        changeCount += 1
        if accm >= abs(allMinus):
            break

    print(changeCount)
    

def checkBeMinus(list):
    return min(list) < 0

main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = []
cou = 0
anti = 0
for i in range(n):
    a = A[i]
    b = B[i]
    if (a - b) < 0:
        cou += 1
        anti += (a-b)
    else:
        D.append(a-b)
if anti == 0:
    ans = 0
elif sum(D) + anti < 0:
    ans = -1
else:
    D.sort(reverse = True)
    l = len(D)
    for i in range(l):
        anti += D[i]
        if anti >= 0:
            ans = cou + i + 1
            break
print(ans)

Submission status: Accepted

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

minus = [b[i]-a[i] for i in range(n) if a[i]-b[i]<0]
c = [a[i]-b[i] for i in range(n) if a[i]-b[i]>0]

if sum(c)<sum(minus):
  print(-1)
  sys.exit()
elif sum(minus)==0:
  print(0)
  sys.exit()

c.sort()
cost = sum(minus)
x = 0
for i,y in enumerate(c[::-1]):
  if x>cost:
    print(len(minus)+i)
    sys.exit()
  else:
    x+=y
print(len(minus)+len(c))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    minus = 0
    num_minus = 0
    plus = []
    for i, (a, b) in enumerate(zip(aa,bb)):
        judge = b-a
        if judge>0:
            minus+=judge
            num_minus += 1
        else:
            plus.append(-judge)
    if minus==0:
        print(0)
        exit()
    plus.sort(reverse=True)
    for i, num in enumerate(plus,1):
        minus-=num
        if minus<=0:
            print(i+num_minus)
            exit()
    print(-1)

Submission status: Accepted

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

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

s = 0
cnt = 0
delta = []
for i in range(N):
    if A[i] < B[i]:
        cnt += 1
        s += B[i] - A[i]
    else:
        delta.append(A[i] - B[i])
n = 0
for d in sorted(delta, reverse=True):
    if n < s:
      n += d
      cnt += 1
    else:
        break
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = map(int, input().split())
B = map(int, input().split())
diff = [a - b for a, b in zip(A, B)]
upper = sorted([x for x in diff if x > 0])
lower = sorted([x for x in diff if x < 0])
ans = N - len([x for x in diff if x == 0])
while upper and lower:
    rest = upper.pop()
    while lower:
        tgt = lower.pop()       # negative value
        if rest + tgt >= 0:
            rest += tgt
        else:
            lower.append(tgt + rest)
            break
if lower:
    print(-1)
else:
    print(ans - len(upper))

Submission status: Accepted

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)]
upper = sorted([x for x in diff if x > 0])
lower = sorted([x for x in diff if x < 0])
ans = N - len([x for x in diff if x == 0])
while upper and lower:
    rest = upper.pop()
    while lower:
        tgt = lower.pop()       # negative value
        if rest + tgt >= 0:
            rest += tgt
        else:
            lower.append(tgt + rest)
            break
if lower:
    print(-1)
else:
    print(ans - len(upper))

Submission status: Accepted

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)]
upper = sorted([x for x in diff if x > 0])
lower = sorted([x for x in diff if x < 0], reverse=True)
ans = N - len([x for x in diff if x == 0])
while upper and lower:
    rest = upper.pop()
    while lower:
        tgt = lower.pop()       # negative value
        if rest + tgt >= 0:
            rest += tgt
        else:
            lower.append(tgt + rest)
            break
if lower:
    print(-1)
else:
    print(ans - len(upper))

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:
    D = [a-b for a,b in zip(A,B)]
    PLUS = sorted(filter(lambda x:x>0,D),reverse=True)
    MINUS = list(filter(lambda x:x<0,D))
    ans = len(MINUS)
    addans = 0
    while len(MINUS)>0:
        if addans==0: addans += 1
        if PLUS[0]>=abs(MINUS[0]):
            PLUS[0] += MINUS[0]
            MINUS.pop(0)
        elif PLUS[0]<abs(MINUS[0]):
            MINUS[0] += PLUS[0]
            PLUS.pop(0)
            addans += 1
        #print(PLUS,MINUS)
    ans += addans
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
k = []
j = 0
na= 0
c = 0
if sum(a)<sum(b):
    print(-1)
else:
    for i in range(n):
        if a[i]<b[i]:
            na +=b[i]-a[i]
            c+=1
        elif a[i] > b[i]:
            k.append(a[i]-b[i])
    k.sort()
    k = k[::-1]
    while na > 0:
        na -=k[j]
        c +=1
        j+=1
    print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
k = []
j = 0
na= 0
c = 0
for i in range(n):
    if a[i]<b[i]:
        na +=b[i]-a[i]
        c+=1
    elif a[i] > b[i]:
        k.append(a[i]-b[i])
k.sort()
while na > 0 and k:
    na -=k[-1]
    k.pop(-1)
    c +=1
if na > 0:print(-1)
else:print(c)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
mii=lambda:map(int,input().split())

n=int(input())
a=list(mii())
b=list(mii())
k = []
j = 0
na= 0
c = 0
for i in range(n):
    if a[i]<b[i]:
        na +=b[i]-a[i]
        c+=1
    elif a[i] > b[i]:
        k.append(a[i]-b[i])
k.sort()
while na > 0 and k:
    na -=k[-1]
    k.pop(-1)
    c +=1
if na > 0:print(-1)
else:print(c)

Submission status: Accepted

Solution code:
N = int(input())
A =  []
l1 = input().split()
for i in l1:
    A.append(int(i))
B = []
l2 = input().split()
for i in l2:
    B.append(int(i))
if sum(A) < sum(B):
    print(-1)
else:
    z = 0
    ar1 = []
    ar2 = []
    for i in range(N):
        if A[i] < B[i] :
            ar1.append(B[i] - A[i])
        if A[i] > B[i]:
            ar2.append(A[i] - B[i])
    if len(ar1) == 0:
        print(0)
    else:
        s = sum(ar1)
        ar2.sort(reverse = True)
        temp = 0
        b = 0
        for  i in range(len(ar2)):
            temp += ar2[i]
            if temp >= s:
                b = i+1
                break
        print(len(ar1)+b)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]

def solve(N, A, B):
    xs = [a - b for a, b in zip(A, B)]
    if sum(xs) < 0:
        return -1
    xs.sort()
    rxs = xs[:]
    # print(xs)
    i = 0
    j = len(xs) - 1
    while xs[i] < 0:
        l = -xs[i]
        while l > 0:
            d = min(l, xs[j])
            l -= d
            xs[j] -= d
            if xs[j] == 0:
                j -= 1
        xs[i] = 0
        i += 1
    # print(xs)
    return sum(a != b for a, b in zip(rxs, xs))

print(solve(N, A, B))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [A[i] - B[i] for i in range(N)]
C.sort(reverse=True)

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

ans = sum([1 for i in range(N) if C[i] < 0])
temp = sum([C[i] for i in range(N) if C[i] < 0])
if(not temp):
    print(0)
    exit()

for item in C:
    ans += 1
    temp += item
    if temp >= 0:
        break
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split()))) #現状
B = np.array(list(map(int, input().split()))) #要求値

AminusB = A-B

if np.all(AminusB >= 0):
    print(0)
elif np.sum(AminusB) < 0:
    print(-1)
else:
    AminusB = np.sort(AminusB)[::-1]
    count = len(AminusB[AminusB < 0])
    minus = AminusB[AminusB < 0].sum()
    
    for i in range(N):
        minus += AminusB[i]
        count += 1
        if minus >= 0:
            break

    print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from itertools import accumulate

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

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

        if diff > 0:
            plus.append(diff)
        elif diff < 0:
            minus.append(diff)

    sorted_plus = list(accumulate([0] + sorted(plus, reverse=True)))
    summed_plus = sorted_plus[-1]
    summed_minus = sum(minus)
    ans = len(minus)

    if summed_plus < abs(summed_minus):
        print(-1)
        exit()

    for i in range(len(sorted_plus)):
        summed = sorted_plus[i] - sorted_plus[0]

        if summed + summed_minus >= 0:
            print(ans)
            exit()

        ans += 1

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

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

num = 0
l = 0
r = n-1
while(c[l]<0):
    num += c[l]
    l += 1
    if num < 0:
        num+= c[r]
        r -= 1

print(n-1 - r + 1 + l + 1 - 2)

Submission status: Accepted

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)]
upper = sorted([x for x in diff if x > 0])
lower = sorted([x for x in diff if x < 0], reverse=True)
ans = N - len([x for x in diff if x == 0])
while upper and lower:
    rest = upper.pop()
    while lower:
        tgt = lower.pop()
        if rest + tgt >= 0:
            rest += tgt
        else:
            lower.append(tgt + rest)
            break
if lower:
    print(-1)
else:
    print(ans - len(upper))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
leeway = sum(A) - sum(B)
if leeway < 0:
	print(-1)
else:
	cnt = N
	for i in sorted(range(N), key = lambda x: A[x] - B[x]):
		diff = A[i] - B[i]
		if diff >= 0:
			if diff <= leeway:
				leeway -= diff
				cnt -= 1
			else:
				break
	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:
  C = [A[i] - B[i] for i in range(N)]
  D = sorted([c for c in C if c > 0])
  E = [c for c in C if c < 0]
  ans = 0
  num = sum(E)
  while num < 0:
    num += D.pop()
    ans += 1
  print(ans + len(E))

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
cnt=0
tmp=[]
sum_tmp=0
if sum(a)<sum(b):
    print(-1)
    sys.exit()
for i in range(n):
    if a[i]>=b[i]:
        tmp.append(a[i]-b[i])
    else:
        cnt+=1
        sum_tmp+=abs(a[i]-b[i])
tmp.sort(reverse=True)
for i in range(len(tmp)):
    if sum_tmp<=0:
        break
    sum_tmp-=tmp[i]
    cnt+=1

print(cnt)

Submission status: Accepted

Solution code:
N = raw_input()
A_list = map(int, raw_input().split())
B_list = map(int, raw_input().split())
if sum(A_list) < sum(B_list):
    print(-1)
else:
    import numpy as np
    diff = [x - y for (x, y) in zip(A_list, B_list)]
    neg_list = [i for i in diff if i < 0]
    neg_sum = sum(neg_list)
    pos_list = [i for i in diff if i > 0]
    pos_list.sort(reverse=True)
    pos_list = np.cumsum(pos_list).tolist()
    for j,k in enumerate(pos_list):
        if len(neg_list) == 0:
            print(0)
            break
        elif neg_sum + k >= 0:
            print(j+1+len(neg_list))
            break 
        else:
            continue

Submission status: Accepted

Solution code:
# coding:utf-8
n = int(input())
a = list(map(int,input().split(' ')))
b = list(map(int,input().split(' ')))
d = [ a - b for a,b in zip(a,b)]
m = [ di for di in d if di < 0]
p = [ di for di in d if di > 0]

sum_m = sum(m)
if sum_m == 0:
  print(0)
  exit()

p.sort(reverse=True)
for i,pi in enumerate(p):
  sum_m += pi
  if sum_m >= 0:
    print(len(m) + 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()))

stack = []
lack = 0
count = 0

if  sum(A) >= sum(B) :
    for i in range(N) :
        if A[i] < B[i] :
            count += 1
            lack += B[i] - A[i]

        elif A[i] > B[i] :
            stack.append(A[i] - B[i])
    stack.sort(reverse=True)
    if count != 0 :
        for j in range(len(stack)) :
            lack -= stack.pop(0)
            count += 1
            if lack <= 0 :
                break
    print(count)

else :
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
num = 0
s = 0
l = []
for i in range(n):
  if a[i] < b[i]:
    s += b[i]-a[i]
    num += 1
  if a[i] > b[i]:
    l.append(a[i]-b[i])
l = sorted(l, reverse=True)
if num == 0:
  print("0")
else:
  for i,j in enumerate(l):
    s -= l[i]
    num += 1
    if s <= 0:
      break

if num != 0 and sum(a) < sum(b):
  print("-1")
elif num != 0 and sum(a) >= sum(b):
  print(num)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
supply_list = []
lack = 0
lack_cnt = 0

for i in range(n):
    if a_list[i] < b_list[i]:
        lack += b_list[i] - a_list[i]
        lack_cnt += 1
    elif a_list[i] > b_list[i]:
        supply_list.append(a_list[i] - b_list[i])

if lack_cnt == 0:
    print(0)
    exit()
elif len(supply_list) == 0:
    print(-1)
    exit()

supply = 0
supply_cnt = 0

for i in sorted(supply_list, reverse=True):
    supply += i
    supply_cnt += 1
    if supply >= lack:
        break

if supply < lack:
    print(-1)
    exit()

print(lack_cnt + supply_cnt)

Submission status: Accepted

Solution code:
n = input()
a = input().split()
b = input().split()
ans = 0
aa = 0
plus = []
def list_sum(l):
    r = 0
    for i in l:
        r += int(i)
    return r

if not list_sum(a) >= list_sum(b):
    print(-1)
else:
    for i in range(int(n)):
        if int(a[i]) < int(b[i]):
            ans += 1
            aa += int(b[i]) - int(a[i])
        else:
            plus.append(int(a[i]) - int(b[i]))
    plus = sorted(plus,reverse=True)
    for i in plus:
        if aa <= 0:
            break
        aa = aa - i
        ans += 1
    print(ans)

Submission status: Accepted

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

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

C.sort()
D.sort(reverse=1)

rest = 0
j = 0
ans = 0
for c in C:
    while j < len(D) and rest < c:
        rest += D[j]
        ans += 1
        j += 1
    if rest < c:
        ans = -1
        break
    ans += 1
    rest -= c
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

def f():
    if sum(A) < sum(B):
        return -1
    
    d = [a - b for a,b in zip(A, B)]
    d.sort()
    
    ret = 0
    tl = len(d) - 1
    hd = 0
    stock = 0
    while d[hd] < 0:
        stock += d[hd]
        hd += 1
        ret += 1
        while stock < 0:
            stock += d[tl]
            tl -= 1
            ret += 1
    
    return ret

print(f())

Submission status: Accepted

Solution code:
n = int(input())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]
ABs = [a - b for a, b in zip(As, Bs)]
if sum(As) < sum(Bs):
    print(-1)
    exit()
mABs = [ab for ab in ABs if ab < 0]
ans, t = len(mABs), sum(mABs)
for ab in sorted(ABs, reverse=True):
    if t >= 0:
        break
    ans += 1
    t += ab
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]
ABs = [a - b for a, b in zip(As, Bs)]
if sum(As) < sum(Bs):
    print(-1)
    exit()
mABs = [ab for ab in ABs if ab < 0]
pABs = [ab for ab in ABs if ab > 0]
ans, t = len(mABs), sum(mABs)
for pab in sorted(pABs, reverse=True):
    if t >= 0:
        break
    ans += 1
    t += pab
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]
ABs = [a - b for a, b in zip(As, Bs)]
if sum(As) < sum(Bs) or min(ABs) > 0:
    print(-1)
    exit()
mABs = [ab for ab in ABs if ab < 0]
pABs = [ab for ab in ABs if ab > 0]
ans, t = len(mABs), sum(mABs)
for pab in sorted(pABs, reverse=True):
    if t >= 0:
        break
    ans += 1
    t += pab
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def compare(lstA, lstB):
    lst_fail = [b - a for a, b in zip(lstA, lstB) if a<b]
    lst_pass = sorted([a - b for a, b in zip(lstA, lstB) if a>=b], reverse=True)
    return lst_fail, lst_pass

def change_count(lst_fail, lst_pass):
    sum_short = sum(lst_fail)
    cnt = 0
    scores = 0
    for score in lst_pass:
        if scores >= sum_short:
            break
        else:
            scores += score
            cnt += 1
    return cnt


lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))

lst_fail, lst_pass = compare(lst_A, lst_B)

cnt_f = len(lst_fail)
cnt_p_change = change_count(lst_fail, lst_pass)

if sum(lst_A) < sum(lst_B):
    print(-1)
else:
    print(cnt_f + cnt_p_change)

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

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

if all(map(lambda x: x[0] >= x[1], zip(A,B))):
    print(0)
    exit(0)

residual = []
required = []
ans_num_changed = 0


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

tarinai_all = sum(required)
ans_num_changed += len(required)

residual = sorted(residual, reverse=True)

for e,r in enumerate(residual, 1):
    tarinai_all -= r
    ans_num_changed += 1
    if tarinai_all <= 0:
        break
print(ans_num_changed)

Submission status: Accepted

Solution code:
import numpy as np

N = input()
a_list = np.array(list(map(int, input().split())))
b_list = np.array(list(map(int, input().split())))

#  # すべての準備度が必要度を上回っている場合
#  if all([a >= b for a, b in zip(a_list, b_list)]):
#      print(0)
#  # 準備度が足りていない試験がある場合
#  else:
# そもそも準備度の総量が足りない場合
if sum(a_list) < sum(b_list):
    print(-1)
else:
    # 準備度を移動すれば受かる場合
    # 一番数の大きいところから足りていないところに割り振る戦略
    # a_listをcopy
    # 準備度の余裕をリストに格納
    diff_list = a_list - b_list
    # 昇順にソート
    diff_list.sort()
    start = 0
    end = -1
    current_value = diff_list[end]
    while diff_list[start] < 0:
        # 最大値から減算
        current_value += diff_list[start]
        while current_value < 0:
            end -= 1
            current_value += diff_list[end]
        start += 1
    if start == 0:
        print(0)
    else:
        print(start - end)

Submission status: Accepted

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


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

ans=0
L=[]
short_sum=0

for a,b in zip(A,B):
	if a<b:
		short_sum+=(b-a)
		ans+=1
	elif a>b:
		L.append(a-b)

L.sort(reverse=True)

if short_sum ==0:
	print 0
	quit()


for i, x in  enumerate(L):
	short_sum-=x
	if short_sum<=0:
		ans+=i+1
		break

print ans

Submission status: Accepted

Solution code:
n = int(input())

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

plus_list = []
minus_list = []
count = 0

for i in range(n):
    if a[i] < b[i]:
        count += 1
        minus_list.append(b[i]-a[i])
    elif a[i] > b[i]:
        plus_list.append(a[i]-b[i])

c = sum(minus_list)
plus_list.sort(reverse = True)

for num in plus_list:
    if c <= 0:
        break
    else:
        c -= num
        count += 1

if c > 0:
    print(-1)
else:
    print(count)

Submission status: Accepted

Solution code:
# C
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
#print(N)#
#print(A)#
#print(B)#

def zeros(n): return [0 for i in range(n)]

m = 0
count = 0
for i in range(N):
    if A[i]<B[i]: 
        count += 1
        m += A[i] - B[i]
#print(m, count)#
D = zeros(N)
for i in range(N): D[i] = [max(A[i] - B[i], 0), i]
D.sort()
D.reverse()
# mの分だけDの大きいものから移動
#print(D)#
for i in range(N):
    if m>=0: break
    m += D[i][0]
    count += 1
else: count = -1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    S = 0
    count = 0
    list_A = []
    
    for i in range(N):
        d = B[i] - A[i] #AをB以上にしないといけない
        if (0 < d):
            S += d
            count += 1
        else:
            if (d < 0):
                list_A.append(-d)
    list_A.sort()

    if (count == 0):
        return 0

    for s in reversed(list_A):
        S -= s
        count += 1
        if (S < 0):
            return count
    return -1
print(main())

Submission status: Accepted

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

avail = []
need = 0
res = 0
sumA = 0
sumB = 0

for i in range(N):
	sumA += A[i]
	sumB += B[i]
	if A[i] > B[i]:
		avail.append(A[i] - B[i])
	if A[i] < B[i]:
		need += B[i] - A[i]
		res += 1

if sumA < sumB:
	print(-1)

else:
	avail.sort(reverse=True)
	avail.append(0)
	for i in avail:
		if need <= 0:
			print(res)
			break
		res += 1
		need -= i

Submission status: Accepted

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

C = []
for i in range(len(A)):
    C.append(B[i] - A[i])

if sum(C) > 0:
    print(-1)

else:
    ans = 0
    dif = 0
    for i in C:
        if i > 0:
            ans += 1
            dif += i

    for i in sorted(C):
        if dif <= 0:
            break

        ans += 1
        dif += i

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

D = [ai - bi for ai, bi in zip(A, B)]
M = [di for di in D if di < 0]
P = [di for di in D if di > 0]

sum_m = sum(M)
if sum_m == 0:
    print(0)
    exit()

P.sort(reverse=True)
for i, pi in enumerate(P):
    sum_m += pi
    if sum_m >= 0:
        print(len(M) + i + 1)
        exit()
print(-1)

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

D = [ai - bi for ai, bi in zip(A, B)]
M = [di for di in D if di < 0]
P = [di for di in D if di > 0]

sum_m = sum(M)
if sum_m == 0:
    print(0)
    exit()

P.sort(reverse=True)
for i, pi in enumerate(P):
    sum_m += pi
    if sum_m >= 0:
        print(len(M) + i + 1)
        exit()
print(-1)

Submission status: Accepted

Solution code:
from itertools import accumulate
import sys
input = sys.stdin.readline

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

D = [ai - bi for ai, bi in zip(A, B)]
M = [di for di in D if di < 0]
P = [di for di in D if di > 0]

sum_m = sum(M)
if sum_m == 0:
    print(0)
    exit()

P = accumulate(sorted(P, reverse=True))
for i, pi in enumerate(P):
    if pi + sum_m >= 0:
        print(len(M) + i + 1)
        exit()
print(-1)

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

D = [ai - bi for ai, bi in zip(A, B)]
M = [di for di in D if di < 0]
P = [di for di in D if di > 0]

sum_m = sum(M)
if sum_m == 0:
    print(0)
    exit()

P = accumulate(sorted(P, reverse=True))
for i, pi in enumerate(P):
    if pi + sum_m >= 0:
        print(len(M) + i + 1)
        exit()
print(-1)

Submission status: Accepted

Solution code:
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)]
    plus = [x for x in C if x > 0]
    minus = [x for x in C if x < 0]
    change = sum(minus)
    count = 0
    if change == 0:
        return 0

    for i in sorted(plus,reverse=True):
        if change >= 0:
            break
        change += i
        count += 1
    return count + len(minus)

print(solve())

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 = [x-y for x,y in zip(a,b)]
p = [z for z in c if z > 0]
m = [z for z in c if z < 0]

f = sum(m)
l = len(m)
for k in sorted(p, reverse=True):
  if f >= 0:
    break
  f += k
  l += 1
print(l)

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:
    D=[]
    d=0
    a=0
    for i in range(len(A)):
        c=A[i]-B[i]
        D.append(c)
        if c<0:
            a+=1
            d-=c
    D.sort()
    j=1
    while d>0:
        if d>D[-j]:
            a+=1
            d-=D[-j]
            j+=1
        else:
            a+=1
            break
    print(a)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
win=[]
lose=[]
if sum(a)<sum(b):
  print(-1)
else:
  for i in range(n):
    if a[i]<b[i]:
      lose.append(b[i]-a[i])
    else:
      win.append(a[i]-b[i])
  if len(lose)==0:
    print(0)
  else:
    win.sort()
    win.reverse()
    x=sum(lose)
    ct=0
    for j in range(len(win)):
      ct+=win[j]
      if ct>=x:
        break
    print(j+len(lose)+1)

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()))
if sum(a) < sum(b):
	print(-1)
else:
	excess = []
	needed_num = 0
	needed_score = 0
	for i in range(n):
		if a[i] > b[i]:
			excess.append(a[i]-b[i])
		elif a[i] < b[i]:
			needed_num += 1
			needed_score += b[i]-a[i]

	if needed_num == 0:
		print(0)
	else:
		excess.sort(reverse=True)
		e_sum = [excess[0]]
		for i in range(1, len(excess)):
			e_sum.append(e_sum[-1]+excess[i])
		ind = bisect_left(e_sum, needed_score)
		print(needed_num + ind+1)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    if sum(A) < sum(B):
        return -1
    AB = [a - b for a, b in zip(A, B)]
    plus = [x for x in AB if x > 0]
    minus = [x for x in AB if x < 0]
    t = sum(minus)
    ans = len(minus)
    for m in sorted(plus, reverse=True):
        if t >= 0:
            break
        t += m
        ans += 1
    return ans
print(solve())

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


N = ii()
A = il()
B = il()
S = 0
diff = []
num_i = 0
for a, b in zip(A, B):
    if a < b:
        S += b - a
        num_i += 1
    else:
        diff.append(a - b)
diff.sort(reverse=True)
if num_i == 0:
    print(0)
    exit()
c = 0
for i in range(len(diff)):
    S -= diff[i]
    c += 1
    if S < 0:
        break
else:
    print(-1)
    exit()
print(num_i + c)