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:
def main():
    n = int(input())
    A = input_list()
    B = input_list()
    plusD = []
    minusD = []
    ans = 0
    for i, a in enumerate(A):
        if a - B[i] > 0:
            plusD.append(a - B[i])
        elif a - B[i] == 0:
            continue
        else:
            minusD.append(a - B[i])
            ans += 1
    points = sum(minusD)
    if points >= 0:
        print(0)
        exit()
    count = 0
    for i, d in enumerate(plusD):
        points += d
        ans += 1
        if points >= 0:
            break
    if points >= 0:
        print(ans)
    else:
        print(-1)
    
    
    

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

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 = [0]*N
sumA = 0
sumB = 0
for i in range(N):
    sa = A[i] - B[i]
    diff[i] = sa
    sumA += A[i]
    sumB += B[i]
if sumA < sumB:
    print(-1)
    exit()
diff.sort(reverse= True)
#print(diff)
fixidx = -1
usedI = False
c = 0
usedJ = []
for i in range(N):
    usedI = False
    if diff[i] > 0:
        for j in range(fixidx,-N,-1):
            #print(i,j,diff[i],diff[j])
            if diff[i] == 0:
                break
            if diff[j] < 0:
                if not usedI:
                    usedI = True
                    c += 1
                if diff[i] + diff[j] >= 0:
                    diff[i] += diff[j]
                    diff[j] = 0
                    usedJ.append(j)
                    fixidx = j+1
                else:
                    diff[i] = 0
                    diff[j] += diff[i]
                    usedJ.append(j)
                    fixidx = j
                    break
            elif diff[j] == 0:
                #print(usedJ)
                print(c+len(set(usedJ)))
                exit()
    else:
        break
print(c+len(set(usedJ)))

Submission status: Wrong Answer

Solution code:
#87 C - Exam and Wizard
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
diff = [a-b for a,b in zip(A,B)]
diff = sorted(diff,reverse = True)
negative_v = [i for i in diff if i < 0]
tot = sum(negative_v)
print(diff)
print(tot)

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

Submission status: 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
ab=[]
cd=[]
for i in range(n):
  m=a[i]-b[i]
  if m<0:
    ab.append(m)
  else:
    cd.append(m)
res=sum(ab)
cd.sort(reverse=True)
t=0
while t<len(cd):
  res+=cd[t]
  if res>0:
    print(len(ab)+t+1)
    exit()
  else:
    t+=1

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
d=list(map(int,input().split()))
if(sum(l)<sum(d)):
    print(-1)
else:
    x,y=0,0
    k=[]
    for i in range(n):
        x+=l[i]
        y+=d[i]
        if(d[i]==l[i]):
            k.append(l[i])
        elif(d[i]>l[i]):
            if(x<=y):
                k.append(d[i])
            else:
                k.append(d[i]+(x-y))
                x=0
                y=0
        if(l[i]>d[i]):
            if(x>y):
                k.append(d[i]+(x-y))
                x=0
                y=0
            else:
                k.append(d[i])
    m=0
    for i in range(n):
        if(l[i]!=k[i]):
            m+=1
    if(sum(l)!=sum(k)):
        m+=1
    print(m)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
import bisect

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

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

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

div.sort()
idx = bisect_left(div, 0)
positive = div[idx:]
negative = div[:idx]

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

ans = len(negative)
changed = False

for x in negative:
    while x > positive[-1]:
        x -= positive.pop(-1)
        ans += 1
        changed = False
    positive[-1] -= x
    if not changed:
        ans += 1
        changed = True

print(ans)

Submission status: Wrong Answer

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

C = [x - y for x, y in zip(A, B)]
Minus = [i for i in C if i < 0]
minus = sum(Minus)
EX = [i for i in C if i > 0]

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

i = 0
while minus < 0:
    minus += EX[i]
    i += 1

print(len(Minus) + i)

Submission status: Wrong Answer

Solution code:
# 13
import sys
import numpy as np


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = np.array(LI())
b = np.array(LI())
diff = a - b
pos = [diff[i] for i in range(len(diff)) if diff[i] > 0]
neg = [diff[i] for i in range(len(diff)) if diff[i] < 0]
if sum(pos) + sum(neg) < 0:
    print(-1)
    exit()

if len(neg) == 0:
    print(0)
    exit()

