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: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

cl = [0]*n
solved = True
for i in range(n):
    cl[i] = al[i]-bl[i]
    if cl[i]<0:
        solved = False

if solved:
    print(0)
    exit()

cl.sort(reverse=True)
# print(cl)

last = n-1
i = 0
res = 0
used = [False for _ in range(n)]
while True:
    if cl[last]>0:
        break

    while cl[i]>0 and cl[last]<0:
        if cl[i]+cl[last]>=0:
            cl[i] += cl[last]
            cl[last] = 0
            if not used[last]:
                res += 1
            used[last] = True
            last -= 1
        else:
            cl[last] += cl[i]
            cl[i] = 0
        if not used[i]:
            res += 1
        used[i] = True
    i += 1
    if i==last:
        break

if cl[last]<0:
    res = -1
print(res)
# print(cl)

Submission status: Accepted

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

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

    diffs = []
    rest = 0
    ans = 0
    for i in range(n):
        if a[i] < b[i]:
            rest += b[i] - a[i]
            ans += 1
        if a[i] > b[i]:
            diffs.append(a[i] - b[i])
            
    diffs.sort()
    while rest > 0:
        diff = diffs.pop()
        rest -= diff
        ans += 1

    return ans


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

Submission status: Accepted

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

isho = 0
tarinai = []
amari = []

for i in range(N):
    if A[i] == B[i]:
        isho += 1
    elif A[i] - B[i] > 0 :
        amari.append(A[i]-B[i])
    else:
        tarinai.append(B[i] - A[i])

if len(tarinai) == 0:
    print(0)
    exit()
elif len(amari) == 0:
    print(-1)
    exit()

amari = sorted(amari,reverse=True)

ind = 0
tmp = 0

sum_tari = sum(tarinai)

while sum_tari > tmp:
    tmp += amari[ind]
    ind += 1

ans = ind + len(tarinai)
print(ans)

Submission status: Accepted

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

diff = [A[i] - B[i] for i in range(N)]
diff = sorted(diff)
j = 0
k = N - 1
num = 0
tmp = 0
while diff[j] < 0:
    tmp += diff[j]
    j += 1
    num += 1
    while tmp < 0:
        tmp += diff[k]
        k -= 1
        num += 1

print(num)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
import re
sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B):
    if sum(A) < sum(B):
        return -1

    P = []
    M = []
    for i in range(N):
        d = A[i] - B[i]
        if d > 0:
            P.append(d)
        elif d < 0:
            M.append(d)

    P.sort()
    M.sort()

    c = 0
    ans = 0
    for m in M:
        while -m > c:
            c += P.pop()
            ans += 1
        c += m
        ans += 1

    return ans


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    print(slv(N, A, B))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = []
d = 0
dc = 0
for I in range(n):
 x = a[I] - b[I]
 if x >= 0:
  c.append(x)
 else:
  d -= x
  dc += 1  
c.sort(reverse = True)
m = len(c)
k = 0
while d >0 and k < m :
 d -= c[k]
 k += 1
if d >0:
 print(-1)
else:
 print(dc + k)

Submission status: Accepted

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
a=i2()
b=i2()
c=[]
e=0
for i in range(n):
 c.append([a[i]-b[i],i])
c.sort()
d=[c[n-1][0],n-1]
if c[0][0]<0:
   e+=1
for i in range(n):
 if c[i][0]<0:
    d[0]+=c[i][0]
    e+=1
    if d[0]<0:
       d[1]-=1
       d[0]+=c[d[1]][0]
       e+=1
 else:
    break
if (sum(a)-sum(b))<0:
   print(-1)
else:
   print(e)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

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

diff = [A[i] - B[i] for i in range(N)]
diff.sort()
r = sum(A) - sum(B)
ans = N
for i in range(N):
    if diff[i] < 0:
        continue
    if r < 1:
        break
    if diff[i] <= r:
        ans -= 1
        r -= diff[i]

print(ans)

Submission status: Accepted

Solution code:
def c_exam_and_wizard(N, A, B):
    ans = 0
    short_val = 0  # 合格に不足する準備度
    can_give = []
    for a, b in zip(A, B):
        if a < b:
            ans += 1
            short_val += b - a
        elif a > b:
            can_give.append(a - b)

    if short_val == 0:
        return 0
    can_give.sort()

    idx = len(can_give) - 1
    while True:
        if not can_give:
            ans = -1
            break

        if can_give[idx] >= short_val:
            ans += 1
            break

        short_val -= can_give[idx]
        can_give.pop()
        ans += 1
        idx -= 1
    return ans

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print(c_exam_and_wizard(N, A, B))

