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

count=0

d=[]
for i in range (N):
    _d=a[i]-b[i]
    d.append([i,_d])
d=sorted(d,key=lambda x: x[1])
z=min(d,key=lambda y: y[1])
while z[1]<0:
    if d[0][1]>=0:
        pass
    else:
        d[0][1]+=abs(d[0][1])
        d[-1][1]-=abs(d[0][1])
        count+=1
        d=sorted(d,key=lambda x: x[1])
        z=min(d,key=lambda y: y[1])
print(d)
print(count)

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

count=0

d=[]
for i in range (N):
    _d=a[i]-b[i]
    d.append([i,_d])
d=sorted(d,key=lambda x: x[1])
z=min(d,key=lambda y: y[1])
while z[1]<0:
    for j in range(N):
        if d[j][1]>=0:
            pass
        else:
            d[j][1]+=abs(d[j][1])
            d[-1][1]-=abs(d[j][1])
            count+=1
    d=sorted(d,key=lambda x: x[1])

print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ans = 1

import sys

if 0 in A:
  print(0)
  sys.exit()
  

for a in A:
  ans *= a

if ans > pow(10, 18):
  print(-1)
  sys.exit()

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0]
D = [0]

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

D = sorted(D)

csum = sum(C)
dsum = sum(D)

if(csum + dsum > 0):
    print(-1)
elif(csum == 0):
    print(0)
else:
    ans = len(C)
    for i in D:
        csum += i
        ans += 1
        if(csum <= 0):
            break
    print(ans)

Submission status: Wrong Answer

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

    remain = []
    lack = 0
    for i in range(N):
        diff = A[i] - B[i]
        if diff > 0:
            remain.append(diff)
        if diff < 0:
            lack += abs(diff)
            ans += 1

    if lack == 0:
        print("0")
        return
    
    sm = 0
    for r in remain:
        sm += r
        ans += 1
        if sm >= lack:
            print(ans)
            break
    else:
        print("-1")
    
if __name__ == '__main__':
    solve()

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

ans = 0
minus = 0
plus_list = []
for a, b in zip(A, B):
    if a < b:
        ans += 1
        minus += b - a
    plus_list.append(a - b)

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

plus_list_ = sorted(plus_list, reverse=True)

for p in plus_list:
    minus -= p
    ans += 1
    if minus < 0:
        break

print(ans)
#print(minus)
#print(plus_list)

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):
    ans = -1
else:
    d = [a[i]-b[i] for i in range(n)]
    d.sort()
    
    tmp = -1 if d[0] < 0 else 0
        
    for i in range(n):
        if d[i] < 0:
            if d[tmp] + d[i] >= 0:
                d[tmp] += d[i]
            else:
                while d[tmp] + d[i] < 0:
                    d[i] += d[tmp]
                    tmp -= 1
        else:
            ans = i - tmp
            break
            
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.stdin.readline

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
sa = []
sa_a = 0
sa_b = []
sa_c = 0
count = 0

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

sa.sort()

if sum(sa) <= 0:
    print(-1)
    sys.exit()
else:
    for i in range(N):
        if sa[i] <= 0:
            count += 1
            sa_a += abs(sa[i])
        else:
            sa_b.append(sa[i])

while sa_a > sa_c:
    sa_c += sa_b.pop()
    count += 1

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(hoge) for hoge in input().split()]
B = [int(hoge) for hoge in input().split()]
#Aiの総和を変えずに、なるべく少ない量のAiを変えて全部合格を目指す
if sum(B) > sum(A):
    print(-1)
else:
    diff = 0
    points = []
    ans = 0
    for i in range(N):
        a = A[i]
        b = B[i]
        if a < b:
            diff += b - a
            ans += 1
        elif a > b:
            points.append(a-b)
    for p in points:
        if diff <= 0:
            print(ans)
            exit()
        diff -= p
        ans += 1 
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(hoge) for hoge in input().split()]
B = [int(hoge) for hoge in input().split()]
#Aiの総和を変えずに、なるべく少ない量のAiを変えて全部合格を目指す
if sum(B) > sum(A):
    print(-1)
else:
    diff = 0
    points = []
    ans = 0
    for i in range(N):
        a = A[i]
        b = B[i]
        if a < b:
            diff += b - a
            ans += 1
        elif a > b:
            points.append(a-b)
    for p in points:
        if diff <= 0:
            print(ans)
            break
        diff -= p
        ans += 1

Submission status: Wrong Answer

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):
    K = -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
sorted(T)[::-1]
while S > 0:
    S -= T[0]
    del T[0]
    K += 1
print(K)