ans = len(neg)
s_neg = sum(neg)
for elem in pos:
    if s_neg < 0:
        ans += 1
        s_neg += elem
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
# 13
import sys
import numpy as np


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = np.array(LI())
b = np.array(LI())
diff = a - b
pos = [diff[i] for i in range(len(diff)) if diff[i] > 0]
neg = [diff[i] for i in range(len(diff)) if diff[i] < 0]
if sum(pos) + sum(neg) < 0:
    print(-1)
    exit()

# if len(neg) == 0:
#     print(0)
#     exit()

ans = len(neg)
s_neg = sum(neg)
for elem in pos:
    if s_neg < 0:
        ans += 1
        s_neg += elem
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
# 13
import sys
import numpy as np


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = np.array(LI())
b = np.array(LI())
diff = a - b
neg = np.sum(np.where(diff < 0, diff, 0))
# print(np.where(diff < 0, diff, 0))
pos = np.sum(np.where(diff >= 0, diff, 0))
# print(np.where(diff >= 0, diff, 0))
# print(diff)
# print(neg)
# print(pos)
if pos + neg < 0:
    print(-1)
    exit()

sort_diff = sorted(diff)
# print(sort_diff)
ans = 0
yojou = 0
for ai, bi in zip(a, b):
    if bi > ai:
        ans += 1
        if yojou-(bi-ai) >= 0:
            yojou -= bi - ai
        else:
            yojou += sort_diff.pop(-1)
            ans += 1
            yojou -= bi - ai
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sp=sn=0
P=[]
a=0
for i in range(n):
  d=A[i]-B[i]
  if d<0:
    sn+=d
    a+=1
  else:
    sp+=d
    P.append(d)
if sp<-sn:
  print(-1)
  exit()
for p in sorted(P[::-1]):
  if sn>=0:
    break
  else:
    sn+=p
    a+=1
print(a)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sp=sn=0
P=[]
a=0
for i in range(n):
  d=A[i]-B[i]
  if d<0:
    sn+=d
    a+=1
  else:
    sp+=d
    P.append(d)
if sp<-sn:
  print(-1)
  exit()
for p in P[::-1]:
  if sn>=0:
    break
  else:
    sn+=p
    a+=1
print(a)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num, ans, val = 0, 0, 0
vals = []

for i in range(n):
    c = b[i]-a[i]
    if c > 0:
        num += c
        ans += 1
    elif c < 0:
        vals.append(-c)
vals.sort()

if len(vals) == 0:
    print(0)
elif sum(a) < sum(b):
    print(-1)
else:
    for i in vals[::-1]:
        ans += 1
        val += i
        if val >= num:
            print(ans)
            break

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num, ans, val = 0, 0, 0
vals = []

for i in range(n):
    c = b[i]-a[i]
    if c > 0:
        num += c
        ans += 1
    elif c != 0:
        vals.append(-c)
vals.sort()

if len(vals) == 0:
    print(0)
else:
    for i in vals[::-1]:
        ans += 1
        val += i
        if val >= num:
            print(ans)
            sys.exit()
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num, ans, val = 0, 0, 0
vals = []

for i in range(n):
    c = b[i]-a[i]
    if c > 0:
        num += c
        ans += 1
    else:
        vals.append(-c)
vals.sort()

if len(vals) == 0:
    print(0)
else:
    for i in vals[::-1]:
        ans += 1
        val += i
        if val >= num:
            print(ans)
            sys.exit()
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num, ans = 0, 0
vals = []

for i in range(n):
    c = b[i]-a[i]
    if c >=0:
        num += c
        ans += 1
    else:
        vals.append(-c)
vals.sort()

if len(vals) == 0:
    print(0)
else:
    val = 0
    for i in vals[::-1]:
        ans += 1
        val += i
        if val >= num:
            print(ans)
            sys.exit()
    print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]

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

        for i in range(N):

            if i == N - 1:
                if S > 0:
                    C[i] += S

            else:
                if C[i] != A[i]:
                    if C[i] > A[i]:
                        pass
                    
                    else: # C[i] < A[i]
                        d = A[i] - C[i]
                        if S >= d:
                            C[i] += d
                            S -= d

        ans = 0
        for a, c in zip(A, C):
            if a != c:
                ans += 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()))
x = 0
y = 0
for i in range(n):
    if a[i] >= b[i]:
        x += a[i] - b[i]
    else:
        y += b[i] - a[i]
if x >= y:
    print(y)
else:
    print(-1)

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

minus_sum = 0
plus_list = []
ans = 0
for i in range(N):
    if A[i] < B[i]:
        minus_sum += (B[i] - A[i])
        ans += 1
    elif A[i] > B[i]:
        plus_list.append(A[i] - B[i])
