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 = list(map(int,input().split()))
b_list = list(map(int,input().split()))
if sum(a_list)<sum(b_list):
    print(-1)
else:
    diff_list = [a_list[i]-b_list[i] for i in range(n)]
    cnt=0
    for diff in diff_list:
        if diff<0:
            cnt+=1
    while min(diff_list)<0:
        M = max(diff_list)
        for i in range(n):
            dif = diff_list[i]
            if dif<0 and M+dif>=0:
                M+=dif
                diff_list[i]=0
            elif M+dif<0:
                break
        cnt+=1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
L_over = []
L_short = []
for i in range(n):
    c = A[i] - B[i]
    if c > 0:
        L_over.append(c)
    elif c < 0:
        L_short.append(-c)
L_over.sort(reverse=True)
if sum(L_over) < sum(L_short):
    print(-1)
else:
    cnt = len(L_short)
    p = 0
    for i in range(len(L_over)):
        if p >= sum(L_short):
            break
        p += L_over[i]
        cnt += 1
    print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, *d = map(int, open(0).read().split())
s = [b-a for a,b in zip(d[:n],d[n:]) if b>a]
m = c = 0
if s:
  for i in sorted(a-b for a,b in zip(d[:n],d[n:]) if a>=b)[::-1]:
    m += i
    c += 1
    if sum(s)<=m:
      print(len(s)+c)
      exit()
  print(-1)
else:
  print(0)

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)
else:
    before = [A[i] - B[i] for i in range(N)]
    before.sort()
    diff = [A[i] - B[i] for i in range(N)]
    # print(diff)
    diff.sort()
    left = 0
    right = N - 1
    while not all([x >= 0 for x in diff]):
        if diff[left] < 0:
            temp = diff[left]
            diff[left] = min(0, diff[left] + diff[right])
            diff[right] -= diff[left] - temp
        if diff[left] == 0:
            if diff[left + 1] < 0:
                left += 1
        if diff[right] == 0:
            if diff[left] < 0:
                right -= 1
    # print(diff)
    cnt = 0
    for i in range(N):
        if before[i] != diff[i]:
            cnt += 1
    print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0
asum = sum(a)
c = []
csum = 0
num = set()
for i,j in zip(a,b):
	if i<j:
		i = i + (j-i)
		ans += 1
	if i>j:
		num.add(i-j)
	csum += i


while csum>asum and len(num):
	i = max(num)
	num.remove(i)
	csum -= i
	ans += 1

if csum>asum: ans = -1
	
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from bisect import insort_left

def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    
    if sum(A) < sum(B):
        print(-1)
        exit()

    ok = []
    ng_cnt = 0
    ng_num = 0
    for i in range(N):
        if A[i] - B[i] >= 0:
            insort_left(ok, A[i] - B[i])
        else:
            ng_cnt += 1
            ng_num += B[i] - A[i]
    ans = ng_cnt

    while ng_num > 0:
        ans += 1
        ng_num -= ok.pop()

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = np.array([int(i) for i in input().split()])
    b = np.array([int(i) for i in input().split()])

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

    diff = a - b
    org_diff = np.array(diff)

    def move(num):
        max_i = np.argmax(diff)
        max_v = diff[max_i]
        if max_v > num:
            diff[max_i] -= num
        else:
            diff[max_i] = 0
            move(num - max_v)

    for i in range(n):
        if diff[i] >= 0:
            continue
        move(-diff[i])
        diff[i] = 0
    diff -= org_diff
    print(sum([1 for i in diff if i != 0]))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

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

    diff = [0] * n
    for i in range(n):
        diff[i] = a[i] - b[i]
    org_diff = diff[:]

    def move(num):
        max_v = max(diff)
        max_i = diff.index(max_v)
        if max_v > num:
            diff[max_i] -= num
        else:
            diff[max_i] = 0
            move(num - max_v)

    for i in range(n):
        if diff[i] >= 0:
            continue
        move(-diff[i])
        diff[i] = 0
    print(sum([1 for i in range(n) if diff[i] != org_diff[i]]))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(s) for s in input().split()]