Submission status: Wrong Answer

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
    sorted(T)[::-1]
    while S > 0:
        S -= T[0]
        del T[0]
        K += 1
    print(K)

Submission status: Wrong Answer

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
    sorted(T)[::-1]
    while S > 0:
        S -= T[0]
        del T[0]
        K += 1
    print(K)

Submission status: Wrong Answer

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
    sorted(T)[::-1]
    while S > 0:
        S -= T[0]
        T.pop(0)
        K += 1
    print(K)

Submission status: Wrong Answer

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
    sorted(T)[::-1]
    while S > 0:
        S -= T[0]
        T.pop(0)
        K += 1
    print(K)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
s = 0
m = []
l = 0
if sum(B)<=sum(A):
    for i in range(N):
        if A[i] < B[i]:
            k += 1
            s += (B[i]- A[i])
        elif A[i] > B[i]+1:
            m.append(A[i]- B[i])
        elif A[i] == B[i]+1:
            l += (A[i]- B[i])
        n = sorted(m,reverse = True)
    for j in range(len(m)):
        if s > 0:
            s -= n[j]
            k += 1
        if s <0:
            break
    if (s > 0) and(k > 0):
         k += s
else:
    print(-1)
print(k)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
s = 0
m = []
l = 0
if sum(B)<=sum(A):
    for i in range(N):
        if A[i] < B[i]:
            k += 1
            s += (B[i]- A[i])
        elif A[i] > B[i]+1:
            m.append(A[i]- B[i])
        elif A[i] == B[i]+1:
            l += (A[i]- B[i])
    for j in range(len(m)):
        if s > 0:
            s -= m[j]
            k += 1
        if s <= 0:
            break
    if (s > 0) and(k > 0):
         k += s
else:
    k = -1
print(k)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
s = 0
m = []
l = 0
if sum(B)<=sum(A):
    for i in range(N):
        if A[i] < B[i]:
            k += 1
            s += (B[i]- A[i])
        elif A[i] > B[i]+1:
            m.append(A[i]- B[i])
        elif A[i] == B[i]+1:
            l += (A[i]- B[i])
    for j in range(len(m)):
        if s > 0:
            s -= m[j]
            k += 1
        if s <0:
            break
    if (s > 0) and(k > 0):
         k += s
else:
    k = -1
print(k)

Submission status: Wrong Answer

Solution code:

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
s = 0
m = 0
l = 0
if sum(B)<=sum(A):
    for i in range(N):
        if A[i] < B[i]:
            k += 1
            s += (B[i]- A[i])
        elif A[i] > B[i]+1:
            k += 1
            m += (A[i]- B[i])
        elif A[i] > B[i]:
            l += (A[i]- B[i])
print(k)

Submission status: Wrong Answer

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

ans = 0

if sum(a) >= sum(b):
    diff = [a[i]-b[i] for i in range(n)]
    diff.sort()
    shortage = 0
    for i in diff:
        if i < 0:
            ans += 1
            shortage += -i
        else:
            break
    idx = n-1
    while shortage > 0:
        shortage -= diff[idx]
        ans += 1
else:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
a = list( map(int, input().split()))
b = list( map(int, input().split()))
d = [0]*n

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

d.sort()

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

ans = 0
last=n-1
for neg in range(n):
    if d[neg]>=0:break
    else:
        ans+=1
        while d[neg]<0:
            tmp = min(-d[neg], d[last])
            d[neg] += tmp
            d[last] -= tmp
            if d[last] == 0:
                last-=1
                ans+=1

if d[last] != a[last]-b[last] and d[last]!=0 : ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l_a = list(map(int,input().split()))
l_b = list(map(int,input().split()))
if sum(l_a)<sum(l_b):
  print(-1)
  exit()
l_sa = sorted(list(i-j for i,j in zip(l_a,l_b)))
s = 0
cou =0
print(l_sa)
for i in l_sa:
  if i<0:
    s += i
    cou +=1
  else:
    break
#print(s,cou)
l_sb = l_sa[::-1]
for i in l_sb:
  if s<0:
    s += i
    cou += 1
 #   print(s,cou)
  else:
    break
