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: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Cm = []
Cp = []
for i in range(N):
	if(A[i]-B[i]<0):
		Cm.append(A[i]-B[i])
	elif(A[i]-B[i]>0):
		Cp.append(A[i]-B[i])
Cm.sort()
Cp.sort()

cnt = 0
nowCp = 0
j = 0

for i in range(len(Cp)):

	if(sum(Cp)<abs(sum(Cm))):
		print(-1)
		break
	elif(len(Cm) == 0):
		print(0)
		break

	nowCp += int(Cp[len(Cp)-(i+1)])
	while(j<len(Cm)):
		if(nowCp>=abs(Cm[len(Cm)-j-1])):
			nowCp -= abs(Cm[len(Cm)-j-1])
			cnt += 1
			j += 1
		else:
			break

	if(cnt == len(Cm)):
		print(cnt+i+1)
		break

Submission status: Time Limit Exceeded

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()
k = 0
neg = 0

if sum(a) < sum(b):
  print(-1)
else:
  k = sum(x < 0 for x in c)
  
  if k == 0:
    print(0)
  else:
    for i in range(n):
      if sum(c[len(c) - 1 - i:]) >= -sum(c[:k]):
        print(k + i + 1)
        break

Submission status: Time Limit Exceeded

Solution code:
import sys

test_num = int(input())
prep = [int(i) for i in input().split()]
gokaku = [int(i) for i in input().split()]
prep_sum = sum(prep)
gokaku_sum = sum(gokaku)

ans = [0 for i in range(test_num)]

# -1
if gokaku_sum > prep_sum:
    print(-1)
    sys.exit()

amari = prep_sum-gokaku_sum

diff = [prep[i]-gokaku[i] for i in range(test_num)]
amari_idx = []
husoku_idx = []
for i, item in enumerate(diff):
    if item < 0:
        husoku_idx.append(i)
    else:
        amari_idx.append(i)

# 0
if len(husoku_idx) == 0:
    print(0)
    sys.exit()

# >0
husoku = [diff[husoku_idx_item] for husoku_idx_item in husoku_idx]
husoku_sum = sum(husoku)
output = len(husoku_idx)
ordered_amari = [diff[amari_idx_item] for amari_idx_item in amari_idx]
ordered_amari.reverse()
ct = len(ordered_amari)
for i in range(ct):
    output += 1
    if sum(ordered_amari[:(i+1)]) > -husoku_sum:
        break

print(output)
sys.exit()

Submission status: Time Limit Exceeded

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

not_enough_sum = 0
not_enough_count = 0
for a,b in zip(A, B):
  if a < b:
    not_enough_sum += b-a
    not_enough_count += 1
stock = [a-b for a,b in zip(A,B) if a > b]
stock = sorted(stock, reverse=True)
for i in range(len(stock)+1):
  if sum(stock[0:i]) >= not_enough_sum:
    print(i+not_enough_count)
    exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
B =list(map(int,input().split()))
if(sum(A) < sum(B)):
  print(-1)
  exit()
cnt = 0
for i in range(N):
  if(A[i] >= B[i]):
    cnt += 1
if(cnt == N):
  print(0)
  exit()
C = []
tmp = []
for b in B:
  for a in A:
    if(a not in tmp):
      tmp.append(a)
    if(a >= b):
      C.append(a)
      A.remove(a)
      break
ans = 0
for i in range(len(C)):
  if(C[i] != tmp[i]):
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

sumA = sum(A)
sumB = sum(B)

count = 0
idx = -1
tempIdx = -1
if sumA < sumB:
    print(-1)
else:
    for i in range(N):
        if A[i] < B[i]:
            tempIdx = A.index(max(A))
            A[idx] -= (B[i] - A[i])
            if idx != tempIdx:
                idx = tempIdx
                count += 1
            A[i] += (B[i] - A[i])
            count += 1
    print(count)

Submission status: Time Limit Exceeded

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()
k = 0
neg = 0

