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:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

# どのようにしても合格できない場合
if sum(a) < sum(b):
    print(-1)
    exit()

sum_minus = 0
ans = 0
a_minus_b = []
for i in range(n):
    if a[i] < b[i]:
        sum_minus += a[i] - b[i]
        ans += 1
    a_minus_b.append(a[i] - b[i])

if sum_minus >= 0:
    print(ans)
    exit()

for i in sorted(a_minus_b, reverse=True):
    sum_minus += i
    ans += 1
    if sum_minus >= 0:
        print(ans)
        exit()

Submission status: Accepted

Solution code:
import numpy as np
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)

else:
    a_n = np.array(a)
    b_n = np.array(b)

    d = a_n - b_n
    d_min = d[d < 0]
    d_max = np.sort(d[d > 0])[::-1]
    num = len(d_min)
    if num == 0:
        print(0)
        exit()
    for i in range(len(d_max)+1):

        if np.sum(d_min) + np.sum(d_max[:i]) > 0:
            print(num + i)
            exit()

Submission status: Accepted

Solution code:
from itertools import accumulate


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)]
  pos_acc = list(accumulate(sorted(filter(lambda x: x>0, diffs), reverse=True)))
  negatives = list(filter(lambda x: x<0, diffs))
  neg_sum = abs(sum(negatives))
  num_negs = len(negatives)
  if num_negs == 0:
    print(0)
    return
  for i, pa in enumerate(pos_acc):
    if neg_sum <= pa:
      print(num_negs + i + 1)
      return
  print(-1)


if __name__ == '__main__':
  main()

Submission status: Accepted

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

lack = 0
count = 0
C = [0] * N
for i in range(N):
    if A[i] < B[i]:
        count = count + 1
        lack = lack + B[i] - A[i]
    else:
        C[i] = A[i] - B[i]

if lack == 0:
    print("0")
elif count == N:
    print("-1")
else:
    C = sorted(C, reverse = True)
    for i in range(N):
        if C[i] == 0:
            break
        if lack <= C[i]:
            count = count + 1
            lack = 0
            break
        else:
            lack = lack - C[i]
            count = count + 1
    if lack == 0:
        print(count)
    else:
        print("-1")

Submission status: Accepted

Solution code:
from sys import stdin

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

    plus, minus = [], []
    for a, b in zip(A, B):
        n = a - b
        if n < 0:
            minus.append(n)
        else:
            plus.append(n)

    x = sum(minus)
    if abs(x) > sum(plus):
        print(-1)
        return
    elif x == 0:
        print(0)
        return
    else:
        ans = len(minus)

    plus.sort(reverse=1)
    for i in plus:
        x += i
        ans += 1
        if x >= 0:
            break
    print(ans)

input = lambda: stdin.readline()
main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
_ = input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))

diff = [i - j for i, j in zip(a, b)]

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

short = [i for i in diff if i < 0]
change = len(short)
shortage = sum(short)

over = [i for i in diff if i > 0]
for i in sorted(over, reverse=True):
    if shortage < 0:
        change += 1
        shortage += i
    else:
        break
print(change)

Submission status: Accepted

Solution code:
N = int(input())
a =list(map(int,input().split()))
b =list(map(int,input().split()))
dif = []
for i in range(N):
	dif.append(a[i]-b[i])
dif.sort()
if sum(dif) < 0:
	print(-1)
else:
	Count1 = 0
	Sum1 = 0
	for i in range(N):
		if dif[i] < 0:
			Count1 += 1
			Sum1 += dif[i]
		else:
			c = Count1
			break
	Sum2 = 0
	for i in range(N-c):
		if  Sum2 < (-1)*Sum1:
			Sum2 += dif[N-i-1]
			Count1 += 1
		else:
			break
	print(Count1)

Submission status: Accepted

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

ans = -1

not_enough = []
enough = []

for i in range(N):
    if A[i] - B[i] < 0:
        not_enough.append(abs(A[i] - B[i]))
    else:
        enough.append(A[i] - B[i])

enough.sort()
enough.reverse()

#print(enough)
#print(not_enough)

count = 0
whre = 0
res_eno = 0
res_not_eno = 0

if len(not_enough) == 0:
    print('0')
elif len(enough) == 0:
    print('-1')
