Problem:


Score : 400 points

Problem Statement

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

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

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

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

For every i, B_i \leq C_i holds.

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

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

A_i and B_i are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3
2 3 5
3 4 1

Sample Output 1

3

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

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

Sample Input 2

3
2 3 3
2 2 1

Sample Output 2

0

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

Sample Input 3

3
17 7 1
25 6 14

Sample Output 3

-1

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

Sample Input 4

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

Sample Output 4

5



Sample input:
3
2 3 5
3 4 1


Sample output:
3

Submission status: Wrong Answer

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

    # start = time.time()
    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])
    Sumlist = [sum(sa[N:i-1:-1]) for i in range(N+1)]
    print(Sumlist[len(sa)])

    for i in range(len(sa),insert_index-1,-1):
        # print(i)
        # print(sa[len(sa)-1:i-1:-1])
        if abs(Sum) <= Sumlist[i]:
            print(insert_index - 1 + N - i + 1)
            # elapsed_time = time.time() - start
            # print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
            exit()
    # elapsed_time = time.time() - start
    # print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
    print(-1)

Submission status: Wrong Answer

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

import numpy as np

def solve():
    N = int(input())
    A = np.array(list(map(int,input().split())))
    B = np.array(list(map(int,input().split())))
    if A.sum() < B.sum():
        res = -1
    else:
        C = B-A
        d = np.maximum(C,0).sum()
        M = np.minimum(np.maximum(C,0),1).sum()
        C.sort()
        for c in C:
            d += c
            M += 1
            if d <= 0:
                break
        res = M
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

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

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

neg = [v for v in c if v < 0]
total = sum(neg)
num = len(neg)

if num == 0:
    print(0)
else:
    for v in reversed(c[num:]):
        total += v
        num += 1
        if total >= 0:
            break
    print(num)

Submission status: Wrong Answer

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

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

neg = [v for v in c if v < 0]
total = sum(neg)
num = len(neg)

if num == 0:
    print(0)

for v in reversed(c[num:]):
    total += v
    num += 1
    if total >= 0:
        break

print(num)

Submission status: Wrong Answer

Solution code:
#!/Users/takahiro.kida/git/atcorder/venv/bin/python

import numpy
import scipy

from collections import defaultdict
from pprint import pprint

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

diff = [0 for x in range(N)]

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

if (sum(diff) < 0):
    print (-1)
else:
    shakkin = 0
    ans = 0
    for d in diff:
        if d < 0:
            shakkin += d
            ans += 1
    for d in sorted(diff, reverse=True):
        if shakkin >= 0:
            break
        print("diff: %s", d)
        ans += 1
        shakkin += d
        if shakkin >= 0:
            break
    print(ans)

Submission status: Wrong Answer

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

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

neg = [v for v in c if v < 0]
total = sum(neg)
num = len(neg)

for i, v in enumerate(reversed(c[num:])):
    total += v
    if total >= 0:
        break
    else:
        num += 1

print(num)

Submission status: Wrong Answer

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

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

diff = []

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

diff.sort()
ans = 0

if diff[0] >= 0:
    print(0)
    exit()

i = 0
j = n-1
ans += 2
while True:
    if i >= j:
        print(ans)
        exit()
    if diff[i] >= 0:
        print(ans)
        exit()
    if diff[j] <= 0:
        print(-1)
        exit()
    if diff[j] > -diff[i]:
        diff[j] += diff[i]
        i += 1
        if diff[i] < 0:
            ans += 1
    elif diff[j] < -diff[i]:
        diff[i] += diff[j]
        j -= 1
        if diff[j] > 0:
            ans += 1
    else:
        i += 1
        j -= 1
        if diff[i] < 0:
            ans += 1
        if diff[j] > 0:
            ans += 1

Submission status: Wrong Answer

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

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

neg = [v for v in c if v < 0]
total = sum(neg)
num = len(neg)

for i, v in enumerate(reversed(c[num:])):
    total += v
    if total >= 0:
        break