Submission status: Accepted

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

T = [x-y for x,y in zip(A,B)]
M = [t for t in T if t < 0]
P = [t for t in T if t >= 0]

ans = 0
ans += len(M)
b = sum(M)
P.sort()
P=P[::-1]
for p in P:
	if b >= 0:
		break
	b+=p
	ans += 1
#print(T)
#print(M)
#print(P)
print(ans)

Submission status: Accepted

Solution code:

import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    input_rawA = read_func().strip().split()
    input_rawB = read_func().strip().split()
    A = []
    B = []
    sub_AB = []
    sumA = 0
    sumB = 0
    count = 0
    sum_fusoku = 0
    for i in range(N):
        A.append(long(input_rawA[i]))
        B.append(long(input_rawB[i]))
        sub_AB.append((max(A[i] - B[i],0), i))
        sumA += A[i]
        sumB += B[i]
        if A[i] < B[i]:
            count += 1
            sum_fusoku += B[i] - A[i]


    if sumA < sumB:
        print -1
        return

    sub_AB.sort(reverse = True)

    for i in range(len(sub_AB)):
        if sum_fusoku > 0:
            sum_fusoku -= sub_AB[i][0]
            count += 1
        else:
            break
    print count



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i,j in zip(a,b):
  c.append(i-j)
c.sort()
if min(c) >= 0:
  print(0)
  exit()
else:
  zero = bisect.bisect_left(c,0)
  minus = sum(c[:zero])
  #print(minus)
  d = c[zero:]
  ans = zero
  for i in d[::-1]:
    minus += i
    ans += 1
    if minus >= 0:
      print(ans)
      exit()
print(-1)

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

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

T = [0]*N
for i in range(N):
    T[i] = A[i] - B[i]

T.sort()
r = N-1
l = 0

flag = True
cnt = 0

if T[l] >= 0:
    print(0)
else:
    while T[l] < 0:
        if l >= r:
            flag = False
            break
        elif T[r] >= -T[l]:
            T[r] += T[l]
            l += 1
        else:
            T[l] += T[r]
            r -= 1
                


    ans = l + (N - r)
    if flag:
        print(ans)
    else:
        print(-1)

Submission status: Accepted

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

sumA = sum(A)
sumB = sum(B)

if sumA < sum(B):
    print('-1')
    exit()
ans = 0
totalMinus = 0
for a, b in zip(A, B):
    if a-b > 0:
        plusSubject.append(a-b)
    elif a-b < 0:
        totalMinus += a-b
        ans += 1

plusSubject = sorted(plusSubject, reverse=True)

for plus in plusSubject:
    if totalMinus >= 0:
        break
    totalMinus += plus
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import numpy as np


def solve():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    B = np.array(list(map(int, input().split())))

    # 自明
    if sum(A) < sum(B):
        print(-1)
        return

    # できるとき
    few = A < B
    few_cnt = few.sum()
    few_sum = (B - A)[few].sum()
    if few_cnt == 0:
        print(0)
        return

    over = (A >= B)
    overs = (A - B)[over]
    overs.sort()
    overs_cumsum = overs[::-1].cumsum()
    for i, v in enumerate(overs_cumsum):
        if v >= few_sum:
            print(few_cnt + i + 1)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

    # print(d)

    if sum(d) < 0:
        return -1

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

    ans = len(neg)

    i = 0
    n = sum(neg)
    while n< 0:
        n+= pos[i]
        i+=1
    ans += i
    return ans


print(solve())

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
D = []
luck = []
raku = []
for i in range(N):
    d = A[i]-B[i]
    if d < 0:
        luck.append(d)
    else:
        raku.append(d)
    D.append(d)
Luck = sum(luck)
raku.sort(reverse=True)
R = len(raku)
L = len(luck)
flag = 0
for j in range(R):
    if Luck >= 0:
        print(0)
        flag = 1
        break
    Luck+=raku[j]
    if Luck >= 0:
        print(j+1+L)
        flag = 1
        break
    else:
        pass
if flag == 0:
    print(-1)

Submission status: Accepted

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

if sum(A) > sum(B):
    counter = 0
    array = []
    num = 0
    for i in range(N):
        if A[i] >= B[i]:
            array.append(A[i] - B[i])

        else:
            counter += 1
            num += (B[i] - A[i])

    array.sort()
    while num > 0:
        counter += 1
        num -= array[-1]
        del array[-1]

    print(counter)
    
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
if sum(a) <= sum(b):
    ans = -1