B = [int(o) for o in input().split()]
S = []
C = []
global memory
t = False
count = 0
if sum(B) > sum(A):
    print("-1")
else:
    for i in range(n):
        S.append(A[i]-B[i])
        C.append(B[i])
    while min(S)<0:
        ma = max(S)
        may = S.index(ma)
        mi = min(S)
        miy = S.index(mi)
        if t == True:
            if S[memory] > 0:
                ma = S[memory]
                may = memory
        if ma>=abs(mi):
            S[miy] = 0
            S[may] += mi
        else:
            S[may] = 0
            S[miy] += ma
        memory = may
        t = True
    for t in range(len(C)):
        C[t] += S[t]
        if C[t] != A[t]:
            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()))
diff = [a-b for a,b in zip(A, B)]
orderdiff = sorted(diff, reverse=True)

# 浮いている分からマイナスを補填、何個使ったかを管理するためのリスト
plsdiffcnt = []
for i in orderdiff:
  if i > 0:
    plsdiffcnt.append(i + sum(plsdiffcnt))

#マイナス値の合計とその個数を取得
mnscnt = 0
mnssum = 0
for i in diff:
  if i < 0:
    mnscnt += 1
    mnssum -= i
    
if all([i >= 0 for i in diff]):
  print(0)
  exit()
    
if sum(diff) < 0:
  print(-1)
  exit()
     
for i, v in enumerate(plsdiffcnt):
  if mnssum <= v:
    ans = i + 1
    break
print(mnscnt+ans)

Submission status: Time Limit Exceeded

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

diff = [0] * (N)
diffsum = 0
for i in range(N):
    diff[i] = A[i] - B[i]
    diffsum += diff[i]
if diffsum < 0:
    print("-1")
    exit()
diff.sort()
change = [0] * (N)
for i in range(N):
    if diff[i] < 0:
        change[i] = 1
        for j in range(N)[::-1]:
            if diff[j] > 0:
                change[j] = 1
                ch = min(-diff[i], diff[j])
                diff[i] += ch
                diff[j] -= ch
                if diff[i] == 0:
                    break
print(sum(change))

Submission status: Time Limit Exceeded

Solution code:
# Ai>=Bi 出来るならそのままにする
# Ai < Bi 仕方ないので溢れたぶんを使う
# 「変更したAi」の最小値が欲しいので、使うのは余ってるやつが多い順に使ったほうがいい

n = int(input())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))

lack, lack_i = 0, []
extra, extra_i = 0, []
for i in range(n):
    if arr[i] < brr[i]:
        lack += brr[i] - arr[i]
        lack_i.append([i, brr[i] - arr[i]])
    else:
        extra += arr[i] - brr[i]
        extra_i.append([i, arr[i] - brr[i], False])

if lack > extra:
    print(-1)
    exit()

# 大量に余っているやつから優先的に使う
extra_i = list(sorted(extra_i, key=lambda x: x[1], reverse=True))
ans = 0

l_i = 0
e_i = 0

while True:
    if l_i >= len(lack_i):
        break
    e_i = 0
    while True:
        if lack_i[l_i][1] <= extra_i[e_i][1]:
            extra_i[e_i][2] = True  # 一回でも使用した
            ans += 1  #
            extra_i[e_i][1] -= lack_i[l_i][1]
            break
        else:
            extra_i[e_i][2] = True  # 一回でも使用した
            e_i += 1
            lack_i[l_i][1] -= extra_i[e_i][1]
            extra_i[e_i][1] = 0
        if e_i > len(extra_i):
            break  # ここにくるときがよくわからんけど
    l_i += 1

bonus = len(list(filter(lambda x: x[2] == True, extra_i)))
print(ans + bonus)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = []
chang_point = 0
min_count = 0
e = 0
f = -1

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

    d = sorted(d)
    if d[0]>=0:
        print(0)
    else:
        for i in range(n-1):
            if d[i]<0 and d[i+1]>=0:
                change_point = i + 1

        while(abs(sum(d[0:change_point]))> e):
            e = e + d[f]
            min_count = min_count + 1
            f = f-1
        
        min_count = min_count + change_point
        print(min_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))