plus_list.sort(reverse=True)

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

i = 0
while minus_sum > 0:
    minus_sum -= plus_list[i]
    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()))

diff = []
for i in range(n):
    diff.append(a[i]-b[i])
if all(diff[i]>=0 for i in range(len(diff))):
    print(0)
    exit()

nega = [-x for x in diff if x <0]
posi = [x for x in diff if x >=0]
sum_nega = sum(nega)
sum_posi = sum(posi)
if sum_nega > sum_posi:
    print(-1)
    exit()
#print(diff,nega,posi,sum_nega,sum_posi)
cnt =0
i = 0
while sum_nega > 0:
    sum_posi -= nega[i]
    sum_nega  -= nega[i]
    if sum_nega <=0:
        break
    cnt +=1
    i +=1

print(len(nega)+cnt)

Submission status: Wrong Answer

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

diff = []
for i in range(n):
    diff.append(a[i]-b[i])
if all(diff[i]>=0 for i in range(len(diff))):
    print(0)
    exit()

nega = [-x for x in diff if x <0]
posi = [x for x in diff if x >=0]
sum_nega = sum(nega)
sum_posi = sum(posi)
if sum_nega > sum_posi:
    print(-1)
    exit()
#print(diff,nega,posi,sum_nega,sum_posi)
cnt =0
i = 0
while sum_nega > 0:
    sum_posi -= nega[i]
    sum_nega  -= nega[i]
    if sum_nega <=0:
        break
    cnt +=1
    i +=1

print(len(nega)+cnt,cnt)

Submission status: Wrong Answer

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

l = []
h = []

for i in range(N):
    if A[i] < B[i]:
        l.append(B[i]-A[i])
    else:
        h.append(A[i]-B[i])

h.sort()

res = 0
y = 0
for x in l:
    if x > y:
        if h:
            res += 1
            y += h.pop()
        else:
            res = -1
            break
    
    res += 1
    y -= x

print(res)

Submission status: Wrong Answer

Solution code:
def solve():
  N = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  C = []
  for a,b in zip(A,B):
    C.append(a-b)
  if sum(C)<0:
    return -1
  minus = sum([abs(x) for x in C if x<0])
  ans = len([abs(x) for x in C if x<0])
  for i in range(N):
    if C[i]>0:
      if minus==0:
        break
      ans += 1
      if minus>C[i]:
        minus -= C[i]
      else:
        minus = 0
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
import bisect
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sumA = sum(A)
sumB = sum(B)
if sumA<sumB:exit(print(-1))
surplus = [0]*n
for i in range(n):
    surplus[i] = A[i]-B[i]
surplus.sort()

index_zero = bisect.bisect_left(surplus,0)
reqt = sum(surplus[:index_zero])
ans = index_zero
index_ = n-1
#print(surplus, index_zero, reqt)
while reqt < 0:
    reqt += surplus[index_]
    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()))

m=0
c=0
for i in range(N):
    if B[i]>A[i]:
        m+=B[i]-A[i]
        c+=1

for i in range(N):
    if B[i]<A[i] and m>0:
        m-=min(A[i]-B[i],m)
        c+=1
print(c if m==0 else -1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
na=sorted(a)
nb=sorted(b)
t=0
for i in range(n):
  if na[i]<nb[i]:
    t=1
    print(-1)
    break
if t==0:
  c=[0]*n
  for i in range(n):
    c[i]=a[i]-b[i]
  c.sort()
  ans=0
  s=0
  for i in range(n):
    if c[i]<0:
      s=s+abs(c[i])
      ans=ans+1
  for i in range(n):
    if s<=0:
      break
    else:
      s=s-c[n-i-1]
      ans=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()))

#%%
husoku = [a-b for a, b in zip(A, B) if a-b < 0]
amari = [a-b for a, b in zip(A, B) if a-b > 0]

H = abs(sum(husoku))
if len(husoku) == 0:
    ans = 0
elif H > sum(amari):
    ans = -1
else:
    for i, a in enumerate(amari, start=1):
        H -= a
        if H <= 0:
            ans = len(husoku) + i
            break

print(ans)

Submission status: Wrong Answer

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

diff = []
for i in range(n):
    diff.append(a[i]-b[i])
if all(diff[i]>=0 for i in range(len(diff))):
    print(0)
    exit()