else:
    for i in enough:

        res_eno = i
        count += 1

        while res_eno > 0:
            #print("count" + (str)(count))
            #print("whre" + (str)(whre))
            #print("res_eno" + (str)(res_eno))
            #print("res_not_eno" + (str)(res_not_eno))

            if res_not_eno == 0:
                if res_eno >= not_enough[whre]:
                    #print('A')
                    res_eno -= not_enough[whre]
                    count += 1
                    whre += 1
                    if whre == len(not_enough):
                        break
                else:
                    #print('B')
                    res_not_eno = not_enough[whre] - res_eno
                    res_eno = 0
            else:
                if res_eno >= res_not_eno:
                    #print('C')

                    res_eno -= res_not_eno
                    res_not_eno = 0
                    count += 1
                    whre += 1
                    if whre == len(not_enough):
                        break
                else:
                    #print('D')

                    res_not_eno = res_not_eno - res_eno
                    res_eno = 0
        if whre == len(not_enough):
            break

    if res_not_eno == 0 and whre == len(not_enough):
        print(count)
    else:
        print('-1')

Submission status: Accepted

Solution code:
def solve(n, a, b):
    negatives = [b[i] - a[i] for i in range(n) if a[i] < b[i]]
    positives = [a[i] - b[i] for i in range(n) if a[i] > b[i]]
    s = sum(negatives)
    positives.sort(reverse=True)
    
    if s == 0:
        return 0
    
    accum = 0
    for i, a in enumerate(positives):
        accum += a
        if accum >= s:
            return len(negatives) + i + 1

    return -1

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

    print(solve(n, a, b))


main()

Submission status: Accepted

Solution code:
import sys

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    AB = list(zip(A, B))
    bigger_diff = list(map(lambda ab: max(ab[0] - ab[1], 0), AB))
    lesser_diff = list(map(lambda ab: min(ab[0] - ab[1], 0), AB))
    exceeded = sum(bigger_diff)
    shortage = sum(lesser_diff)

    if exceeded < abs(shortage):
        print(-1)
        sys.exit()

    if all(map(lambda ab: ab[0] >= ab[1], AB)):
        print(0)
        sys.exit()



    ans = len(list(filter(lambda d: d<0, lesser_diff)))

    bigger_diff = sorted(bigger_diff, reverse=True)

    ss = 0
    for i in range(len(bigger_diff)):
        ss += bigger_diff[i]
        if ss >= abs(shortage):
            ans += i + 1
            break
    print(ans)



if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
ary = list(map(int, input().split()))
bry = list(map(int, input().split()))

n
ary
bry

if sum(ary) < sum(bry):
    print(-1)

else:
    diff = sorted([ary[i] - bry[i] for i in range(n)])

    ns = [d for d in diff if d < 0]
    minus = abs(sum(ns))
    cnt = len(ns)
    for p in diff[::-1]:
        if minus <= 0:
            break
        else:
            minus -= p
            cnt += 1

    print(cnt)

Submission status: Accepted

Solution code:
import numpy as np

def solve(A, B):
  diff = np.array(A) - np.array(B)
  shortage = list(filter(lambda d: d < 0, diff))
  remainder = sorted(list(filter(lambda d: d > 0, diff)), reverse=True)
  short_sum = sum(shortage)
  if short_sum == 0: return 0
  counter = 0
  for r in remainder:
    short_sum += r
    counter += 1
    if short_sum >= 0: return len(shortage) + counter
  return -1

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

print(solve(A, B))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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 A.sum() < B.sum():
        res = -1
    else:
        C = B-A
        d = np.maximum(C,0).sum()
        M = np.minimum(np.maximum(C,0),1).sum()
        C.sort()
        for c in C:
            if d <= 0:
                break
            d += c
            M += 1
        res = M
    return str(res)

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

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()
    
# 少ないところの合計 + 差が大きい順にソートして、全マイナス分を引ききるまでの個数

point = 0
upper = []
count = 0

for a, b in zip(A, B):
    if a < b:
        # 準備不足
        point += b - a
        count += 1
        
    else:
        # 準備OK
        upper.append(a - b)
        
upper.sort(reverse = True)

index = 0
while point > 0:
    point -= upper[index]
    index += 1
    
print(count + index)

Submission status: Accepted

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

if sum(A) < sum(B) : 
    print(-1)
else :
    diff=[A[i] - B[i] for i in range(len(A))]
    pos=sorted([x for x in diff if x > 0])
    neg=[x for x in diff if x < 0]
    nes=sum(neg)
    if nes >= 0 :
        print(0)
    else:
        items = 0
        total_pos = 0
        for p in reversed(pos):
            items += 1
            total_pos += p
            if(total_pos + nes >= 0) :
                print(items + len(neg))
                break

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array([int(x) for x in input().split()])
b = np.array([int(x) for x in input().split()])

