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: Runtime Error

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)-1)]
        while sumlist[count]<absum:
            count+=1
        
        print(count+1+minum)

Submission status: Runtime Error

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


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

j=0
i = 0
nowCp = int(Cp[len(Cp)-i-1])


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

	else:
		break
	

if(j == len(Cm)):
	print(j+i+1)
else:
	print(-1)

Submission status: Runtime Error

Solution code:
import numpy as np

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

if sum(A)<sum(B):
    print(-1)
else:
    C = np.sort(np.array(A) - np.array(B))[::-1]
    
    temp = np.sum(C<0)
    s=np.sum(C[C<0])
    while(True):
        if s>=0:
            break
        temp+=1
        s += C[0]
        np.delete(C, 0)
    print(temp)

Submission status: Runtime Error

Solution code:
import numpy as np

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

if sum(A)<sum(B):
    print(-1)
else:
    C = np.sort(np.array(A) - np.array(B))[::-1]
    
    temp = np.sum(C<0)
    s=np.sum(C[C<0])
    for i in range(10000):
        if s>=0:
            break
        temp+=1
        s += C[0]
        np.delete(C, 0)
    print(temp)

Submission status: Runtime Error

Solution code:
for a,b in zip(A,B):
    if b>a:
        need+=b-a
        Ans+=1

Gap=[a-b for a,b in zip(A,B)]
Gap.sort(reverse=True)
index=0
while need>0 and Ans+index<=N:
    need-=Gap[index]
    index+=1
    
if Ans+index<=N:
    print Ans+index
else:
    print "-1"

Submission status: Runtime Error

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

mi = []
pl = []
sum = 0
if (np.sum(B) > np.sum(A)):
    print('-1')
    exit()

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

            
pl.sort(reverse=True)
p = np.sum(mi)
if (p != 0) and (len(pl) != 0):
    count = 1
    for i in pl:
        if p-i > 0:
            p -= i
            count += 1
        else:
            break
        
    else:
        print('-1')
        exit()
elif(p == 0) and (len(pl) != 0):
    print('0')
else:
    count = 0

print(len(mi)+count)

Submission status: Runtime Error

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

mi = []
pl = []
sum = 0
if (sum(B) > sum(A)):
    print('-1')
    exit()

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

            
pl.sort(reverse=True)
p = sum(mi)
if (p != 0) and (len(pl) != 0):
    count = 1
    for i in pl:
        if p-i > 0:
            p -= i
            count += 1
        else:
            break
        
    else:
        print('-1')
        exit()
elif(p == 0) and (len(pl) != 0):
    print('0')
else:
    count = 0

print(len(mi)+count)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
rich=[]
poor=[]
ans=0
if sum(a)<abs(sum(b)):
    ans=-1
else:
    for i in range(n):
        t=a[i]-b[i]
        if t>0:
            rich.append(t)
        elif t<0:
            poor.append(t)
    
    rich.sort(reverse=True)
    poor.sort()
    ans=len(poor)
    if len(poor)==0:
        crich=0
    else:
        crich=1
        cpoor=0
        while cpoor<len(poor):
            if rich[crich-1]>abs(poor[cpoor]):
                rich[crich-1]+=poor[cpoor]
                cpoor+=1
            elif rich[crich-1]==abs(poor[cpoor]):
                crich+=1
                cpoor+=1
            else:
                poor[cpoor]+=rich[crich-1]
                crich+=1
print(ans+crich)

Submission status: Runtime Error

Solution code:
import itertools
import copy

cou = 0
h = 0

c = []

N = int(input())
A = [int(i) for i in input().split()]

B = [int(i) for i in input().split()]

a = list(itertools.permutations(A))

for i in range(len(a)):
    j = list(copy.copy(a[i]))
    for k in range(N):
        if(B[k] > j[k]):
            h = 0
            break
        else:
            h = 1
    if(h == 1):
        h = 0
        for k in range(N):
            if(A[k] == j[k]):
                cou += 1
        c.append(cou)
        cou = 0

if not a:
    print(-1)

else:
    print(min(c))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
d = 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:
      	c4 = sorted(c1, reverse=True)
        for i in range(n):
            d = d + c4[i]
            if d + c2 > 0:
                res = c3 + i
                break

print(res)

Submission status: Runtime Error

Solution code:
from sys import stdin
import numpy as np