if sum(a) < sum(b):
  print(-1)
else:
  k = sum(x < 0 for x in c)
  neg = sum(c[:k])
  
  if k == 0:
    print(0)
  else:
    for i in range(n):
      if sum(c[len(c) - 1 - i:]) >= abs(neg):
        print(k + i + 1)
        break

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))
L = [A[i] - B[i] for i in range(N)]
ans = 0
if sum(A) < sum(B) :
    ans = -1
else :
    debt = []
    deposit = []
    for i in range(N) :
        if L[i] < 0 :
            debt.append(-L[i])
        elif L[i] > 0 :
            deposit.append(L[i])
        
    debt = sorted(debt)
    deposit = sorted(deposit,reverse=True)
    
    debt_duplicated = debt.copy()
    deposit_duplicated = deposit.copy()
    
    
    for i in range(len(debt)) :
        for j in range(len(deposit)) :
            if debt[i] <= deposit[j] :
                deposit[j] -= debt[i]
                debt[i] = 0
                break
            else :
                debt[i] -= deposit[j]
                deposit[j] = 0
    
    for i in range(len(debt)) :
        if debt[i] != debt_duplicated[i] :
            ans += 1
    for i in range(len(deposit)) :
        if deposit[i] != deposit_duplicated[i] :
            ans += 1

print(ans)
#print(debt)
#print(deposit)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
Alist=[int(i) for i in input().split(" ")]
Blist=[int(i) for i in input().split(" ")]
sumA=sum(Alist)
sumB=sum(Blist)
Dlist=[]
muinus=0
muinus_num=0
count=0
if sumA<sumB:
    print(-1)
else:
    for i in range(n):
        AB=Alist[i]-Blist[i]
        Dlist.append(AB)
        if AB<0:
            muinus+=AB
            muinus_num+=1
    while muinus<0:
        muinus+=max(Dlist)
        Dlist.remove(max(Dlist))
        count+=1
    print(muinus_num+count)

Submission status: Time Limit Exceeded

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

sumA = sum(A)
sumB = sum(B)

count = 0
idx = -1
tempIdx = -1
if sumA < sumB:
    print(-1)
else:
    for i in range(len(A)):
        if A[i] < B[i]:
            tempIdx = A.index(max(A))
            A[idx] -= (B[i] - A[i])
            if idx != tempIdx:
                idx = tempIdx
                count += 1
            A[i] += (B[i] - A[i])
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
a = [int(a[i]) for i in range(n)]
b = input().split()
b = [int(b[i]) for i in range(n)]
 
ap = []
am = 0
for i in range(n):
  if a[i]<b[i]: am = am + b[i]-a[i]
  else: ap = ap + [a[i]-b[i]]

ap = sorted(ap)
ap.reverse()
s = 0
c = 0
while s<am and c<len(ap):
  s = s + ap[c]
  c = c+1
 
if s>=am: print(c+n-len(ap))
else: print(-1)

Submission status: Time Limit Exceeded

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

not_enough = []
stock = []
for a,b in zip(A, B):
  if a < b:
    not_enough.append(b-a)
  elif a > b:
    stock.append(a-b)
stock = sorted(stock, reverse=True)
for i in range(len(stock)+1):
  if sum(stock[0:i]) >= sum(not_enough):
    print(i+len(not_enough))
    exit()
print(-1)

Submission status: Time Limit Exceeded

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()
under = []
over = []
ans = 0
i = 0
while i <N:
  if A[i] < B[i]:
    under.append(B[i]-A[i])
  if A[i] > B[i]:
    over.append(A[i]-B[i])
  i += 1
over.sort()
ans += len(under)
if ans == 0:
  print('0')
  exit()
mnt = 0
cnt = 0
while sum(under) > mnt:
  cnt += 1
  mnt = sum(over[-cnt:])
ans += cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

test_num = int(input())
prep = [int(i) for i in input().split()]
gokaku = [int(i) for i in input().split()]
prep_sum = sum(prep)
gokaku_sum = sum(gokaku)