print(cou)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

    surplus = []
    need = 0
    ans = 0
    for a, b in zip(A, B):
        diff = a - b
        if diff >= 0:
            surplus.append(diff)
        else:
            need += -diff
            ans += 1

    surplus.sort(reverse=True)
    cusum = [0] * (len(surplus) + 1)
    for i, sur in enumerate(surplus):
        cusum[i+1] = cusum[i] + sur

    ans = bisect_left(cusum, need)
    if ans > N:
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

    surplus = []
    need = 0
    ans = 0
    for a, b in zip(A, B):
        diff = a - b
        if diff > 0:
            surplus.append(diff)
        if diff < 0:
            need += -diff
            ans += 1

    surplus.sort(reverse=True)
    cusum = [0] * (len(surplus) + 1)
    for i, sur in enumerate(surplus):
        cusum[i+1] = cusum[i] + sur

    plus = bisect_left(cusum, need)
    ans += plus

    if plus == len(surplus):
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

    surplus = []
    need = 0
    ans = 0
    for a, b in zip(A, B):
        diff = a - b
        if diff > 0:
            surplus.append(diff)
        if diff < 0:
            need += -diff
            ans += 1

    surplus.sort(reverse=True)
    cusum = [0] * (len(surplus) + 1)
    for i, sur in enumerate(surplus):
        cusum[i+1] = cusum[i] + sur

    ans += bisect_left(cusum, need)
    
    if ans > N:
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()

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):
        print(-1)
        return

    sub = []
    t = 0
    cnt = 0
    for i in range(n):
        if a[i] < b[i]:
            cnt += 1
            t += b[i] - a[i]
        else:
            sub.append(a[i]-b[i])
    if cnt == 0:
        print(0)
        return

    ans = cnt
    i = 0
    while t > 0:
        ans += 1
        t -= sub[i]
        i += 1
    print(ans)








if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if diff_AB < 0:
	print(-1)
	exit()

Diff = []	
for i in range(n):
	diff = A[i] - B[i]
	Diff.append(diff)

Diff = sorted(Diff)
print(Diff)
res = 0
for i in range(n):
	if Diff[i] < 0:
		res += 1
	else:
			if Diff[i] <= diff_AB:
				diff_AB -= Diff[i]
			else:
				diff_AB = 0
				res += 1

if diff_AB > 0:
	res += 1

print(res)

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

c = []
t = 0
ans = 0
for x, y in zip(a,b):
    if x < y:
        t += y-x
        ans += 1
    else:
        c.append(x-y)

c.sort(reverse=True)
for p in c:
    t -= p
    ans += 1
    if t<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=[]
v=0
SUM=0
s=sum(A)
t=sum(B)
if s<t:
  print("-1")
else:
  for i in range(N):
    if A[i]-B[i]<0:
      SUM+=B[i]-A[i]
      v+=1
    elif A[i]-B[i]==0:
      v=v
    else:
      v=v
      C.append(A[i]-B[i])
C=sorted(C,reverse=True)
u=0
for i in range(len(C)):
  while SUM-C[i]>=0:
    SUM=SUM-C[i]
    u+=1
print(u+v)

Submission status: Wrong Answer

Solution code:
import copy

n=int(input())

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

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

tt1 = copy.copy(t1)
tt1.sort()
tt2 = copy.copy(t2)
tt2.sort()

for i in range(n):
    if tt1[i] < tt2[i]:
        print(-1)
        exit()

count = 0
for i in range(n):
    if t1[i] >= t2[i]:
        count += 1
    if count == n:
        print(0)
        exit()

t = 0
m = 0
L = []
for k in range(n):
    if t1[k] > t2[k]:
        L.append(t1[k]-t2[k])
    else:
        m += t2[k] - t1[k]
        t += 1

L = (sorted(L))[::-1]

for e in L:
    m -= e
    t += 1
    if m <= 0:
        break
print(t)

Submission status: Wrong Answer

Solution code:
import copy

n=int(input())

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

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

tt1 = copy.copy(t1)
tt1.sort()
tt2 = copy.copy(t2)
tt2.sort()

for i in range(n):
    if tt1[i] < tt2[i]:
        print(-1)
        exit()

count = 0
for i in range(n):
    if t1[i] >= t2[i]:
        count += 1
    if count == n:
        print(0)
        exit()

t = 0
m = 0
L = []
for k in range(n):
    if t1[k] > t2[k]:
        L.append(t1[k]-t2[k])
    else:
        m += t2[k] - t1[k]
        t += 1
print(L)
L = (sorted(L))[::-1]
print(L)
for e in L:
    m -= e
    t += 1
    if m <= 0:
        break
print(t)

Submission status: Wrong Answer

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

if sum(A)<sum(B):
  print(-1)
else:
  C.sort(key=lambda x:x[2])   #不足量が大きい方から順に
  #print(C)
  cnt=0
  diff=0
  for i in range(N):
    flag=False
    if diff!=0:
      C[i][2] += diff
      flag=True
    if C[i][2]<0:
      diff=C[i][2]
      C[i][2]=0
      flag=True
    if flag:
      cnt +=1
      
  #print(C)
  print(cnt)