c = np.sort(a - b).tolist()
c_minus = [x for x in c if x < 0]
c_plus = sorted([x for x in c if x > 0], reverse=True)

cnt = len(c_minus)
cnt_sum = -sum(c_minus)

while cnt_sum > 0:
    if len(c_plus) <= 0:
        cnt = -1
        break
    cnt_sum -= c_plus.pop(0)
    cnt += 1
print(cnt)

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:
    ans=0
    sco=0
    C=[]
    for i in range(N):
        if A[i]<B[i]:
            ans+=1
            sco+=B[i]-A[i]
        elif A[i]>B[i]:
            C.append(A[i]-B[i])
    if ans==0:
        print(0)
        exit()
    C.sort(reverse=True)
    
    for i in range(len(C)):
        ans+=1
        sco-=C[i]
        if sco<=0:
            break
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().strip().split()))
Bs = list(map(int,input().strip().split()))

diff = []
for i in range(0,N):
    diff.append(As[i] - Bs[i])

minuss = list(filter(lambda x: x < 0, diff))
sorted_diff = sorted(diff,reverse=True)
pla = list(filter(lambda x: x >= 0, sorted_diff))

minus_num = len(minuss)
minus = sum(minuss)
i = 0
while i < len(pla):
    if minus >= 0:
        print(i + minus_num)
        exit()
    minus += pla[i]
    i += 1

if minus >= 0:
    print(i + minus_num)
else:
    print("-1")

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/keyence2019/tasks/keyence2019_c

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

# check already can pass
need_prepare = 0
need_change = 0
prepare_margin = 0
for a, b in zip(A, B):
    if a < b:
        need_prepare += b - a
        need_change += 1
    else:
        prepare_margin += a - b

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

# check impossibility
if prepare_margin < need_prepare:
    print(-1)
    exit(0)

# find
margins = []
for i in range(len(A)):
    margins.append((A[i] - B[i], i))

# print(margins)
margins = sorted(margins, reverse=True)
# print(margins)
# print(need_prepare)

for i in range(len(margins)):
    need_prepare -= margins[i][0]
    need_change += 1
    if need_prepare <= 0:
        break

print(need_change)

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

loss = 0
count = 0
c = n*[0]
for i in range(n):
    if(a[i] < b[i]):
        loss += b[i] - a[i]
        count += 1
    else: c[i] = a[i] - b[i]


c.sort(reverse=True)
for i in range(n):
    if(loss <= 0): break
    count += 1
    loss -= c[i]

print(count)

Submission status: Accepted

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

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

diffs = sorted([(ai, bi, ai - bi) for ai, bi in zip(a, b)], key = lambda x: x[2])
# print(diffs)
if diffs[0][2] >= 0:
    print(0)
    exit()

remains = [d for _, _, d in diffs]
idx = 1
remains_changed = deepcopy(remains)
for i, d in enumerate(remains_changed):
    if d >= 0:
        break
    else:
        while d < 0:
            if remains_changed[-idx] >= -d:
                remains_changed[-idx] += d
                d = 0
                remains_changed[i] = 0
            else:
                d += remains_changed[-idx]
                remains_changed[-idx] = 0
                idx += 1
# print(remains)
# print(remains_changed)
ans = 0
for r, rc in zip(remains, remains_changed):
    if r != rc:
        ans += 1
print(ans)

Submission status: Accepted

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 v < 0]
total = sum(neg)
num = len(neg)

if sum(c) < 0:
    print(-1)
elif num == 0:
    print(0)
else:
    for v in reversed(c[num:]):
        total += v
        num += 1
        if total >= 0:
            break
    print(num)

Submission status: Accepted

Solution code:
import copy
N=int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
plus=[]
minus=[]
for a,b in zip(A,B):
	if a < b:
		minus.append(b - a)
	elif b < a:
		plus.append(a - b)
plus.sort(reverse=True)
p = 0
m = 0
befor = copy.deepcopy(plus)
while m != len(minus):
	if p == len(plus):
		print(-1)
		exit()
	if plus[p] > minus[m]:
		plus[p] -= minus[m]
		m += 1
	elif plus[p] == minus[m]:
		p += 1
		m += 1
	else: #plus[p] < minus[m]
		minus[m] -= plus[p]
		p += 1