ans = [0 for i in range(test_num)]

# -1
if gokaku_sum > prep_sum:
    print(-1)
    sys.exit()

amari = prep_sum-gokaku_sum

diff = [prep[i]-gokaku[i] for i in range(test_num)]
amari_idx = []
husoku_idx = []
for i, item in enumerate(diff):
    if item < 0:
        husoku_idx.append(i)
    else:
        amari_idx.append(i)

# 0
if len(husoku_idx) == 0:
    print(0)
    sys.exit()

# >0
husoku = [diff[husoku_idx_item] for husoku_idx_item in husoku_idx]
husoku_sum = sum(husoku)
output = len(husoku_idx)

ordered_amari = sorted([diff[amari_idx_item] for amari_idx_item in amari_idx], reverse=True)
ct = len(ordered_amari)
for i in range(ct):
    output += 1
    if sum(ordered_amari[:(i+1)]) > -husoku_sum:
        break

print(output)
sys.exit()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
diff = []
for i in range(N):
    diff.append(A[i] - B[i])
neg_diff = [i for i in diff if i < 0]
pos_diff = [i for i in diff if i > 0]
pos_diff.sort(reverse=True)
if abs(sum(neg_diff)) > abs(sum(pos_diff)):
    print(-1)
    exit(0)
else:
    if sum(neg_diff) == 0:
        print(0)
        exit(0)

for i in range(0,len(pos_diff)+1):
    if abs(sum(pos_diff[0:i])) > abs(sum(neg_diff)):
        print(len(neg_diff)+i)
        exit(0)

Submission status: Time Limit Exceeded

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

ap = []
am = 0
for i in range(n):
  if a[i]<b[i]: am = am + b[i]-a[i]
  else: ap = ap + [a[i]-b[i]]

ap.reverse()
s = 0
c = 0
while s<am and c<len(ap):
  s = s + ap[c]
  c = c+1

if s>=am: print(c+n-len(ap))
else: print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
 
if sum(a) < sum(b):
    print('-1')
    exit()
 
count = 0
sabun = [(i, a[i]-b[i]) for i, _ in enumerate(a) if a[i]<b[i]]
husoku = sum([x[1] for x in sabun])
a_sorted = sorted(a)
while husoku < 0:
    ma = a_sorted[-1]
    a_sorted = a_sorted[:-1]
    mai = a.index(ma)
    husoku += ma - b[mai]
    a[mai] = b[mai]
    count += 1
 
print(len(sabun) + count)

Submission status: Time Limit Exceeded

Solution code:
N = input()
a_list = list(map(int, input().split()))
b_list = 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
        c_list = [a for a in a_list]
        # 準備度の余裕をリストに格納
        diff_list = [c - b for c, b in zip(c_list, b_list)]
        # 一度減算し始めたら対象が0になるまで続ける
        selected_biggest_idx = diff_list.index(max(diff_list))
        while not all([c >= b for c, b in zip(c_list, b_list)]):
            if diff_list[selected_biggest_idx] == 0:
                selected_biggest_idx = diff_list.index(max(diff_list))
            min_idx = diff_list.index(min(diff_list))
            # 最大と最小で移し替えを行う
            if diff_list[selected_biggest_idx] < abs(diff_list[min_idx]):
                # 足りていない値のほうが大きい場合
                c_list[selected_biggest_idx] -= diff_list[selected_biggest_idx]
                c_list[min_idx] += diff_list[selected_biggest_idx]
            else:
                # 足りていない値のほうが小さい場合
                c_list[selected_biggest_idx] -= abs(diff_list[min_idx])
                c_list[min_idx] += abs(diff_list[min_idx])
            # diff_listを更新
            diff_list = [c - b for c, b in zip(c_list, b_list)]
        result_count = len([1 for a, c in zip(a_list, c_list) if a != c])
        print(result_count)