Submission status: Wrong Answer

Solution code:
# にぶたんの臭いがする
# 変えなくてもいい(A[i]>=B[i])ならそれでいいが、変えなくてはいけないときに備えてプールしておく
# k個まで変えていいとし、探索終了後に余ったやつを、使ったやつ（A[i]-B[i]）を小さい順にならべたもの、に還元して返してやる 還元しきれなかった数は変えなきゃいけないやつなのでkと比較
# でいけるのか？
# 使った数をカウントしていけば途中で切れそう なので余剰分を大きい順にソートして順に使っていくと残ったやつは変える必要がない

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(B) > sum(A):
    print(-1)
    exit()

pre_ans = 0
difs = []
for i in range(N):
    if A[i] > B[i]:
        difs.append(A[i] - B[i])
    elif A[i] < B[i]:
        # 確実にかえなくてはいけないやつ
        pre_ans += 1
difs = sorted(difs, reverse=True)
# print("使っていくぶん: {}".format(difs))
len_difs = len(difs)

l, r = -1, N + 1
while r - l > 1:
    mid = (l + r) // 2
    count = 0  # 借りた数
    tmp = 0  # 使える分
    for i in range(N):
        if A[i] < B[i]:
            if tmp < (B[i] - A[i]):
                # 新たに借りる必要がある
                # print("足りないので借ります {}".format(difs[count]))
                tmp += difs[count]
                count += 1
            tmp -= B[i] - A[i]
            # print("tmp = {}".format(tmp))
        if mid <= count:
            # 使いすぎ問答無用
            # print("使いすぎたので反省")
            l = mid
            break
    if mid > count:
        # print("まだ使える")
        r = mid
print(l + pre_ans)

Submission status: Wrong Answer

Solution code:
# にぶたんの臭いがする
# 変えなくてもいい(A[i]>=B[i])ならそれでいいが、変えなくてはいけないときに備えてプールしておく
# k個まで変えていいとし、探索終了後に余ったやつを、使ったやつ（A[i]-B[i]）を小さい順にならべたもの、に還元して返してやる 還元しきれなかった数は変えなきゃいけないやつなのでkと比較
# でいけるのか？
# 使った数をカウントしていけば途中で切れそう なので余剰分を大きい順にソートして順に使っていくと残ったやつは変える必要がない

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(B) > sum(A):
    print(-1)
    exit()

pre_ans = 0
difs = []
for i in range(N):
    if A[i] > B[i]:
        difs.append(A[i] - B[i])
    elif A[i] < B[i]:
        # 確実にかえなくてはいけないやつ
        pre_ans += 1
difs = sorted(difs, reverse=True)
# print("使っていくぶん: {}".format(difs))
len_difs = len(difs)

l, r = 0, N + 1
while r - l > 1:
    mid = (l + r) // 2
    count = 0  # 借りた数
    tmp = 0  # 使える分
    for i in range(N):
        if A[i] < B[i]:
            if tmp < (B[i] - A[i]):
                # 新たに借りる必要がある
                # print("足りないので借ります {}".format(difs[count]))
                tmp += difs[count]
                count += 1
            tmp -= B[i] - A[i]
            # print("tmp = {}".format(tmp))
        if mid <= count:
            # 使いすぎ問答無用
            # print("使いすぎたので反省")
            l = mid
            break
    if mid > count:
        # print("まだ使える")
        r = mid
print(l + pre_ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    B = np.array(list(map(int, input().split())))
    C = A - B
    S = 0
    for c in C:
        if c < 0:
            S += -c

    ans = 0
    s = 0
    t = 0
    for c in C:
        if c < 0:
            ans += 1
        elif s < S:
            t += 1
            s += c
    if s < S:
        print(-1)
    elif ans == 0:
        print(0)
    else:
        print(ans + t)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    B = np.array(list(map(int, input().split())))
    C = A - B
    S = 0
    for c in C:
        if c < 0:
            S += -c

    ans = 0
    s = 0
    t = 0
    for c in C:
        if c < 0:
            ans += 1
        elif s < S:
            t += 1
            s += c
    if s < -S:
        print(-1)
    elif ans == 0:
        print(0)
    else:
        print(ans + t)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int,input().split()))
dum = s[0]
count = 0
for i in s:
  if i <= dum:
    count += 1
  dum = min(dum,i)
print(count)

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):
    result = -1
else:
    count = 0
    over = [0]
    result = 0
    for i in range(n):
        if a[i]>b[i]:
            over.append(a[i]-b[i])
        elif a[i]<b[i]:
            count += b[i]-a[i]
            result += 1
    over.sort(reverse=True)
    i = 0
    while count > 0:
        result += 1
        count -= over[i]