print(num + i + 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
An = map(int, input().split(maxsplit=N))
Bn = map(int, input().split(maxsplit=N))

minus = []
plus = []

for Ai, Bi in zip(An, Bn):
    if Ai > Bi:
        plus.append(Ai - Bi)
    elif Ai < Bi:
        minus.append(Ai - Bi)

if sum(plus) + sum(minus) < 0:
    print(-1)
    exit()


change_plus = 0
sum_minus = sum(minus)
while sum_minus < 0:
    sum_minus += plus.pop()
    change_plus += 1
print(str(change_plus + len(minus)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = sorted(a - b for a, b in zip(A, B))

def f():
    i, j = 0, len(D) - 1
    p = 0
    ans = 0
    print(D)
    while j >= i:
        # print(i, j, D[i], D[j], p)
        if D[i] >= 0:
            return ans
        if p >= -D[i]:
            p += D[i]
            i += 1
            ans += 1
            continue
        p += D[j]
        j -= 1
        ans += 1
    if p >= 0:
        return ans
    return -1

print(f())

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
if sum(A) < sum(B):
    print(-1)
    exit()
d = [A[i]-B[i] for i in range(N)]
print(d)
d.sort()
neg = 0
count = 0
for e in d:
    if e < 0:
        neg += e
        count += 1
        continue
    break
for e in d[::-1]:
    if neg >= 0:
        break
    neg += e 
    count += 1
print(count)

Submission status: Wrong Answer

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

if sum(a)>=sum(b):
    a=np.array(a)
    b=np.array(b)
    sabun=a-b
    husoku = sabun[sabun < 0]
    yojyo = sabun[sabun > 0]
    for i in range(len(yojyo)):
        if -1*sum(husoku)<sum(yojyo[0:i+1]):
            print(i+1+len(husoku))
            exit()

else:
    print(-1)

Submission status: Wrong Answer

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

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

# -1になっている数を数える
minus = [x for x in delta if x < 0]
plus = [x for x in delta if x >= 0]
plus.reverse()
min_sum = sum(minus)
min_sum = min_sum * -1

num = -1
plu_sum = 0

if min_sum == 0:
    num = 0
else:
    for i in range(len(plus)):
        plu_sum += plus[i]
        if plu_sum >= min_sum:
            num = i + 1 + len(minus)
            break

print(num)

Submission status: Wrong Answer

Solution code:
n = input()
ali = list(map(int, input().split()))
bli = list(map(int, input().split()))

asum = sum(ali)
bsum = sum(ali)
if asum < bsum:
  print('-1')
  exit()

ng = 0
dif = 0
plus = []

for (a,b) in zip(ali,bli):
  if a < b:
    ng += 1
    dif += (b-a)
  else:
    plus.append(a-b)
    
if ng == 0:
  print('0')
  exit()

plus.sort(reverse=True)

n = 1
for z in plus:
  if z < dif:
    dif -= z
    n += 1
  else:
    break

print(ng+n)

Submission status: Wrong Answer

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

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

if shortage < 0:
	for i in [x for x in delta if x > 0]:
		shortage += i
		ans += 1
		if shortage >= 0: break

if shortage >= 0:
	print(ans)
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    if sum(A) < sum(B):
        return -1

    d = [0]*N
    for i in range(N):
        d[i] = A[i] - B[i]
    d.sort()
    e = 1
    s = 0
    ans = 0
    amari = 0
    fusoku = d[s]
    while s <= N:
        if d[s] >= 0:
            break
        if amari == 0:
            amari = d[-e]
            e += 1
            ans += 1
        if fusoku + amari < 0:
            amari = 0
            fusoku += amari
        else:
            ans += 1
            amari += fusoku
            s += 1
            fusoku = d[s]
    return ans
        

if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

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 = np.sort(diff)

    for ind in range(len(diff)):
        if diff[ind] > 0:
            break

    minus = np.sum(diff[:ind])
    minus_num = ind

    yoyuu = diff[ind:][::-1]

    for ind in range(len(yoyuu)):
        minus += yoyuu[ind]
        if minus > 0:
            ans = minus_num + ind + 1
            break
        if minus == 0:
            ans = minus_num + ind
            break

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    """
    1 <= N <= 10^5
    1 <= Ai,Bi <= 10^9
    """
    N = int(input())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    ans = f(N, A, B)
    print(ans)


def f(N, A, B):
    diff = [b - a for a, b in zip(A, B)]
    diff = sorted(diff)
    diff_sum = sum(diff)

    if diff_sum > 0:
        return -1

    if len([d for d in diff if d <= 0]) == N:
        return 0

    start = 0
    end = N - 1
    used = [False] * N
    while start < end:
        ds = diff[start]
        de = diff[end]
        if ds <= 0 and de <= 0:
            break

        if ds < 0 and de > 0:
            cost = min(abs(ds), de)
            diff[start] += cost
            diff[end] -= cost

            print(diff)

            used[start] = True
            used[end] = True

            if diff[start] >= 0:
                start += 1
            if diff[end] <= 0:
                end -= 1

    ans = sum(used)
    return ans


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math
import copy




def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l

n=nCast(input())
a=nCast(input().split())
b=nCast(input().split())
c=list()
def func():
    result=0
    if sum(a)<sum(b):
        return -1
    for idx in range(0,len(a)):
        c.append(a[idx]-b[idx])
    c.sort()
    need=0
    result=0
    print(c)
    for number in c:
        if number<0:
            need+=number
            result+=1
        else:
            break
    if need!=0:
        return 0
    for number in reversed(c):
        result+=1
        need+=number
        if need>=0:
            break
    return result

print(func())

Submission status: Wrong Answer

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 = np.sort(diff)

    for ind in range(len(diff)):
        if diff[ind] > 0:
            break

    minus = np.sum(diff[:ind])
    minus_num = ind

    yoyuu = diff[ind:][::-1]

    for ind in range(len(yoyuu)):
        minus += yoyuu[ind]
        if minus >= 0:
            break

    ans = minus_num + ind + 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
ali = list(map(int, input().split()))
bli = list(map(int, input().split()))

asum = sum(ali)
bsum = sum(ali)
if asum < bsum:
  print('-1')
  exit()

ng = 0
dif = 0
plus = []

for (a,b) in zip(ali,bli):
  if a < b:
    ng += 1
    dif += (b-a)
  else:
    plus.append(a-b)

plus.sort(reverse=True)

n = 1
for z in plus:
  if z < dif:
    dif -= z
    n += 1

print(ng+n)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
diff = [0 for _ in range(N)]
for i in range(N):
    diff[i] = A[i] - B[i]
diff = sorted(diff)
diff_copy = diff.copy()

i = 0
j = N-1

flag = True
while True:
    if diff[i]<0:
        while True:
            if i>=N or j<0:
                flag = False
                break

            if abs(diff[i])>diff[j]:
                diff[i] += diff[j]
                diff[j] = 0
                j -= 1
            else:
                diff[j]-=abs(diff[i])
                diff[i] = 0
                break
        i += 1
    else:
        break



if flag:
    ans = 0
    for i in range(N):
        if diff_copy[i]!=diff[i]:
            ans += 1
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = tuple(map(int, input().split()))
b_list = tuple(map(int, input().split()))
a_sum = sum(a_list)
b_sum = sum(b_list)

if b_sum > a_sum:
    print(-1)
    exit()

change = False
ans = 0
for i in range(N):
    a = a_list[i]
    b = b_list[i]
    if i == N-1 and b > a:
        change = True
        ans += 2
    elif b > a:
        change = True
        ans += 1

if change:
    print(ans + 1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
df = list([0 for i in range(n)])
ans = -1

if sum(a) < sum(b):
    ans = -1
else:
    for i in range(n):
        df[i] = a[i] - b[i]
    minus = list(filter(lambda x: x < 0, df))
    plus = list(filter(lambda x: x > 0, df))
    m_count = len(minus)
    m_sum = sum(minus)
    plus.sort(reverse=True)

    if m_count == 0:
        ans = 0
    else:
        print(minus)
        print(plus)
        for i in range(len(plus)):
            m_sum += plus[i]
            if m_sum >= 0:
                break

        if m_sum>=0:
            ans = m_count + i + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())), np.int64)
B = np.array(list(map(int, input().split())), np.int64)
if sum(B) > sum(A):
    print(-1)
else:
    diff = A - B
    sort_idx = diff.argsort()
    # A = A[sort_idx]
    # B = B[sort_idx]
    diff = diff[sort_idx]
    l = 0
    r = N-1
    ans = sum(diff < 0)
    if ans != 0:
        ans += 1
    while True:
        if diff[l] >= 0:
            break
        if diff[r] >= -diff[l]:
            diff[r] += diff[l]
            diff[l] = 0
            l += 1
        else:
            r -= 1
            ans += 1
    print(ans)

Submission status: Wrong Answer

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

num = 0
diff = 0
for a, b in zip(A, B):
  if a < b:
    num += 1
    diff -= (b - a)

if diff == 0:
  print(0)
else:
  for a, b in zip(A, B):
    if a > b:
      diff += (a - b)
      num += 1
      if diff >= 0:
        break

  #print(diff)
  if diff < 0:
    print(-1)
  else:
    print(num)

Submission status: Wrong Answer

Solution code:
N = raw_input().split()
N = int(N[0])
a_list = raw_input().split()
a_list = [int(x) for x in a_list]
b_list = raw_input().split()
b_list = [int(x) for x in b_list]
if sum(a_list) < sum(b_list):
    print -1
else:
    diff_list = []
    for a, b in zip(a_list, b_list):
        diff_list.append(a-b)
    print diff_list
    minus_sum = sum([x for x in diff_list if x < 0])
    minus_len = len([x for x in diff_list if x < 0])
    plus_list_sorted = sorted([x for x in diff_list if x > 0], reverse=True)
    tmp_idx = 0
    print minus_len
    for item in plus_list_sorted:
        if minus_sum >= 0:
            break
        minus_sum += item
        tmp_idx += 1
    print minus_len + tmp_idx

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
B = list(map(int,input().split(" ")))
if sum(A)-sum(B) < 0:
    print(-1)
    exit()
C = []
ans = 0
count = 0
for i in range(N):
    C.append(A[i]-B[i])
    if A[i]-B[i] < 0:
        ans += 1
        count += A[i]-B[i]
    elif A[i]-B[i] > 0:
        C.append(A[i]-B[i])
    else:
        continue
if count == 0:
    print(0)
    exit()
C.sort()
C.reverse()
for i in range(len(C)):
    count += C[i]
    ans += 1
    if count >= 0:
        print(ans)
        exit()
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
diff = [0 for _ in range(N)]
for i in range(N):
    diff[i] = A[i] - B[i]
diff = sorted(diff)
diff_copy = diff.copy()

i = 0
j = N-1

flag = True
while True:
    if diff[i]<0:
        while True:
            if i>=N or j<0:
                flag = False
                break
            if abs(diff[i])>diff[j]:
                diff[i] + diff[j]
                j -= 1
            else:
                diff[j]-=abs(diff[i])
                diff[i] = 0
                break
        i += 1
    else:
        break


if flag:
    if diff_copy[j]==diff[j]:
        print(N-(j-i)-1)
    else:
        print(N-(j-i))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
dif = [a[i] - b[i] for i in range(n)]
dif_abs = [abs(dif[i]) for i in range(n)]
sorted_dif = sorted(dif)
dif_minus = [(dif_abs[i] - dif[i]) // 2 for i in range(n)]
total_minus = sum(dif_minus)
len_minus = sum(x>0 for x in dif_minus)

res = 0

if sum(a) < sum(b):
    print(-1)
else:
    if len_minus == 0:
        print(res)
    else:
        res += len_minus
        while True:
            if total_minus <= 0:
                break
            total_minus -= sorted_dif.pop()
            res += 1

    print(res)

Submission status: Wrong Answer

Solution code:
def solve():
    _ = input()
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    d = [x-y for x, y in zip(a, b)]

    # print(d)

    if sum(d) < 0:
        return -1

    neg = [d for d in d if d < 0]
    pos = sorted([x for x in d if x > 0], reverse=True)

    ans = len(neg)

    # calc cumsum
    cumsum = [0] * len(pos)
    cumsum[0] = pos[0]
    for i in range(1, len(pos)):
        cumsum[i] = cumsum[i-1] + pos[i]
    
    i = 0
    n = sum(neg)
    while n< 0:
        n+= cumsum[i]
        i+=1
    ans += i
    return ans


print(solve())

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

n = int(input())
a = [int(i) for i  in input().split()]
b = [int(i) for i  in input().split()]

pos = [a[i] - b[i] for i in range(n) if a[i] - b[i] >= 0]
neg = [a[i] - b[i] for i in range(n) if a[i] - b[i] < 0]
pos.sort()

pval = sum(pos)
nval = sum(neg)

print(pos)
print(neg)
if pval + nval < 0:
    print(-1)
else:
    ans = 0
    while(nval < 0):
        nval += pos.pop()
    
    print(n-len(pos))

Submission status: Wrong Answer

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

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

q = []
p = []
d = 0

for i in range(n):
    if a[i] - b[i] < 0:
        q.append(a[i]-b[i])
    elif a[i] - b[i] > 0:
        p.append(a[i]-b[i])
    else:
        d += 1

s = sum(q)
while s < 0:
    if p == []:
        print(-1)
        exit()
    else:
        t = p.pop()
        s += t

print(n - d - len(p))

Submission status: Wrong Answer

Solution code:
def solve():
    _ = input()
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    D = [x-y for x, y in zip(A, B)]

    if sum(D) < 0:
        return -1

    m = max(D)
    neg = [d for d in D if d < 0]
    pos = [x for x in D if x > 0]

    ans = len(neg)

    # calc cumsum
    cumsum = [0] * len(pos)
    cumsum[0] = pos[0]
    for i in range(1, len(pos)):
        cumsum[i] = cumsum[i-1] + pos[i]
    
    i = 0
    n = sum(neg)
    while n< 0:
        n+= cumsum[i]
        i+=1
    ans += i
    return ans


print(solve())

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

n = int(input())
a = [int(i) for i  in input().split()]
b = [int(i) for i  in input().split()]

pos = [a[i] - b[i] for i in range(n) if a[i] - b[i] >= 0]
neg = [a[i] - b[i] for i in range(n) if a[i] - b[i] < 0]
pos.sort

pval = sum(pos)
nval = sum(neg)

#print(pos)
#print(neg)
if pval + nval < 0:
    print(-1)
else:
    ans = 0
    while(nval < 0):
        nval += pos.pop()
    
    print(n-len(pos))

Submission status: Wrong Answer

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

prepare_sum = sum(A)
dif_sum = sum(B)
if prepare_sum < dif_sum:
    print(-1)

husoku = []
enough = []
idx = 0
for a, b in zip(A,B):
    if a < b:
        husoku.append(idx)
    else:
        enough.append(idx)
    idx += 1
    
residual = sorted(np.array(A)[enough] - np.array(B)[enough])

score = 0
budget = prepare_sum - dif_sum
for r in residual:
    if r <= budget:
        score += 1
        budget -= r
    else:
        break
print(len(A) - score)

Submission status: Wrong Answer

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

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

x = 0
for i in range(n):
    if a[i] < b[i]:
        x += 1

print(x)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = [int(a) for a in input().split()]
b_list = [int(a) for a in input().split()]
# N = 3
# a_list = [17, 7, 1]
# b_list = [25, 6, 14]

if sum(a_list) < sum(b_list):
    print("-1")
else:
    d_list = [a - b for a, b in zip(a_list, b_list)]
    d_list.sort()
    minus = [d for d in d_list if d < 0]
    deficit = sum(minus)
    ans = len(minus)
    if deficit == 0:
        print("0")
    else:
        for i in range(N):
            deficit += d_list[-i]
            if deficit >= 0:
                ans += i + 1
                break
        print(str(ans))

Submission status: Time Limit Exceeded

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

# from numba import njit

# input = stdin.readline

# @njit
def solve(n,a,b):
  delta = [b[i] - a[i] for i in range(n)]
  need = list(sorted(x for x in delta if x > 0))
  amari = list(sorted((abs(x) for x in delta if x < 0),reverse=True))
  if sum(need) > sum(amari):
    return -1
  k = 0
  s = 0
  while s < sum(need):
    s += amari[k]
    k += 1
  return k + len(need)
  


def main():
  N = int(input())
  # N,M = map(int,input().split())
  a = list(map(int,input().split()))
  b = list(map(int,input().split()))
  print(solve(N,a,b))
  return

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[43]:


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


# In[44]:


if sum(A) < sum(B):
    ans = -1
else:
    diff_plus = sorted([B[i]-A[i] for i in range(N) if B[i]-A[i]>0])
    diff_minus = sorted([A[i]-B[i] for i in range(N) if A[i]-B[i]>0],reverse=True)
    if len(diff_plus) > 0:
        j = 0
        cnt = len(diff_plus)
        for i,x in enumerate(diff_plus):
            while sum(diff_minus[:j+1]) < x:
                j += 1
            diff_minus[j] = sum(diff_minus[:j+1])-x
            for k in range(j):
                diff_minus[k] = 0
        ans = j+1+cnt
    else:
        ans = 0
print(ans)


# In[ ]:

Submission status: Time Limit Exceeded

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

excess = []
shortage = []

ans = 0

for i in range(n):
    if a[i] >= b[i]:
        excess.append(a[i] - b[i])
    else:
        shortage.append(b[i] - a[i])

ans = len(shortage)

if sum(excess) < sum(shortage):
    print(-1)
elif len(shortage) == 0:
    print(0)
else:
    excess.sort(reverse=True)
    excess_num = 0
    for i in range(n):
        excess_num += excess[i]
        ans += 1
        if excess_num >= sum(shortage):
            print(ans)
            exit()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

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

S_ba = []
S_ab = []
for i in range(N):
    if A[i] < B[i]: S_ba.append(B[i] - A[i])
    else: S_ab.append(A[i] - B[i])
    
ans = 0
tot = 0
S_ab.sort(reverse=True)
for i in range(len(S_ab)):
    # tot += A[i] - B[i]
    if sum(S_ba) <= tot: break
    tot += S_ab[i]
    ans += 1

if sum(S_ba) > sum(S_ab): print(-1)
else : print(len(S_ba) + ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit

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

@njit
def func():
  if np.all(a>=b):
    print(0)
  elif np.sum(a)<np.sum(b):
    print(-1)
  else:
    temp_a = a[a-b<0]
    temp_b = b[a-b<0]
    sum_a = np.sum(temp_a)
    sum_b = np.sum(temp_b)
    ans = len(temp_a)

    if sum_a>=sum_b:
      print(ans)
    else:
      aa = a[a-b>=0]
      bb = b[a-b>=0]
      for _ in range(n-ans):
        index = np.argmax(aa-bb)
        sum_a += aa[index]
        sum_b += bb[index]
        ans += 1
        if sum_a>=sum_b:
          print(ans)
          break
        else:
          aa[index]=0
          bb[index]=0
          continue
func()

Submission status: Time Limit Exceeded

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

if np.all(a>=b):
  print(0)
elif np.sum(a)<np.sum(b):
  print(-1)
else:
  temp_a = a[a-b<0]
  temp_b = b[a-b<0]
  sum_a = np.sum(temp_a)
  sum_b = np.sum(temp_b)
  ans = len(temp_a)

  if sum_a>=sum_b:
    print(ans)
  else:
    aa = a[a-b>=0]
    bb = b[a-b>=0]
    for _ in range(n-ans):
      index = np.argmax(aa-bb)
      sum_a += aa[index]
      sum_b += bb[index]
      ans += 1
      if sum_a>=sum_b:
        print(ans)
        exit()
      else:
        aa[index]=0
        bb[index]=0
        continue

Submission status: Time Limit Exceeded

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
if SB>SA:
    print(-1)
    exit()
for a,b in zip(A,B):
    if a>b:
        C.append(a-b)
    elif a==b:
        cnt+=1
for c in sorted(set(C)):
    if S>=C.count(c)*c:
        cnt+=C.count(c)
        S-=C.count(c)*c
    else:
        cnt+=S//c
        S=0
    if S==0:
        break
print(N-cnt)