nega = [-x for x in diff if x <0]
posi = [x for x in diff if x >=0]
sum_nega = sum(nega)
sum_posi = sum(posi)
if sum_nega > sum_posi:
    print(-1)
    exit()
#print(nega,posi,sum_nega,sum_posi)
cnt =0
posi_tot =0
for i in range(len(posi)):
    posi_tot +=posi[i]
    if posi_tot > sum_nega:
        cnt +=1
        break
    else:
        cnt +=1
print(len(nega)+cnt)

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:
    p = []
    m = []
    for i in range(N):
        if(B[i]-A[i] <= 0):
            p.append(A[i]-B[i])
        else:
            m.append(B[i]-A[i])
    
    ans = len(m)
    idx = 0
    res = sum(m)
    while res > 0:
        res = res - p[idx]
        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()))

if(sum(A) < sum(B)):
    print(-1)
else:
    p = []
    m = []
    for i in range(N):
        if(B[i]-A[i] <= 0):
            p.append(A[i]-B[i])
        else:
            m.append(B[i]-A[i])
    
    p.sort(reverse=True)
    ans = len(m)
    idx = 0
    res = sum(m)
    while res > 0:
        res = res - p[idx]
        ans += 1

    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
la = [int(w) for w in input().split()]
lb = [int(w) for w in input().split()]
ld = [a-b for a, b in zip(la, lb)]
if sum(ld) < 0:
    print(-1)
    exit()

llower = [w for w in ld if w < 0]
lupper = [w for w in ld if w > 0]
lowsum = sum(llower)
lupper.sort(reverse=True)

ans = len(llower)

print(llower)
for d in lupper:
    if lowsum >= 0:
        break
    ans += 1
    lowsum += d


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
d = []
s = 0
for ai, bi in zip(a, b):
    if ai < bi:
        ans += 1
        s += bi - ai
    else:
        d.append(ai - bi)
if ans:
    ds = 0
    for di in d:
        ds+=di
        ans+=1
        if ds >= s:
            break
    if ds < s:
        print(-1)
    else:
        print(ans)
else:
    print(0)

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:
    ans = 0
    d = []
    s = 0
    for ai,bi in zip(a,b):
        if ai < bi:
            ans += 1
            s += bi-ai
        else:
            d.append(ai-bi)
    if ans:
        ds = 0
        for di in d:
            ds+=di
            ans+=1
            if ds >= s:
                break
        print(ans)
    else:
        print(0)

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:
    ans = 0
    d = []
    s = 0
    for ai,bi in zip(a,b):
        if ai-bi < 0:
            ans += 1
            s += bi-ai
        else:
            d.append(ai-bi)
    if ans:
        ds = 0
        for di in d:
            ds+=di
            ans+=1
            if ds >= s:
                break
        print(ans)
    else:
        print(0)

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:
    ans = 0
    d = []
    s = 0
    for ai, bi in zip(a, b):
        if ai - bi < 0:
            ans += 1
            s += bi - ai
        else:
            d.append(ai - bi)
    ds = 0
    for i, di in enumerate(d):
        if ds >= s:
            break
        ds += di
        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()))
if sum(a) < sum(b):
    print(-1)
else:
    ans = 0
    l = [ai - bi for ai, bi in zip(a, b) if ai - bi >= 1]
    m = t = sum(l)
    for i in range(n):
        x = a[i] - b[i]
        if x >= 0:
            continue
        t += x
        ans += 1
    tmp = m - t
    c = 0
    while(tmp):
        if l[c] < tmp:
            tmp -= l[c]
            l[c] = 0
            ans += 1
        else:
            ans += 1
            break
        c += 1
    print(ans)

Submission status: Wrong Answer

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

S_ba = 0
S_ab = []
# flag = True
bad = 0
for i in range(N):
    S_ab.append(A[i] - B[i])
    if A[i] < B[i]:
        S_ba += B[i] - A[i]
        bad += 1
        # flag = False
# if flag:
#     print(0)
#     exit()
# if S_ba > sum(S_ab):
#     print(-1)
#     exit()

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

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

Submission status: Wrong Answer

Solution code:
from itertools import *

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

S_ba = 0
S_ab = []
flag = True
bad = 0
for i in range(N):
    S_ab.append(A[i] - B[i])
    if A[i] < B[i]:
        S_ba += B[i] - A[i]
        bad += 1
        flag = False
if flag:
    print(0)
    exit()
if S_ba > sum(S_ab):
    print(-1)
    exit()

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

Submission status: Wrong Answer

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),ans)