Submission status: Time Limit Exceeded

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

ans = -1

not_enough = []
enough = []

for i in range(N):
    if A[i] - B[i] < 0:
        not_enough.insert(0, abs(A[i] - B[i]))
    else:
        enough.insert(0, abs(A[i] - B[i]))

enough.sort()
enough.reverse()
not_enough.sort()
not_enough.reverse()

#print(enough)
#print(not_enough)

count = 0
whre = 0
res_eno = 0
res_not_eno = 0

if len(not_enough) == 0:
    print('0')
elif len(enough) == 0:
    print('-1')
else:
    for i in range(len(enough)):

        res_eno = enough[i]
        count += 1

        while res_eno > 0:
            #print("count" + (str)(count))
            #print("whre" + (str)(whre))
            #print("res_eno" + (str)(res_eno))
            #print("res_not_eno" + (str)(res_not_eno))

            if res_not_eno == 0:
                if res_eno >= not_enough[whre]:
                    #print('A')
                    res_eno -= not_enough[whre]
                    count += 1
                    whre += 1
                    if whre == len(not_enough):
                        break
                else:
                    #print('B')
                    res_not_eno = not_enough[whre] - res_eno
                    res_eno = 0
            else:
                if res_eno >= res_not_eno:
                    #print('C')

                    res_eno -= res_not_eno
                    res_not_eno = 0
                    count += 1
                    whre += 1
                    if whre == len(not_enough):
                        break
                else:
                    #print('D')

                    res_not_eno = res_not_eno - res_eno
                    res_eno = 0
        if whre == len(not_enough):
            break

    if res_not_eno == 0 and whre == len(not_enough):
        print(count)
    else:
        print('-1')

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    AB = list(zip(A, B))
    bigger_diff = list(map(lambda ab: max(ab[0] - ab[1], 0), AB))
    lesser_diff = list(map(lambda ab: min(ab[0] - ab[1], 0), AB))
    exceeded = sum(bigger_diff)
    shortage = sum(lesser_diff)

    if exceeded < abs(shortage):
        print(-1)
        sys.exit()

    if all(map(lambda ab: ab[0] > ab[1], AB)):
        print(0)
        sys.exit()



    ans = len(list(filter(lambda d: d<0, lesser_diff)))

    bigger_diff = sorted(bigger_diff, reverse=True)

    for i in range(len(bigger_diff)):
        if sum(bigger_diff[:i]) >= abs(shortage):
            ans += i
            break
    print(ans)



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  As = map(int, input().split())
  Bs = map(int, input().split())

  diffs = [a - b for a, b in zip(As, Bs)]
  positives = list(sorted(filter(lambda x: x>0, diffs), reverse=True))
  negatives = list(sorted(filter(lambda x: x<0, diffs)))
  if abs(sum(negatives)) > sum(positives):
    print(-1)
    return
  max_i = -1
  for neg in negatives:
    for i, pos in enumerate(positives):
      neg += pos
      max_i = max(i, max_i)
      if neg >= 0:
        positives[i] = neg
        break
      else:
        positives[i] = 0
  print(len(negatives) + max_i + 1)


if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

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

count = 0
sabun = [(i, a[i]-b[i]) for i, _ in enumerate(a) if a[i]<b[i]]
husoku = sum([x[1] for x in sabun])

while husoku < 0:
    husoku += max(a) - b[a.index(max(a))]
    a[a.index(max(a))] = b[a.index(max(a))]
    count += 1