#+は過剰、-は不足
sub_list = []
for i in range(len(a_list)):
	sub_list.append(a_list[i]-b_list[i])

if sum(sub_list) < 0:
	print(-1)
else:
	l_neg = [i for i in sub_list if i < 0]
	l_pos = [i for i in sub_list if i > 0]
	if len(l_neg) == 0:
		print(0)
	else:
		neg_sum = sum(l_neg) * -1
		pos_count = 0
		for i in range(len(l_pos)+1):
			if sum(l_pos[:i]) > neg_sum:
				pos_count = i
				break
		print(pos_count+len(l_neg))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from itertools import combinations
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a = np.array(a)
b = np.array(b)

ans = 0
if np.sum(a) < np.sum(b):
  ans = -1
elif np.sum((a - b) >= 0) == n:
  ans = 0
else:
  for i in range(2, n + 1):
    for c in combinations([r for r in range(n)], i):
      a_temp = np.delete(a, list(c))
      b_temp = np.delete(b, list(c))
      if np.sum((a_temp - b_temp) >= 0) == n - i:  
        if np.sum(a[list(c)]) >= np.sum(b[list(c)]):
            ans = i
            break
    if ans > 0:
      break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
siken_kazu=int(input())
junbido=[int(i) for i in input().split(" ")]
goukakudo=[int(i) for i in input().split(" ")]
yoyudo=[junbido[i]-goukakudo[i] for i in range(siken_kazu)]
shoki_yoyudo=copy.deepcopy(yoyudo)
k=0
hantei=2
if sum(junbido)<sum(goukakudo):
    k=-1
elif min(yoyudo)>=0:
    pass
else:
    while hantei>1:
        hantei=0
        a=max(yoyudo)
        for i in range(siken_kazu):
            if yoyudo[i]==a:
                kiroku=i
                break
        for i in range(siken_kazu):
            if yoyudo[kiroku]==0:
                break
            elif yoyudo[i]<0:
                hantei=2
                if yoyudo[kiroku]>=-yoyudo[i]:
                    yoyudo[kiroku]-=-yoyudo[i]
                    yoyudo[i]=0
                elif yoyudo[kiroku]<-yoyudo[i]:
                    yoyudo[kiroku]=0
                    yoyudo[i]+=-yoyudo[kiroku]
for i in range(siken_kazu):
    if shoki_yoyudo[i]!=yoyudo[i]:
        k+=1
print(k)

Submission status: Time Limit Exceeded

Solution code:
import copy
siken_kazu=int(input())
junbido=[int(i) for i in input().split(" ")]
goukakudo=[int(i) for i in input().split(" ")]
yoyudo=[junbido[i]-goukakudo[i] for i in range(siken_kazu)]
shoki_yoyudo=copy.deepcopy(yoyudo)
k=0
if sum(junbido)<sum(goukakudo):
    k=-1
elif min(yoyudo)>=0:
    pass
else:
    while min(yoyudo)<0:
        a=max(yoyudo)
        for i in range(siken_kazu):
            if yoyudo[i]==a:
                kiroku=i
                break
        for i in range(siken_kazu):
            if yoyudo[kiroku]==0:
                break
            elif yoyudo[i]<0:
                if yoyudo[kiroku]>=-yoyudo[i]:
                    yoyudo[kiroku]-=-yoyudo[i]
                    yoyudo[i]=0
                elif yoyudo[kiroku]<-yoyudo[i]:
                    yoyudo[kiroku]=0
                    yoyudo[i]+=-yoyudo[kiroku]
for i in range(siken_kazu):
    if shoki_yoyudo[i]!=yoyudo[i]:
        k+=1
print(k)

Submission status: Time Limit Exceeded

Solution code:
exam = [[],[]]
N = int(input())
exam[0] = list(map(int, input().split()))
exam[1] = list(map(int, input().split()))

diff = [exam[0][n] - exam[1][n] for n in range(N)]

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


minsum = 0
counter = 0
for j in diff:
    if j < 0:
        minsum = minsum + j
        counter = counter + 1