Submission status: Wrong Answer

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
  for i in range(len(c)):
    if sum(c[:i])>=sum(d):
      ans=i
      break
  print(ans+len(d))

Submission status: Wrong Answer

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)
else:
  ans=0
  for i in range(len(c)):
    if sum(c[:i])>=sum(d):
      ans=i
      break
  print(ans+len(d))

Submission status: Wrong Answer

Solution code:
# 並びは関係なくて、「自分より1小さいところ」ならどこでもいい
# なので自分より1小さいところが無かったらそもそも構築できない
# あと0は1つだけだし1は数えない（0は1つだけなので）
n = int(input())
# たしかpythonの辞書は順番も守ってくれた気がするのでソートしとく
a = sorted(list(map(int, input().split())))
d = {}
for av in a:
    if av not in d:
        d[av] = 1
    else:
        d[av] += 1

if a[0] != 0 or d[0] != 1 or len(set(d.keys())) != a[-1] + 1:
    print(0)
    exit()
MOD = 998244353
ans = 1
for i in range(2, a[-1] + 1):
    ans *= pow(d[i - 1], d[i], MOD)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
p=[]
m=[]
def app(num1):
    p.append(num1)
def apm(num2):
    m.append(num2)
if sum(a)<sum(b):
    ans=-1
else:
    for i in range(n):
        if a[i]>b[i]:
            app(a[i]-b[i])
        elif a[i]<b[i]:
            apm(b[i]-a[i])
    num=sum(m)
    ans=len(m)
    num1=0
    for i in range(len(p)):
        if num1>=num:
            break
        num1+=p[i]
        ans+=1
        if num1>=num:
            break
print(ans)

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

surplus = []
lack = []
for i in range(n):
    if a[i] > b[i]:
        surplus.append(a[i] - b[i])
    elif a[i] < b[i]:
        # 同値は含まない
        lack.append(b[i] - a[i])
if len(lack) == 0:
    print(0)
    exit()
surplus = sorted(surplus, reverse=True)
lack = sorted(lack, reverse=True)
right = 0
ans = 1
for left in range(len(surplus)):
    while right < len(lack) and surplus[left] - lack[right] >= 0:
        surplus[left] -= lack[right]
        ans += 1
        right += 1
    # ここまではギリギリ条件を満たしている
    if right < len(lack):
        # 次にleftを使わなきゃいけない
        ans += 1
        # あまったぶんを回す
        lack[right] - surplus[left]
print(ans),

Submission status: Wrong Answer

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:
    c = [a[i]-b[i] for i in range(n) if a[i]-b[i]>=0]
    d = [a[i]-b[i] for i in range(n) if a[i]-b[i]<0]
    d.append(0)
    c.sort(reverse=True)
    from itertools import accumulate
    c = list(accumulate(c))

    from bisect import bisect_left
    index= bisect_left(c,-1*sum(d))
    print(len(d)+index)

Submission status: Wrong Answer

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:
    c = [a[i]-b[i] for i in range(n) if a[i]-b[i]>=0]
    d = [a[i]-b[i] for i in range(n) if a[i]-b[i]<0]
    d.append(0)
    c.sort(reverse=True)
    from itertools import accumulate
    c = list(accumulate(c))

    from bisect import bisect_left
    index= bisect_left(c,-1*sum(d))
    print(len(d)+index)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = [ a[i] - b for i, b in enumerate(list(map(int, input().split())))]
d.sort()
if sum(d) < 0:
    print(-1)
elif d[0] >= 0:
    print(0)
else:
    i, j = 0, n-1
    cnt = 2
    while j >= i:
        if d[j] + d[i] >= 0:
            d[j] -= d[i]
            d[i] = 0
            i += 1
            cnt += 1
        else:
            d[i] += d[j]
            d[j] = 0
            j -= 1
            cnt += 1
        if d[i] >= 0:
            break
    print(cnt)

Submission status: Wrong Answer

Solution code:
import sys

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

sum_a=sum(a)
sum_b=sum(b)
cnt=0
ans=0
d=0
if(sum_a<sum_b):
    print(-1)
else:
    for i in range(n):
        if a[i]<b[i]:
            cnt+=1
            d+=(b[i]-a[i])
    if cnt==0:
        print(cnt)
        sys.exit()
    bb=sorted(b,reverse=True)
    bb_sum=0
    for i in range(n):
        bb_sum+=bb[i]
        if bb_sum>d:
            cnt+=(i+1)
            break
    print(cnt)