ans = len(minus)
if plus[p] != befor[p]:
	ans += p + 1
else:
	ans += p
print(ans)

Submission status: Accepted

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

diff=[a[i]-b[i] for i in range(n)]
diff.sort()
# print(diff)
suma=[0 for i in range(n)]
sumb=[0 for i in range(n)]
suma[0]=a[0]
sumb[0]=b[0]
for i in range(n-1):
    suma[i+1]=suma[i]+a[i+1]
    sumb[i+1]=sumb[i]+b[i+1]
if suma[n-1]>sumb[n-1]:
    point=0
    cnt=0
    for i in range(n):
        if diff[i]>=0:
            break
        point+=diff[i]
        cnt+=1
    # print(point,cnt)
    i=-1
    if point<0:
        for i in range(n):
            point+=diff[n-1-i]
            if point>=0:
                break
    print(cnt+i+1)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

D = []

miCount = 0
miSum = 0

P = []

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

for i in range(N):
    d = A[i] - B[i]
    D.append(d)
    if d < 0:
        miSum += d
        miCount += 1
    elif d > 0:
        P.append(d)


P.sort(reverse=True)
pCount = 0
for p in P:
    if miSum >= 0:
        break
    pCount += 1
    miSum += p

print(pCount + miCount)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

diff = []

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

diff.sort()
ans = 0

if diff[0] >= 0:
    print(0)
    exit()

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

i = 0
j = n-1
memo = [0] * n
while True:
    if i >= j:
        print(sum(memo))
        exit()
    if diff[i] >= 0:
        print(sum(memo))
        exit()

    if diff[j] > -diff[i]:
        diff[j] += diff[i]
        memo[j] = 1
        memo[i] = 1
        i += 1

    elif diff[j] < -diff[i]:
        diff[i] += diff[j]
        memo[i] = 1
        memo[j] = 1
        j -= 1

    else:
        memo[i] = 1
        memo[j] = 1
        i += 1
        j -= 1

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
G1=[]
G2=[]
for i in range(N):
  t=A[i]-B[i]
  if t>=0:
    G1.append(t)
  else:
    G2.append(t)
G1.sort(reverse=True)    
k=len(G2)

if k==0:
  print(0)
else:
  s=sum(G2)
  for i in range(len(G1)):
    s+=G1[i]
    if s>=0:
      print(k+i+1)
      break
  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()]
d = [a[i]-b[i] for i in range(n)]
e = [i for i in d if i > 0]
f = [i for i in d if i < 0]
e.sort(reverse = True)
g = abs(sum(f))
if sum(d) < 0:
    print(-1)
else:
    if f == []:
        print(0)
    else:
        sum = 0
        for i in range(len(e)):
            sum += e[i]
            if sum >=  g:
                print(i+len(f)+1)
                break

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

d = [0] * n

m = []
p = []
for i in range(n):
    d[i] = a[i] - b[i]
    if d[i] > 0:
        p.append(d[i])
    if d[i] < 0:
        m.append(d[i])

if sorted(d)[0] >= 0:
    print(0)
    exit()

p.sort(reverse=True)
m.sort()

for i in range(1, len(p)):
    p[i] += p[i - 1]

sum_m = sum(m)

for i in range(len(p)):
    if p[i] >= abs(sum_m):
        print(len(m) + i + 1)
        exit()

Submission status: Accepted

Solution code:
IMPOSSIBLE = -1


def solve(xs, ys):
    if sum(xs) < sum(ys):
        return IMPOSSIBLE
    n = len(xs)
    ts = [x - y for x, y in zip(xs, ys)]
    da_idxs = sorted(range(n), key=lambda i: ts[i])
    us = ts[:]
    pos_donor = n - 1
    pos_acceptor = 0
    while pos_acceptor < pos_donor:
        acceptor_actidx = da_idxs[pos_acceptor]
        donor_actidx = da_idxs[pos_donor]
        if us[acceptor_actidx] < 0:
            delta = min(-us[acceptor_actidx], us[donor_actidx])
            us[acceptor_actidx] += delta
            us[donor_actidx] -= delta
        if us[donor_actidx] <= 0:
            pos_donor -= 1
        if us[acceptor_actidx] >= 0:
            pos_acceptor += 1
    return sum(t != u for t, u in zip(ts, us))