while minsum < 0:
    minsum = minsum + max(diff)
    counter = counter + 1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = input()
N = int(N)
exam = [[],[]]
for i in range(2):
    x = input()
    exam[i] = [int(x.split()[i]) for i in range(N)]

diff = [exam[0][n] - exam[1][n] for n in range(N)]

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


minsum = 0
counter = 0
for j in diff:
    if j < 0:
        minsum = minsum + j
        counter = counter + 1


while minsum < 0:
    minsum = minsum + max(diff)
    counter = counter + 1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = input()
N = int(N)
exam = []
for i in range(2):
    x = input()
    exam.append([int(x.split()[i]) for i in range(N)])

diff = [exam[0][n] - exam[1][n] for n in range(N)]
diff.sort(reverse=True)

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

i = N - 1
minsum = 0
_counter = 0
while diff[i] < 0:
    minsum = minsum + diff[i]
    i = i - 1
    _counter = _counter + 1

counter = 0
while minsum < 0:
    minsum = minsum + diff[counter]
    counter = counter + 1

print(counter + _counter)

Submission status: Time Limit Exceeded

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

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

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()
    while na > 0 and k:
        na -=k[-1]
        k.pop(-1)
        c +=1
    print(c)

Submission status: Time Limit Exceeded

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

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:
        ar2.sort(reverse = True)
        temp = 0
        b = 0
        for  i in range(len(ar2)):
            temp += ar2[i]
            if temp >= sum(ar1):
                b = i+1
                break
        print(len(ar1)+b)