print(result)

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):
    result = -1
else:
    count = 0
    over = []
    result = 0
    for i in range(n):
        if a[i]>b[i]:
            over.append(a[i]-b[i])
        elif a[i]<b[i]:
            count += b[i]-a[i]
            result += 1
        else:
            continue
    over.sort(reverse=True)
    i = 0
    while count > 0:
        result += 1
        count -= over[i]
print(result)

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)
else:
  cnt=0
  plus=0
  minus=0
  prev=0
  for i in range(n):
    if a[i]<b[i]:
      minus+=(b[i]-a[i])
    else:
      plus+=(a[i]-b[i])
      if minus!=0 and plus>=minus:
        cnt+=(i-prev+1)
        plus-=minus
        minus=0
        prev=i
  print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
aff = [0]*N

if sum(A) < sum(B):
    print(-1)
else:
    need = 0
    ans = 0
    for i in range(N):
        aff[i] = A[i] - B[i]
    
    aff = sorted(aff)
    affrev = aff[::-1]
    
    for i in range(N):
        if aff[i] < 0:
            ans += 1
            need -= aff[i]
        else:
            break
    
    j = 0
    while need > 0:
        need -= affrev[j]
        ans += 1
    
    print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
pluslist=[]
minuslist=[]
cnt=0
if sum(A)<sum(B):
    print("-1")
else:
    for i in range(N):
        if A[i]-B[i]<0:
            minuslist.append(A[i]-B[i])
        elif A[i]-B[i]>0:
            pluslist.append(A[i]-B[i])
    #print(minuslist,pluslist)
    ms=sum(minuslist)
    cnt=cnt+len(minuslist)
    pluslist.sort(reverse=True)
    idx=0
    #print(ms,cnt)
    while ms<0:
        cnt=cnt+1
        ms=ms+pluslist[idx]
    print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
taka = list(map(int,input().split()))
exam = list(map(int,input().split()))

sa = list()
for t,e in zip(taka,exam):
  sa.append(t-e)
sa = sorted(sa)

minus = list(filter(lambda x:x<0,sa))
plus = list(filter(lambda x:x>0,sa))

sum_plus = 0
sum_minus = sum(minus)
for idx,p in enumerate(plus[::-1]):
  sum_plus += p
  if sum_plus>=-sum_minus:
    print(idx+1+len(minus))
    break
else:
  if sum_minus == 0:
    print(0)
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
taka = list(map(int,input().split()))
exam = list(map(int,input().split()))

sa = list()
for t,e in zip(taka,exam):
  sa.append(t-e)
sa = sorted(sa)

minus = list(filter(lambda x:x<0,sa))
plus = list(filter(lambda x:x>0,sa))

sum_plus = 0
sum_minus = sum(minus)
for idx,p in enumerate(plus[::-1]):
  sum_plus += p
  if sum_plus>=-sum_minus:
    print(idx+1+len(minus))
    break
else:
  if all([s==0 for s in sa]):
    print(0)
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
taka = list(map(int,input().split()))
exam = list(map(int,input().split()))

sa = list()
for t,e in zip(taka,exam):
  sa.append(t-e)
sa = sorted(sa)

minus = list(filter(lambda x:x<0,sa))
plus = list(filter(lambda x:x>0,sa))

sum_plus = 0
sum_minus = sum(minus)
for idx,p in enumerate(plus[::-1]):
  sum_plus += p
  if sum_plus>=-sum_minus:
    print(idx+1+len(minus))
    break
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [ai - bi for ai, bi in zip(a, b)]
if sum(c) < 0:
    print(-1)
    exit()
c.sort()
minus = deque()
for ci in c:
    if ci >= 0:
        break
    minus.append(ci)
ans = len(minus)
s = sum(minus) * (-1)
plus = 0
idx = -1
while s >= plus:
    plus += c[idx]
    ans += 1
    idx -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  a = input_list()
  b = input_list()
  if sum(a) < sum(b):
    print(-1)
    exit(0)
  
  change_count = 0
  p = 0
  shorts = 0
  amari = []
  for i, aa in enumerate(a):
    bb = b[i]
    if aa > bb:
      amari.append(aa - bb)
    elif bb > aa:
      shorts += bb - aa
      change_count += 1
      
  if shorts == 0:
    print(0)
    exit(0)
  for am in amari:
    shorts -= am
    change_count += 1
    if shorts <= 0:
      break
  if shorts > 0:
    print(-1)
    exit(0)
  print(change_count)
    
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()