print(len(sabun) + count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
d = [a[i]-b[i] for i in range(n)]
e = [i for i in d if i > 0]
f = [i for i in d if i < 0]
e.sort(reverse = True)
f.sort()
if sum(d) < 0:
    print(-1)
else:
    if f == []:
        print(0)
    else:
        for i in range(len(e)+1):
            if sum(e[0:i]) >=  abs(sum(f)):
                print(i+len(f))
                break

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

    if all(map(lambda ab: ab[0] > ab[1], AB)):
        print(0)
        sys.exit()

    bigger_diff = list(map(lambda ab: max(ab[0] - ab[1], 0), AB))
    lesser_diff = list(map(lambda ab: min(ab[0] - ab[1], 0), AB))
    shortage = sum(map(lambda ab: min(ab[0] - ab[1], 0), AB))


    ans = len(list(filter(lambda d: d<0, lesser_diff)))

    bigger_diff = sorted(bigger_diff, reverse=True)

    for i in range(len(bigger_diff)):
        if sum(bigger_diff[:i]) >= abs(shortage):
            ans += i
            break
    print(ans)



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

    if all(map(lambda ab: ab[0] > ab[1], zip(A, B))):
        print(0)
        sys.exit()

    bigger_diff = list(map(lambda ab: max(ab[0] - ab[1], 0), zip(A, B)))
    lesser_diff = list(map(lambda ab: min(ab[0] - ab[1], 0), zip(A, B)))
    shortage = sum(map(lambda ab: min(ab[0] - ab[1], 0), zip(A, B)))


    ans = len(list(filter(lambda d: d<0, lesser_diff)))

    bigger_diff = sorted(bigger_diff, reverse=True)

    for i in range(len(bigger_diff)):
        if sum(bigger_diff[:i]) >= abs(shortage):
            ans += i
            break
    print(ans)



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
# N = input()   # 単純な入力
# input = list(input()) # ひとかたまりの文字列をリスト化
# N = int(input())  # 整数一つ
# input = input().split()   # スペース区切りを文字としてリスト化
# X,Y = map(int,input().split())  # ２個や３個の整数をそれぞれ取得
# input = list(map(int,input().split()))
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

sa = [A[i] - B[i] for i in range(len(A))]
sa.sort()
# print(sa)
insert_index = bisect.bisect_left(sa,0)
# print(insert_index)
Sum = sum(sa[0:insert_index])

for i in range(len(sa),insert_index-1,-1):
    # print(i)
    # print(sa[len(sa)-1:i-1:-1])
    if abs(Sum) <= sum(sa[N:i-1:-1]):
        print(insert_index - 1 + N - i + 1)
        exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  As = map(int, input().split())
  Bs = map(int, input().split())

  diffs = [a - b for a, b in zip(As, Bs)]
  positives = list(sorted(filter(lambda x: x>0, diffs), reverse=True))
  negatives = list(sorted(filter(lambda x: x<0, diffs)))
  if abs(sum(negatives)) > sum(positives):
    print(-1)
    return
  changed = set()
  for neg in negatives:
    for i, pos in enumerate(positives):
      neg += pos
      changed.add(i)
      if neg >= 0:
        positives[i] = neg
        break
      else:
        positives[i] = 0
  print(len(negatives) + len(changed))


if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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(0,N)]
C.sort()
p = 0
ans = 0
for i in range(0,N):
  if C[i] < 0:
    p += C[i]
    ans += 1
  else:
    break
  if p < 0:
    for j in range(N - 1,0,-1):
      if C[j] > 0:
        p += C[j]
        ans += 1
        C[j] = 0
        break

if sum(A) < sum(B):
  print(-1)
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

a = np.array(a)
b = np.array(b)

if np.sum(a) < np.sum(b):
    ans = -1
else:
    diff = a - b
    diff_ori = np.copy(diff)
    dames= np.where(diff < 0)[0]
    for d in dames:
        yoyuu = np.argmax(diff)
        diff[yoyuu] += diff[d]
        diff[d] -= diff[d]
    ans = len(diff) - np.sum(diff_ori == diff)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = input_list()
    B = input_list()
    C = [A[i] - B[i] for i in range(n)]
    s = sum(C)
    if s < 0:
        print(-1)
        exit()
    C.sort()
    ans = 0
    for i in range(n):
        if C[i] < 0:
            continue
        elif C[i] <= s:
            ans += 1
            s -= C[i]
        else:
            break
    print(n-ans)
    