n = int(stdin.readline().rstrip())
a = [int(i) for i in stdin.readline().rstrip().split(" ")]
b = [int(i) for i in stdin.readline().rstrip().split(" ")]
diff = [ai-bi for (ai,bi) in zip(a,b)]
ab = np.array([a,b,a,diff])
sorted = ab[:, np.argsort(ab[1])[::-1]]
for i in range(n):
    if sorted[3][i] < 0:
        currentmin = 1000000001
        currentminindex = -1
        currentminb = 1000000001
        for k in range(n-i):
            j = k + i
            if (sorted[2][j] >= sorted[1][i]):
                # before change if (sorted[2][j] >= sorted[1][i]) & (sorted[2][i] >= sorted[1][j])
                # if sorted[2][j] < currentmin:
                #     currentmin = sorted[2][j]
                #     currentminindex = j
                #     currentminb = sorted[1][j]
                # elif (sorted[2][j] == currentmin) & (sorted[1][j] < currentminb):
                #     currentmin = sorted[2][j]
                #     currentminindex = j
                #     currentminb = sorted[1][j]
                if sorted[1][j] < currentminb:
                    currentmin = sorted[2][j]
                    currentminindex = j
                    currentminb = sorted[1][j]
        if currentmin < 1000000001:
            iele = sorted[2][i]
            jele = sorted[2][currentminindex]
            sorted[2][i] = jele
            sorted[2][currentminindex] = iele
            sorted[3] = sorted[2] - sorted[1]
status = 0
for i in range(n):
    transfered = np.where(sorted[2] == sorted[0][i])
    if (sorted[1][i] <= sorted[2][transfered]) & (sorted[1][transfered] <= sorted[2][i]):
        iele = sorted[2][i]
        jele = sorted[2][transfered]
        sorted[2][i] = jele
        sorted[2][transfered] = iele
        sorted[3] = sorted[2] - sorted[1]

for i in range(n):
    if sorted[0][i] != sorted[2][i]:
        status +=1
for i in range(n):
    if sorted[3][i] < 0:
        status = -1

print(status)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
rich=[]
poor=[]
ans=0
if sum(a)<abs(sum(b)):
    ans=-1
else:
    for i in range(n):
        t=a[i]-b[i]
        if t>0:
            rich.append(t)
        elif t<0:
            poor.append(t)
    
    rich.sort(reverse=True)
    poor.sort()
    ans=len(poor)
    if len(poor)==0:
        crich=0
    else:
        crich=1
    cpoor=0
    while cpoor<len(poor):
        if rich[crich-1]>abs(poor[cpoor]):
            rich[crich-1]+=poor[cpoor]
            cpoor+=1
        elif rich[crich-1]==abs(poor[cpoor]):
            rich[crich-1]=0
            crich+=1
            cpoor+=1
        else:
            poor[cpoor]+=rich[crich-1]
            crich+=1
print(ans+crich)

Submission status: Runtime Error

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.sum(A) =< np.sum(B):
  print(-1)
else:
  print( np.sum(A < B))

Submission status: Runtime Error

Solution code:
B = np.array(B)
if np.sum(A) < np.sum(B):
    print(-1) 
else:
    sub_a_b = A-B
    p_sub = sorted(list(sub_a_b[sub_a_b>0]),reverse = True)
    # print(p_sub)  
    m_sub = list(sub_a_b[sub_a_b<0])
    max_counter = p_sub[0]
    m_sub_sum = abs(sum(m_sub))
    count = len(m_sub) 
    # print(p_sub[0:1])
    if max_counter >= m_sub_sum:
        print(count)
    else:
        count+=1

        for i in range(len(p_sub)):
            if max_counter >= m_sub_sum:
                print(count)
                exit()
            else:
                max_counter += p_sub[i+1]
                count += 1

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
rich=[]
poor=[]
ans=0
if sum(a)<sum(b):
    ans=-1
else:
    for i in range(n):
        t=a[i]-b[i]
        if t>0:
            rich.append(t)
        elif t<0:
            poor.append(t)
    
    rich.sort(reverse=True)
    poor.sort()
    ans=len(poor)
    if len(poor)==0:
        crich=0
    else:
        crich=1
    cpoor=0
    while cpoor<len(poor):
        if rich[crich-1]>abs(poor[cpoor]):
            rich[crich-1]+=poor[cpoor]
            cpoor+=1
        elif rich[crich-1]==abs(poor[cpoor]):
            rich[crich-1]=0
            crich+=1
            cpoor+=1
        else:
            poor[cpoor]+=rich[crich-1]
            crich+=1
print(ans+crich)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
rich=[]
poor=[]
ans=0
if sum(a)<sum(b):
    ans=-1