def main():
    _ = int(input())
    xs = [int(x) for x in input().split()]
    ys = [int(x) for x in input().split()]
    print(solve(xs, ys))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()] 
B = [int(i) for i in input().split()] 

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

# -1になっている数を数える
minus = [x for x in delta if x < 0]
plus = [x for x in delta if x >= 0]

plus.sort()
plus.reverse()
min_sum = sum(minus)
min_sum = min_sum * -1

num = -1
plu_sum = 0

if min_sum == 0:
    num = 0
else:
    for i in range(len(plus)):
        plu_sum += plus[i]
        if plu_sum >= min_sum:
            num = i + 1 + len(minus)
            break

print(num)

Submission status: Accepted

Solution code:
n = int(input())
a_array = [int(x) for x in input().split()]
b_array = [int(x) for x in input().split()]

plus = 0
minus = 0
plus_array = []
minus_cnt = 0
for i, j in zip(a_array, b_array):
    if j <= i:
        plus += i - j
        plus_array.append(abs(j - i))
    else:
        minus += j - i
        minus_cnt += 1
    # print(minus, plus)

plus_array = sorted(plus_array, reverse=True)
plus_res = 0
if plus < minus:
    print(-1)
elif minus == 0:
    print(0)
else:
    for i in range(len(plus_array)):
        minus -= plus_array[i]
        plus_res += 1
        if minus < 0:
            print(plus_res + minus_cnt)
            exit()

Submission status: Accepted

Solution code:
import numpy as np
import sys

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)
    sys.exit()
c = A - B
c.sort()
i = 0
j = n - 1
ans = 2
if c[0] >= 0:
    print(0)
    sys.exit()
while True:
    if c[i] < 0:
        if c[j] + c[i] >= 0:
            c[j] += c[i]
            c[i] = 0
        else:
            c[i] += c[j]
            c[j] = 0
            j -= 1
            ans += 1
    else:
        if c[i + 1] >= 0:
            break
        else:
            i += 1
            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()))

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

if sumdiff < 0:
    print(-1)
    sys.exit


else:
    diff.sort()

    ans = 0
    i = 0
    while diff[i] < 0: 
        ans = ans + 1
        i = i + 1

    cost = 0
    for i in range(ans):
       cost = cost + diff[i]
    cost = -1 * cost


    j = n-1
    while cost > 0:
        cost = cost - diff[j]
        j = j - 1
        ans = ans + 1

    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np
# いっこ入力
n =int(input())

# 空白区切りの数列をリストに代入
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

d = []
value  =0
value_d =0
number =0
for i in range(n):
    if a[i] < b[i]:
        value += b[i] - a[i]
        number += 1
    else:
        d.append(a[i]-b[i])
        value_d += a[i]-b[i]

if(value > value_d):
    print("-1")
    exit()
d.sort(reverse=True)
if value <= 0:
    print("0")
    exit()

for i in range(len(d)):
    value = value - d[i]
    number += 1
    if value <= 0:
        break
print(number)

Submission status: Accepted

Solution code:
N = int(input())
An = map(int, input().split(maxsplit=N))
Bn = map(int, input().split(maxsplit=N))

minus = []
plus = []

for Ai, Bi in zip(An, Bn):
    if Ai > Bi:
        plus.append(Ai - Bi)
    elif Ai < Bi:
        minus.append(Ai - Bi)

plus = sorted(plus)


if sum(plus) + sum(minus) < 0:
    print(-1)
    exit()


change_plus = 0
sum_minus = sum(minus)
while sum_minus < 0:
    sum_minus += plus.pop()
    change_plus += 1
print(str(change_plus + len(minus)))

Submission status: Accepted

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

count = 0
difference = []

for i in range(len(A)):
    difference.append(A[i] - B[i])

if min(difference) >= 0:
    print(count)
    quit()


difference.sort()
minussum = 0
for d in difference:
    if d < 0:
        count = count + 1
        minussum = minussum + d
difference.reverse()

plussum = 0
for d in difference:
    plussum = plussum + d
    count = count + 1
    if plussum + minussum >= 0:
        print(count)
        quit()

print(-1)

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])
if sum(c)<0:
    print(-1)