def input_list():
    return list(map(int, input().split()))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
diff = [0]*N
sumA = 0
sumB = 0
for i in range(N):
    sa = A[i] - B[i]
    diff[i] = sa
    sumA += A[i]
    sumB += B[i]
if sumA < sumB:
    print(-1)
    exit()
diff.sort(reverse= True)
fixidx = -1
usedI = False
c = 0
usedJ = []
for i in range(N):
    usedI = False
    if diff[i] > 0:
        for j in range(fixidx,-N,-1):
            #print(i,j,diff[i],diff[j])
            if diff[j] < 0:
                if not usedI:
                    usedI = True
                    c += 1
                if diff[i] + diff[j] >= 0:
                    diff[i] += diff[j]
                    diff[j] = 0
                    usedJ.append(j)
                    fixidx = j+1
                    if diff[i] == 0:
                        break
#                    print(i,j,diff[i],diff[j])
                else:
                    diff[j] += diff[i]
                    diff[i] = 0
 #                   print(diff[j])
                    usedJ.append(j)
                    fixidx = j
#                    print(i, j, diff[i], diff[j])
                    break
            elif diff[j] >= 0:
                #print(usedJ)
                print(c+len(set(usedJ)))
                exit()
    else:
        break
#print(c,usedJ)
print(c+len(set(usedJ)))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

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

    plus = []
    totalsa = 0
    ans = 0
    for i in range(N):
        if A[i] > B[i]:
            plus.append(A[i] - B[i])
        elif A[i] < B[i]:
            totalsa += B[i] - A[i]
            ans += 1

    plus.sort(reverse=True)

    t = 0
    for i in range(len(plus)):
        if t >= totalsa:
            break
        ans += 1
        t += plus[i]

    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()))
s_a = sum(a)
s_b = sum(b)
if s_b > s_a:
    print(-1)
    exit()
dist_s = s_a - s_b
dist_list = []
for i in range(n):
    if a[i] >= b[i]:
        dist_list.append(a[i]-b[i])
dist_list.sort()
m = len(dist_list)
count = 0
ans = n
while dist_s > 0 and count < m:
    dist_s -= dist_list[count]
    ans -= 1
    count += 1
    if dist_s < 0:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
#87 C - Exam and Wizard
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)]
diff = sorted(diff,reverse = True)
negative_v = [i for i in diff if i < 0]
tot = sum(negative_v)

if sum(A) < sum(B):
    print(-1)
else:
    if tot >= 0:
        print(0)
    else:
        cnt = 0
        for d in diff:
            tot += d
            # tot が正になったら終了
            if tot < 0:
                cnt += 1
            else:
                cnt += 1
                break
        ans = cnt + len(negative_v)
        print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq 

input = sys.stdin.readline

n = int(input())

a_list = list(map(int, input().split()))

b_list = list(map(int, input().split()))

mainasu_a_b = 0

plus_a_b_list = []
ans = 0
for i in range(n):
    if a_list[i] - b_list[i] < 0:
        mainasu_a_b += a_list[i] - b_list[i]
        ans += 1
    else:
        plus_a_b_list.append(a_list[i] - b_list[i])

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

plus_a_b_list = list(map(lambda x: x*(-1), plus_a_b_list))
heapq.heapify(plus_a_b_list)
while mainasu_a_b < 0:
    if len(plus_a_b_list) == 0 and mainasu_a_b < 0:
        print(-1)
        exit(0)
    max_plus = heapq.heappop(plus_a_b_list)*(-1)
    mainasu_a_b += max_plus
    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()
ans=0
ab=[]
cd=[]
for i in range(n):
  m=a[i]-b[i]
  if m<0:
    ab.append(m)
  else:
    cd.append(m)
res=sum(ab)
cd.sort(reverse=True)
t=0
if len(ab)>0:
  while t<len(cd):
    res+=cd[t]
    t+=1
    if res>0:
      print(len(ab)+t)
      exit()