Submission status: Time Limit Exceeded

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:
        ar1.sort(reverse = True)
        ar2.sort(reverse = True)
        temp = 0
        b = 0
        for  i in range(len(ar2)):
            temp += ar2[i]
            if temp >= sum(ar1):
                b = i+1
                break
        print(len(ar1)+b)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def main():
    listA=[]
    while True:
        try:
            listA.append(list(map(int,input().split())))
        except:
            break;
    N = listA[0][0]
    A = listA[1]
    B = listA[2]

    if sum(A) < sum(B):
        print(-1)
        return
    C = []

    C = np.array(A) - np.array(B)
    C = C.tolist()
    change_list = [i for i in C if i < 0]
    change_count = len(change_list)
    if change_count == 0:
        print(0)
        return
    max_ = 0
    while True:
        change_count += 1
        max_ += max(C)
        if abs(sum(change_list)) < max_:
            break
        C[C.index(max(C))] = 0

    print(change_count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
listA=[]
while True:
    try:
        listA.append(list(map(int,input().split())))
    except:
        break;
N = listA[0][0]
A = listA[1]
B = listA[2]

if sum(A) < sum(B):
    print(-1)
    exit()
C = []
for i in range(len(A)):
    num = A[i] - B[i]
    C.append(num)
change_list = [i for i in C if i < 0]
change_count = len(change_list)
if change_count == 0:
    print(0)
    exit()
max_ = 0
while True:
    change_count += 1
    max_ += max(C)
    if abs(sum(change_list)) < max_:
        break
    C[C.index(max(C))] = 0

print(change_count)

Submission status: Time Limit Exceeded

Solution code:

# ===== Main ==== #
def main():
    # =Input Space = #
    listA=[]
    while True:
        try:
            listA.append(list(map(int,input().split())))
        except:
            break;
    N = listA[0][0]
    A = listA[1]
    B = listA[2]

    if sum(A) < sum(B):
        print(-1)
        return 0
    C = []
    for i in range(len(A)):
        num = A[i] - B[i]
        C.append(num)
    D = C.copy()
    change_list = [i for i in C if i < 0]
    change_count = len(change_list)

    while True:
        if len([i for i in change_list if i < 0]) == 0:
            break
        max_ = max(C)
        max_index = C.index(max_)
        count = -1
        for i in change_list:
            count += 1
            if i < 0:
                if max_ <= abs(i):
                    change_list[count] += max_
                    max_ = 0
                    break
                else:
                    change_list[count] = 0
                    max_ -= abs(i)
        C[max_index] = max_
        change_count += 1

    print(change_count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

# ===== Main ==== #
def main():
    # =Input Space = #
    listA=[]
    while True:
        try:
            listA.append(list(map(int,input().split())))
        except:
            break;
    N = listA[0][0]
    A = listA[1]
    B = listA[2]

    if sum(A) < sum(B):
        print(-1)
        return 0
    C = []
    for i in range(len(A)):
        num = A[i] - B[i]
        C.append(num)
    D = C.copy()

    while True:
        if len([i for i in C if i < 0]) == 0:
            break
        max_ = max(C)
        max_index = C.index(max_)
        count = -1
        for i in C:
            count += 1
            if i < 0:
                if max_ <= abs(i):
                    C[count] += max_
                    max_ = 0
                    break
                else:
                    C[count] = 0
                    max_ -= abs(i)
        C[max_index] = max_

    count = 0
    for i in range(len(C)):
        if D[i] != C[i]:
            count += 1
    print(count)


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

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


if sum(a)>=sum(b):
    for i in range(len(s1)+1):
        if sum(sorted(s1, reverse=True)[0:i])>=sum(s2):
            print(i+len(s2))
            exit()
else:
    print(-1)

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()]
count=0
if sum(A)<sum(B):
    print(-1)
else:
    dif=[A[i]-B[i] for i in range(N)]
    if min(dif)>=0:
        print(0)
    else:
        difsort=sorted(dif)
        pluslist=[i for i in difsort if i>=0]
        zero=difsort.index(pluslist[0])
        minum=len(difsort[:zero])
        maxsort=difsort[::-1]
        absum=abs(sum(difsort[:zero]))
        sumlist=[sum(maxsort[0:i+1]) for i in range(len(pluslist))]
        while sumlist[count]<absum:
            count+=1
        
        print(count+1+minum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
score = list(map(int, input().split()))
sum_score = sum(score)
target = list(map(int, input().split()))
sum_target = sum(target)
sub = list(map(lambda x: score[x] - target[x], range(n)))
cnt = sum(1 for x in sub if x < 0)
sub.sort()
sub.reverse()

sub_new = [0]
for i in range(n):
    sub_new.append(sub_new[i] + sub[i])

if(sum_score > sum_target):
    if(cnt > 0):
        a, b, ans = 0, 0, 1
        tmp = sub_new[1+a]
        while sum(1 for x in sub if x < 0) > 0:
            if(tmp + sub[-(b+1)] >= 0): 
                tmp += sub[-(b+1)]
                sub[-(b+1)] = 0
                b+=1
            else: 
                sub[-(b+1)] -= tmp
                a+=1
                tmp = sub_new[1+a]
                ans += 1
            #print(tmp, sub, ans)
        print(cnt + ans)
    else: print(0)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = list(map(int,input().split()))
c = list(map(int,input().split()))
d = []
su  = 0
for i in range(a):
	d.append(b[i]-c[i])
	if b[i]-c[i] < 0:
		su += b[i]-c[i]

d.sort()
k = 0
j = 1
for i in range(a):
	if d[i] > 0:
		su += d[i]
		while d[i]:
			if abs(d[-j]) < d[i]:
				d[i] +=  d[-j]
				j += 1
				k += 1
			else:
				d[-j] += d[i]
		k += 1

if su < 0:
	print(-1)
else:
	print(k)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
now = input().split()
after = input().split()
for i in range(n):
    now[i] = int(now[i])
    after[i] = int(after[i])
lack_list = []
amari2_list = []      
if sum(now) < sum(after):
    print(-1)
else:
    for i in range(n):
        if now[i]<after[i]:
            lack_list.append(after[i]-now[i])
        elif now[i]>after[i]:
            amari2_list.append(now[i]-after[i])
    amari2_list.sort(reverse=True)
    count = 0;t = 0
    while sum(lack_list)> t:    
        t += amari2_list[count] 
        count += 1 
    ans = len(lack_list)  + count
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
asum = 0
bsum = 0
for i in range(n):
  asum = asum + a[i]
  bsum = bsum + b[i]

if asum < bsum:
  print(-1)
else:
  c = 0
  for i in range(n):
    if a[i] >= b[i]:
      c = c + 1
  if c == n:
    print(0)
  else:
    p = asum - bsum
    while p > 0:
      m = 20000000000
      t = -1
      for i in range(n):
        if a[i] > b[i]:
          if a[i] - b[i] < m:
            m = a[i] - b[i]
            t = i
      b[t] = a[t] if m < p else (m - p)
      p = p - m
    c = 0
    for i in range(n):
      if a[i] != b[i]:
        c = c + 1
    print(c)

Submission status: Time Limit Exceeded

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

if sum(a) < sum(b):
    res = -1
else:
    c = [a[i] - b[i] for i in range(n)]
    c1 = [i if i > 0 else 0 for i in c]
    c2 = sum([i if i < 0 else 0 for i in c])
    c3 = sum([1 if i < 0 else 0 for i in c])
    if c2 < 0:
        c1.sort(reverse=True)
        for i in range(n):
            if sum(c1[0:i]) + c2 > 0:
                res = c3 + i
                break

print(res)

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()]
count=0
if sum(A)<sum(B):
    print(-1)
else:
    dif=[A[i]-B[i] for i in range(N)]
    if min(dif)>=0:
        print(0)
    else:
        difsort=sorted(dif)
        pluslist=[i for i in difsort if i>=0]
        zero=difsort.index(pluslist[0])
        minum=len(difsort[:zero])
        maxsort=difsort[::-1]
        while sum(maxsort[0:count+1])<abs(sum(difsort[:zero])):
            count+=1
        
        print(count+1+minum)

Submission status: Time Limit Exceeded

Solution code:
N=input()
A=map(int,raw_input().split())
B=map(int,raw_input().split())
unmatch=[]
need=0

for i in range(N):
    if A[i]<B[i]:
        unmatch.append(i)
        need+=B[i]-A[i]

Gap=[A[i]-B[i] for i in range(N) if i not in unmatch]
Gap.sort(reverse=True)
Ans=len(unmatch)

while need>0:
    if Ans>=N:
        print "-1"
        exit()
    need-=Gap[Ans-len(unmatch)]
    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()))