else:
    ans = 0
    
    d = [a[i]-b[i] for i in range(n)]
    plus = []
    minus = []
    for i in range(n):
        if d[i] > 0:
            plus.append(d[i])
        elif d[i] < 0:
            minus.append(d[i])

    sum_minus = -sum(minus)
    ans = len(minus)
    plus.sort(reverse=True)
    while sum_minus > 0:
        num_plus = plus.pop(0)
        sum_minus -= num_plus
        ans += 1
print(ans)

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

d_pls = []
d_mns = []
s = 0
for i in range(N):
    t = a[i] - b[i]
    s += t
    if t > 0:
        d_pls.append(t)
    elif t < 0:
        d_mns.append(t)
# 準備不足なら負

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

d_pls = sorted(d_pls, reverse=True)

d_mns = sorted(d_mns)
m_sum = sum(d_mns)
ans = len(d_mns)

i = 0
while m_sum < 0:
    m_sum += d_pls[i]
    ans += 1
    i += 1
print(ans)

Submission status: Accepted

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])
C.sort()
C.reverse()
ans = 0
count = 0
for i in range(N):
    if A[i] < B[i]:
        ans += 1
        count += B[i] - A[i]
for i in range(N):
    if count > 0:
        count -= C[i]
        ans += 1
    else:
        break
        
if sum(A) < sum(B):
    ans = -1
print(ans)

Submission status: Accepted

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

A_origin = A[:]

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

diff = [[i, a - b] for i, a, b in zip(range(N), A, B)]

order = sorted([[i, x] for i, x in diff if x > 0], key=lambda d: d[1])
C = []

charge = order.pop()
for i in range(N):
    while A[i] - B[i] < 0:
        if charge[1] > B[i] - A[i]:
            A[charge[0]] -= (B[i] - A[i])
            charge[1] -= (B[i] - A[i])
            A[i] = B[i]
        else:
            A[charge[0]] = B[charge[0]]
            A[i] += charge[1]
            charge = order.pop()

ans = 0
for a, ao in zip(A, A_origin):
    if a != ao:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
D = [A[i]-B[i] for i in range(N)]
D.sort()
s = 0
for i in range(N):
    if D[i] >= 0:
        break
    s -= D[i]
if s == 0:
    print(0)
else:
    D = [x for x in D if x>0]
    D.sort()
    D.reverse()
    pos = False
    for j in range(len(D)):
        if s-D[j] <= 0:
            pos = True
            break
        s -= D[j]
    if pos:
        print(i+j+1)
    else:
        print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate
from bisect import bisect_left

n = ni()
a = list(li())
b = list(li())

if sum(a) < sum(b):
    print(-1)
    
else:
    over = []
    lack = 0
    ans = 0
    for ai, bi in zip(a,b):
        if ai < bi:
            ans += 1
            lack += (bi-ai)
        else:
            over.append(ai-bi)
            
    over.sort(reverse=True)
    over = [0] + over
    over = list(accumulate(over))
    cnt = bisect_left(over, lack)
    
    ans += cnt
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

C = [0 for i in range(N)]

ans = 0
al_C = 0

for i in range(N):
    C[i] = A[i]-B[i]
    if C[i]<0:
        ans = ans+1
        al_C = al_C-C[i]
    
C.sort()
C.reverse()

if ans==0:
    print(0)
else:
    f = 1
    
    for i in range(N):
        if C[i]<=0:
            print(-1)
            break
        else:
            al_C = al_C-C[i]
            ans = ans +1
        
        if al_C<=0:
            print(ans)
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
import sys

N = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
dist = [B[i] - A[i] for i in range(N)]
distp = [[i, B[i] - A[i]] for i in range(N)]
if sum(dist) > 0:
    print(-1)
    sys.exit()
distp.sort(key=lambda x: x[1], reverse=True)
C = [v for v in A]
ans = 0
for i, dist in distp:
    if dist <= 0:
        break
    while dist > 0:
        v = min(dist, -distp[-1][1])
        dist -= v
        distp[-1][1] += v
        if distp[-1][1] == 0:
            distp.pop()
            ans += 1
    ans += 1
if len(distp):
    i, v = distp[-1]
    if B[i] - A[i] != v:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
over = []
under = 0
uc = 0

if sum(A) < sum(B):
  print(-1)
else:
  for a, b in zip(A, B):
    if a > b:
      over.append(a-b)
    elif a < b:
      uc += 1
      under += b-a
  for o in sorted(over, key=lambda x: -x):
    if under <= 0:
      break
    else:
      under -= o
      uc += 1
  print(uc)

Submission status: Accepted

Solution code:
import sys

N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