else:
  print(0)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
l=[]
x=0#b[i]より大きいa[i]の数
y=0#b[i]より小さいa[i]の数
z=0#負数の総和
for i in range(n):
    if a[i]>=b[i]:
        x+=1
        l.append(a[i]-b[i])
    else:
        y+=1
        z+=a[i]-b[i]
l.sort(reverse=True)

#print(x,y,z,l)
if x==n:
    print(0)
    exit()
ans=y
for i in range(x):
    z+=l[i]
    ans+=1
    if z>=0:
        print(ans)
        exit()
if z<0:
    print(-1)

Submission status: Accepted

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

insufficient = 0
yoyu = []

ans = 0
for i in range(n):
    p = A[i] - B[i]
    if p < 0:#足りない
        insufficient += abs(p)
        ans += 1
    else:
        yoyu.append(p)
yoyu.sort(reverse=True)

if sum(yoyu) < insufficient:
    print(-1)
    exit()

ind = 0    
while insufficient > 0:
    insufficient -= yoyu[ind]
    ind += 1

print(ans+ind)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a = LI()
    b = LI()
    need = 0
    ans = 0
    surplus = []
    for i in range(n):
        if a[i]>=b[i]:
            surplus.append(a[i]-b[i])
        else:
            need += b[i] - a[i]
            ans += 1
    
    surplus.sort(reverse=True)

    if need <= 0:
        print(0)
        exit(0)
    else:
        for r in surplus:
            need -= r
            ans += 1
            if need <= 0:
                print(ans)
                exit(0)
        
        print(-1)
main()

Submission status: Accepted

Solution code:
import sys
if sys.platform=='ios':
	sys.stdin=open('input_file.txt')
	
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

ans=0
if sum(A)<sum(B):
	ans=-1
	
else:
	cnt=0
	res=[]
	for i in range(N):
		if A[i]>=B[i]:
			res.append(A[i]-B[i])
		else:
			cnt+=B[i]-A[i]
			ans+=1
	if cnt>0:
		ans+=1
		res=sorted(res)[::-1]
		for i in range(len(res)):
			if res[i]<cnt:
				ans+=1
				cnt-=res[i]
			else:
				break

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

AB_list = sorted([A-B for A,B in zip(A_list, B_list)])
AB_q = deque(AB_list)

if sum(AB_list) < 0:
    print(-1)
    exit()
if AB_list[0] >= 0:
    print(0)
    exit()

cnt = 0
stock = 0
while len(AB_q) > 0:
    if stock <= 0:
        stock += AB_q.pop()
    else:
        lv = AB_q.popleft()
        if lv >= 0:
            break
        stock += lv
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 0
t = []
ans = 0
for i in range(n):
    if a[i] < b[i]:
        c += b[i] - a[i]
        ans += 1
    else:
        t.append(a[i]-b[i])
t.sort(reverse=True)
i = 0
while c > 0 and i < len(t):
    ans += 1
    c -= t[i]
    i += 1
if c > 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()))

if sum(a) < sum(b):
  print (-1)
  exit()
cnt = 0
d = [0] * n
s = 0
for i in range(n):
  d[i] = a[i]-b[i]
  if d[i] < 0:
    cnt += 1
    s += abs(d[i])
if cnt == 0:
  print (0)
  exit()

d.sort(reverse=True)

t = 0
for i in range(n):
  if s > d[i]:
    cnt += 1
    s -= d[i]
  else:
    cnt += 1
    break
print (cnt)

Submission status: Accepted

Solution code:
from sys import exit
import bisect

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(A) < sum(B):
    print(-1)
    exit()
L = [a - b for a, b in zip(A, B)]
L.sort()
x = bisect.bisect_left(L, 0)
low = abs(sum(L[:x]))
s = 0
ans = x  
L.reverse()
for l in L:
    if s >= low:
        break
    s += l
    ans += 1
print(ans)