c = sorted([a[i] - b[i] for i in range(n)])
k = sum(x < 0 for x in c)

if sum(c) < 0:
  print(-1)
elif 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:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

diff_m = 0
diff_p = []
mcount = 0

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

else:
    for i in range(N):
        difftmp = A[i] - B[i]
        
        if difftmp < 0:
            diff_m += difftmp
            mcount += 1
        
        else:
            diff_p.append(difftmp)
    

    if diff_m == 0:
        print(0)

    else:
        diff_p.sort(reverse = True)

        for j in range(0,N):
            a = sum(diff_p[:j])
 

            if diff_m + a >= 0:
                print(j+mcount)
                break

            else:
                continue

Submission status: Time Limit Exceeded

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



B2=[[b,i,A[i]] for i,b in enumerate(B)]
B2.sort(reverse=True)
used=[]
gap=0
Ans=0
for b in B2:
    if b[1] not in used and A[b[1]]+gap>=b[0]:
        used.append(b[1])
        if b[0]>A[b[1]]:
            Ans+=1
            gap-=max(0,b[0]-A[b[1]])
    else:
        koho=np.where(A>=b[0])[0]
        koho=list(set(koho)-set(used))
        if len(koho)==0:
            print "-1"
            exit()
        koho_B=np.array([B[k] for k in koho])
        used.append(koho[koho_B.argmin()])
        gap+=A[koho[koho_B.argmin()]]-b[0]
        B[b[1]]=0
        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()))

if sum(B) > sum(A):
    print(-1)
else:
    diff = [a-b for a,b in zip(A,B)]
    plus = []
    minus = []
    equal = []
    for d in diff:
        if d >= 0:
            plus.append(d)
        elif d < 0:
            minus.append(d)
    lack = sum(minus)
    while lack < 0:
        lack += max(plus)
        plus.remove(max(plus))
    print(N-len(plus))