if sum(As) < sum(Bs):
    print(-1)
    sys.exit()

ans = 0
sumShortage = 0
Surpluss = []
for A, B in zip(As, Bs):
    if A < B:
        ans += 1
        sumShortage += B - A
    else:
        Surpluss.append(A - B)

Surpluss.sort(reverse=True)

for Surplus in Surpluss:
    if sumShortage <= 0:
        break
    sumShortage -= Surplus
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = 0
ans = True
if sum(A) < sum(B):
    res = -1
    ans = False
    print(res)
if ans:
    N = [A[i]-B[i] for i in range(n) if A[i] < B[i]]
    P = [A[i]-B[i] for i in range(n) if A[i] >= B[i]]
    res = len(N)
    P.sort(reverse=True)
    dif = abs(sum(N))
    i = 0
    while dif>0:
        dif -= P[i] 
        res += 1
        i += 1
    print(res)

Submission status: Accepted

Solution code:
import sys
def inpl(): return [int(i) for i in input().split()]
N = int(input())
A = inpl()
B = inpl()
if sum(A) < sum(B):
    print(-1)
    sys.exit()
sB = [j for i, j in zip(A, B) if i < j]
sA = [i for i, j in zip(A, B) if i < j]
ans = len(sA) 
k = sum(sB) - sum(sA)
C = sorted([i-j for i, j in zip(A,B) if i >= j])
while k > 0:
    c = C.pop()
    k -= c
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys

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

enough=[]
lack=[]
for i in range(n):
    if a[i]-b[i]<0:
        lack.append(a[i]-b[i])
    else:
        enough.append(a[i]-b[i])

change=(-1)*sum(lack)
enough.sort()
enough=enough[::-1]

i=0
while change>0:
    if i>=len(enough):
        print(-1)
        sys.exit()
    change-=enough[i]
    i+=1

print(i+len(lack))

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(i) for i in input().split())
b = list(int(i) for i in input().split())
d = []
for i in range(n):
    d.append(a[i]-b[i])
d = sorted(d)
if sum(d)<0:
    print(-1)
else:
    dif = 0
    count = 0
    for i in range(n):
        if d[i]<0:
            dif -= d[i]
            count += 1
        else:
            break
    if dif>0:
        for i in range(n):
            dif -= d[n-1-i]
            count += 1
            if dif <=0:
                break
    print(count)

Submission status: Accepted

Solution code:
from operator import itemgetter

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a) < sum(b):
    print(-1)
else:
    ab = []
    husoku = 0
    ans = 0
    for i in range(n):
        sa = a[i] - b[i]
        ab.append((a[i], b[i], sa))
        if sa < 0:
            husoku += sa
            ans += 1
    ab = sorted(ab, key=itemgetter(2), reverse=True)
    count = 0
    i = 0
    while i < n and count < -husoku:
        count += ab[i][2]
        i += 1
    print(i + ans)

Submission status: Accepted

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

amari = sorted([A[i]-B[i] for i in range(N) if A[i]>B[i]])
fusoku = sum([B[i]-A[i] for i in range(N) if A[i] < B[i] ])
fusoku_count = len([B[i]-A[i] for i in range(N) if A[i] < B[i]] )
ans = fusoku_count
while fusoku > 0 :
    if not amari:
        print(-1)
        exit()
    fusoku -= amari.pop()
    ans +=1
print(ans)

Submission status: Accepted

Solution code:
import sys
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)
    sys.exit()
H=[]
num=0
ans=0
for i in range(N):
    if A[i]>=B[i]:
        c=A[i]-B[i]
        H.append(c)
    else:
        num+=B[i]-A[i]
        ans+=1
H=sorted(H,reverse=True)
if len(H)==N:
    print(0)
    sys.exit()
for i in range(len(H)):
    num-=H[i]
    ans+=1
    if num<=0:
        break
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N = int( input())
A = list( map( int, input().split()))
B = list( map( int, input().split()))
ans = 0
kata = []
need = []
for i in range(N):
    if A[i] < B[i]:
        need.append(B[i] - A[i])
        ans += 1
    elif B[i] < A[i]:
        kata.append(A[i] - B[i])
need.sort()
kata.sort(key=None, reverse = True)
now = 0
for n in need:
    while n > now:
        if kata:
            now += kata.pop(0)
            ans += 1
        else:
            ans = -1
            break
    now -= n
    if ans == -1:
        break
print( ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
if sum(A) < sum(B):
  print(-1)
else:
  C = [A[i] - B[i] for i in range(N)]
  D = sorted([c for c in C if c > 0])
  E = [c for c in C if c < 0]
  cnt = 0
  F = sum(E)
  while F < 0:
    F += D.pop()
    cnt += 1
  print(cnt + len(E))

Submission status: Accepted

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

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

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

    residual = sorted(np.array(A)[enough] - np.array(B)[enough])

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

Submission status: Accepted

Solution code:
while(True):
    try:
        n = int(input())
        a = list(map(int, input().split()))
        b = list(map(int, input().split()))
        extra = []
        loss = 0
        ans = 0
        for i in range(n):
            dif = a[i] - b[i]
            extra.append(dif)
            loss += min(0, dif)
            if dif < 0:
                ans += 1
        extra.sort(reverse=True)
        for num in extra:
            if loss < 0:
                loss += num
                ans += 1
            else:
                print(ans)
                exit()
        print(-1)
    except EOFError:
        exit()

Submission status: Accepted

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

if a < b:
	print(-1)

else:
	X = []
	for i in range(N):
		X.append(A[i] - B[i])
	X = sorted(X)
	
	x = 0
	c = 0
	for i in range(N):
		if X[i] < 0:
			x += X[i]
			c += 1
		else:
			break

	for i in range(N):
		if x >= 0:
			break
		else:
			c += 1
			x += X[-1 - i]

	print(c)

Submission status: Accepted

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:
    D = [A[i] - B[i] for i in range(N)]
    F = [True if D[i] >= 0 else False for i in range(N)]
    if sum(F) == len(F):
        print(0)
    else:
        Dp = [D[i] for i in range(N) if F[i] == True]
        Dm = [D[i] for i in range(N) if F[i] == False]
        Sm = sum(Dm)
        R  = len(Dm)
        Dp.sort()
        while Sm < 0:
            R  += 1
            Sm += Dp[-1]
            del Dp[-1]
        print(R)

Submission status: Accepted

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

from collections import deque
import heapq
import bisect
import math


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

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

    need = cnt = 0
    ex = []
    for i in range(n):
        if a[i] < b[i]:
            need += b[i] - a[i]
            cnt += 1
        elif a[i] > b[i]:
            ex.append(a[i]-b[i])

    ex.sort(reverse=True)

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

    cur = 0
    for i in range(len(ex)):
        cur += ex[i]
        if cur >= need:
            print(cnt+i+1)
            exit()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = []
for i in range(N):
	D.append(A[i] - B[i])
D.sort()
ans = 0
shortage = 0
for i in D:
	if i >= 0:
		break
	ans += 1
	shortage += i
if shortage < 0:
	D.reverse()
	for i in D:
		if i < 0:
			ans = -1
			break
		shortage += i
		ans += 1
		if shortage >= 0:
			break
print(ans)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
Cp = {}
ans = 0
Cm_sum = 0
for i in range(N):
    c = A[i] - B[i]
    if c > 0:
        Cp[c] = Cp.get(c, 0) + 1
    elif c < 0:
        ans += 1
        Cm_sum -= c
if Cm_sum == 0:
    print(0)
else:
    Cp_sum = 0
    for k in sorted(Cp, reverse=True):
        count = min(1 + (Cm_sum - Cp_sum - 1) // k, Cp[k])
        ans += count
        Cp_sum += k * count
        if Cp_sum >= Cm_sum:
            print(ans)
            exit()
    print(-1)

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a)<sum(b):
    print(-1)
    sys.exit()
    
need = 0
amari = []
ans = 0
for ai,bi in zip(a,b):
    dif = ai-bi
    if dif<0:
        need += abs(dif)
        ans += 1
    else:
        amari.append(dif)
amari.sort(reverse=True)
amari

s = 0
for amari_i in amari:
    if s<need:
        s += amari_i
        ans += 1
    else: break
print(ans)

Submission status: Accepted

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:
    dif = [a - b for a, b in zip(A, B)]
    negative = [d for d in dif if d < 0]
    neg_sum = - sum(negative)
    ans = len(negative)
    positive = sorted([d for d in dif if d >= 0], reverse=True)
    i = 0
    while neg_sum > 0:
        neg_sum -= positive[i]
        i += 1
        ans += 1
    print(ans)

Submission status: Accepted

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

sa = [A[i]-B[i] for i in range(N)]
sa = sorted(sa)
ans=0
minus = 0
for i in sa:
  if i < 0:
    minus += i
    ans+=1
  if i >= 0:
    break

for i in sa[::-1]:
  if minus >= 0:
    break
  if i > 0:
    minus += i
    ans += 1
  if i <= 0:
    break
    
if minus < 0:
  print(-1)
else:
  print(ans)