else:
    for i in range(n):
        t=a[i]-b[i]
        if t>0:
            rich.append(t)
        elif t<0:
            poor.append(t)
    
    rich.sort(reverse=True)
    poor.sort()
    ans=len(poor)
    if len(poor)==0:
        crich=0
    else:
        crich=1
    cpoor=0
    print(rich)
    print(poor)
    while cpoor<len(poor):
        if rich[crich-1]>abs(poor[cpoor]):
            rich[crich-1]+=poor[cpoor]
            cpoor+=1
        elif rich[crich-1]==abs(poor[cpoor]):
            rich[crich-1]=0
            crich+=1
            cpoor+=1
        else:
            poor[cpoor]+=rich[crich-1]
            crich+=1
print(ans+crich)

Submission status: Runtime Error

Solution code:
N = int(input())
a = input().split()
A = []
for i in range(N):
    A.append(int(a[i]))
b = input().split()
B = []
for i in range(N):
    B.append(int(b[i]))
if sum(A) < sum(B):
    print(-1)
else:
    num = 0
    fix = 0
    for i in range(N):
        if A[i] - B[i] <  0:
            num += 1
            fix += B[i] - A[i]
if num == 0:
    print(0)
else:
    ret = []
    for i in range(N):
        if A[i] - B[i] > 0:
            ret.append(A[i] - B[i])
        else:
            ret.append(0)
    ret.sort(reverse=True)
    su = 0
    for i in range(N):
        su += ret[i]
        num += 1
        if su >= fix:
            break
    print(num)

Submission status: Runtime Error

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

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


if __name__ == '__main__':
  main()

Submission status: Runtime Error

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

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

ng = 0
dif = 0
plus = []

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

plus.sort(reverse=True)

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

print(ng+n)

Submission status: Runtime Error

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

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

neg = [v for v in c if c < 0]
total = abs(sum(neg))
num = len(neg)
for i, v in enumerate(reversed(pos(c[num:]))):
    total += v
    if total >= 0:
        break

print(num + i)

Submission status: Runtime Error

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

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

c.sort(reverse=True)
d=np.array(c)
z=np.sum(d < 0)

x=np.sum(d[ d < 0])
y=np.sum(d[ d > 0])

if x == 0:
    print("0")
    exit()

if x + y < 0:
    print("-1")
    exit()    

for i,num in enumerate(c)
    x += num
    if x >= 0:
        print(z+i+1)

Submission status: Runtime Error

Solution code:
# coding: utf-8

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

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

    diff_sum = 0
    diff_cnt = 0
    diff_list = [0] * N
    for i in range(N):
        diff_list[i] = A[i] - B[i]
        if A[i] < B[i]:
            diff_sum += B[i] - A[i]
            diff_cnt += 1

    diff_list_sorted = sorted(diff_list, reverse=True)

    use_sum = 0
    for i in range(N):
        if use_sum >= diff_sum:
            break
        use_sum += diff_list_sorted[i]
        diff_cnt += 1

    return diff_cnt


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

Submission status: Runtime Error

Solution code:
n = int(input())
now = input().split()
after = input().split()
for i in range(3):
    now[i] = int(now[i])
    after[i] = int(after[i])
lack_list = []
amari_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])
    s1 = sum(lack_list)
    
    for i in range(n):#2-1　余っているやつと必要分の差分を抜き出す、大きい順に並び替え
        if now[i]>after[i]:
            amari2_list.append(now[i]-after[i])
    amari2_list.reverse()
    count = 0
    t = 0
    while s1 > t:    #2-2　差分何個が必要か計算
        t += amari2_list[count] 
        count += 1 
    ans = len(lack_list)  + count
    print(ans)

Submission status: Runtime Error

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

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

if sum(c) < 0:
    print(-1)

neg = [v for v in c if c < 0]
pos = [v for v in c if c > 0]
total = abs(sum(neg))

k = 0
for i, v in enumerate(reversed(pos)):
    k += v
    if k >= total:
        break

print(len(neg) + i + 1)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
  int n;
  ll a[100000], b[100000];
  ll diff[100000];
  ll cnt = 0;
  ll invsum = 0;
  cin >> n;
  for(int i=0;i<n;i++)cin >> a[i];
  for(int i=0;i<n;i++)cin >> b[i];
  for(int i=0;i<n;i++){
    diff[i] = a[i] - b[i];
    if(diff[i] < 0){
      cnt++;
      invsum += diff[i];
    }
  }
  sort(diff, diff+n, greater<ll>());
  for(int i=0;i<n;i++){
    if(invsum>=0 || diff[i] <= 0) break;
    invsum += diff[i];
    cnt++;
  }
  if(invsum < 0) cout << -1 << endl;
  else cout << cnt << endl;
}