else:
    c.sort()
    s=[i for i in c if i<0]
    if len(s)==0:
        print(0)
    else:
        d=sum(s)
        t=0
        for i in range(n-1,-1,-1):
            t+=c[i]
            if t>=-d:
                print(n-i+len(s))
                break

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)
else:
    diff1 = 0
    num = 0
    diff2 = []
    for i in range(N):
        if A[i] < B[i]:
            diff1 += B[i] - A[i]
            num += 1
        elif A[i] > B[i]:
            diff2.append(A[i] - B[i])
    if num == 0:
        print(0)
    else:
        diff2.sort(reverse=True)
        sum = 0
        for i in range(len(diff2)):
            sum += diff2[i]
            if sum >= diff1:
                print(num + i + 1)
                sys.exit()

Submission status: Accepted

Solution code:
N = raw_input().split()
N = int(N[0])
a_list = raw_input().split()
a_list = [int(x) for x in a_list]
b_list = raw_input().split()
b_list = [int(x) for x in b_list]
if sum(a_list) < sum(b_list):
    print -1
else:
    diff_list = []
    for a, b in zip(a_list, b_list):
        diff_list.append(a-b)
    minus_sum = sum([x for x in diff_list if x < 0])
    minus_len = len([x for x in diff_list if x < 0])
    plus_list_sorted = sorted([x for x in diff_list if x > 0], reverse=True)
    tmp_idx = 0
    for item in plus_list_sorted:
        if minus_sum >= 0:
            break
        minus_sum += item
        tmp_idx += 1
    print minus_len + tmp_idx

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

num = 0
diff = 0
C = []
for a, b in zip(A, B):
  if a < b:
    num += 1
    diff -= (b - a)
  elif a > b:
    C.append(int(a - b))

if diff == 0:
  print(0)
else:
  C.sort(reverse=True)
  for v in C:
    num += 1
    diff += v
    if diff >= 0:
      break

  #print(diff)
  if diff < 0:
    print(-1)
  else:
    print(num)

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

sa=[]
cnt=0
ans=0
for ai, bi in zip(a, b):
    sa.append(ai-bi)
    if ai<bi:
        cnt+=bi-ai
        ans+=1
sa.sort(reverse=True)

for sai in sa:
    if cnt<=0:
        break
    cnt-=sai
    ans+=1

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

Submission status: Accepted

Solution code:
import numpy as np
flag=0
N=int(input())
A=np.array(list(map(int,input().split())))
B=np.array(list(map(int,input().split())))
if np.sum(A-B)<0:
    flag=-1
D=np.sort(A-B)
failNum=0
failSum=0
for i in range(N):
    if D[i]<0:
        failNum+=1
        failSum+=D[i]
passNum=0
if flag==0:
    while failSum<0:
        failSum+=D[N-1-passNum]
        passNum+=1
if flag==-1:
    print(-1)
else:
    print(failNum+passNum)

Submission status: Accepted

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

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

minus = 0
count = 0
for i in sub:
    if i >= 0:
        break
    count += 1
    minus += i
sub.reverse()

for i in sub:
    if minus >= 0:
        break
    minus += i
    count += 1
if minus < 0:
    print(-1)
else:
    print(count)

Submission status: Accepted

Solution code:
n = int(input())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))

if sum(aList)< sum(bList):
    print(-1)
else:
    saList=[]
    minusSum=0
    minusCount=0
    for i in range(n):
        sa=aList[i]-bList[i]
        if sa<0:
            minusCount+=1
            minusSum+=sa
        saList.append(sa)
    saList.sort()
    saList.reverse()
    
    i=0
    while(minusSum<0):
        minusSum=minusSum+saList[i]
        i+=1

    ans=minusCount+i
    print(ans)

Submission status: Accepted

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

ans = 0
s = []
negative = 0
for i, j in zip(a, b):
    s.append(i - j)
    if i - j < 0:
        negative += i - j
        ans += 1

s.sort(reverse=True)

if ans != 0:
    if sum(s) < 0:
        ans = -1
    else:
        for i , p in enumerate(s):
            negative += p
            if negative >= 0:
                ans += i + 1
                break
    

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
dif = [a[i] - b[i] for i in range(n)]
dif_abs = [abs(dif[i]) for i in range(n)]
sorted_dif = sorted(dif)
dif_minus = [(dif_abs[i] - dif[i]) // 2 for i in range(n)]
total_minus = sum(dif_minus)
len_minus = sum(x>0 for x in dif_minus)

res = 0

if sum(a) < sum(b):
    print(-1)
else:
    if len_minus == 0:
        print(res)
    else:
        res += len_minus
        while True:
            if total_minus <= 0:
                break
            total_minus -= sorted_dif.pop()
            res += 1

        print(res)