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:

def main():
    buf = input()
    N = int(buf)
    buf = input()
    buflist = buf.split()
    A = list(map(int, buflist))
    buf = input()
    buflist = buf.split()
    B = list(map(int, buflist))
    if sum(A) < sum(B):
        print(-1) # impossible
        return
    diff = []
    for i in range(N):
        diff.append(A[i] - B[i])
    diff = list(sorted(diff))
    insufficient_value = 0
    insufficient_count = 0
    for i in range(N):
        if diff[i] < 0:
            insufficient_value += -diff[i]
            insufficient_count += 1
    deduction_count = 0
    pointer = -1
    while insufficient_value > 0:
        insufficient_value -= diff[pointer]
        deduction_count += 1
        pointer -= 1
    print(insufficient_count + deduction_count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8

II = lambda: int(input())
MI = lambda: map(int, input().split())


def main():
    N = II()
    A = tuple(MI())
    B = tuple(MI())
    D = [A[i] - B[i] for i in range(N)]
    if sum(D) < 0: return -1

    md = tuple(filter(lambda d:d < 0, D))
    d = sum(md)
    if d == 0: return 0
    D = sorted(D, reverse=True)
    for i in range(N):
        d += D[i]
        if d >= 0: return len(md) + i + 1
    return -1


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)
else:
    diff = [0] * N
    for i in range(N):
        diff[i] = A[i] - B[i]
    
    diff.sort()

    ans = 0

    l = 0
    r = N - 1

    point = 0
    while(True):
        left = diff[l]
        if left < 0:
            if point >= abs(left):
                ans += 1
                point -= abs(left)
                l += 1
            else:
                ans += 1
                point += diff[r]
                r -= 1
        else:
            break
    
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a_list = [int(a) for a in input().split()]
b_list = [int(a) for a in input().split()]

if sum(a_list) < sum(b_list):
    print("-1")
else:
    d_list = [a - b for a, b in zip(a_list, b_list)]
    d_list.sort()
    minus = [d for d in d_list if d < 0]
    deficit = sum(minus)
    ans = len(minus)
    if deficit == 0:
        print("0")
    else:
        for i in range(N):
            deficit += d_list[-i - 1]
            if deficit >= 0:
                ans += i + 1
                break
        print(str(ans))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
plus=0
minus=[]
for a,b in zip(A,B):
    if a<b:
        ans += 1
        plus += (b-a)
    elif a>b:
        minus.append(a-b)
minus.sort(reverse=True)
while True:
    if plus<=0:
        print(ans)
        break
    if minus==[]:
        print(-1)
        break
    plus -= minus.pop(0)
    ans += 1

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    D = [0] * N
    if sum(A) < sum(B):
        print(-1)
        return
    retval = 0
    minussum = 0
    for i in range(N):
        D[i] = A[i] - B[i]
        if D[i] < 0:
            minussum += D[i]
            retval += 1
    D.sort(reverse=True)

    for d in D:
        if minussum >= 0:
            break
        if d > 0:
            minussum += d
            retval += 1
    print(retval)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sumA = sum(A)
sumB = sum(B)
if sumA < sumB:
  print('-1')
  exit()

D = [a - b for a, b in zip(A, B)]
over0s = []
sumMinus = 0
ans = 0
for d in D:
  if d < 0:
    ans += 1
    sumMinus += d
  else:
    over0s.append(d)
over0s.sort()
while sumMinus < 0:
  ans += 1
  sumMinus += over0s.pop()
print(ans)

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

LOW=[B[i]-A[i] for i in range(n) if B[i]>A[i]]
HIGH=[A[i]-B[i] for i in range(n) if A[i]>B[i]]

HIGH.sort(reverse=True)

ANS=len(LOW)
rest=sum(LOW)

i=0
while rest>0:
    if i==len(HIGH):
        print(-1)
        sys.exit()
    rest-=HIGH[i]
    i+=1
    ANS+=1

print(ANS)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N = d_in()
A_list = np.array(ds_in(), dtype=int)
B_list = np.array(ds_in(), dtype=int)

if sum(A_list) < sum(B_list):
    print(-1)
    exit()

diff = A_list - B_list
neg = - diff[diff < 0].sum()
ans = (diff < 0).sum()

plus = np.sort(diff)[::-1]
for p in plus:
    if neg <= 0:
        break
    ans += 1
    neg -= p

print(ans)

Submission status: Accepted

Solution code:

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

C_plus = []
C_minus = []
for i in range(N):
    c = A[i]-B[i]
    if c>=0:
        C_plus.append(c)
    else:
        C_minus.append(c)
    
C_plus.sort()
C_plus = C_plus[::-1]

minus_num=len(C_minus)
minus=-sum(C_minus)

count=0
for i in range(len(C_plus)):
    if minus<=0:
        break
    minus-=C_plus[i]
    count+=1

if minus>0:
    print(-1)
else:
    print(minus_num+count)

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

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/keyence2019/tasks/keyence2019_c
#2019-01-13

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(reverse = 1)

rakudai = 0
fusoku = 0
for i in range(N):
    if D[i] < 0 :
        rakudai += 1
        fusoku -= D[i]

if sum(A) < sum(B):
    print(-1)
elif rakudai == 0:
    print(0)
else:
    hitsuyou = [0, 0]
    for i in range(N):
        hitsuyou[0] += 1
        hitsuyou[1] += D[i]
        if hitsuyou[1] >= fusoku:
            print(rakudai + hitsuyou[0])
            break

Submission status: Accepted

Solution code:

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

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

D = [a-b for a, b in zip(A, B)]
D.sort()

#print(D)
mi = 0
ans = 0
for d in D:
    if d < 0:
        mi += d
        ans += 1

for i, d in enumerate(D[::-1], 0):
    if mi >= 0:
        print(i+ans)
        exit()
    mi += d
print(i+ans)

Submission status: Accepted

Solution code:
# C
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

res = 0
diff = 0
D_list = [0]*N


for i in range(N):
    if A_list[i] < B_list[i]:
        diff += B_list[i] - A_list[i] 
        res += 1
    else:
        D_list[i] = B_list[i] - A_list[i]

D_list_s = list(sorted(D_list))

for i in range(N):
    if diff <= 0:
        break
    else:
        res += 1
        diff += D_list_s[i]
        
if diff > 0:
    print(-1)
else:
    print(res)

Submission status: Accepted

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

N = int(fin())
A_list = [int(elem) for elem in fin().split()]
B_list = [int(elem) for elem in fin().split()]

if sum(B_list) > sum(A_list):
    print(-1)
    exit(0)


surplus_list = []
debt_sum = 0
num_changed = 0
for i, (A, B) in enumerate(zip(A_list, B_list)):
    if A - B < 0:
        debt_sum += B - A
        num_changed += 1
    elif A - B > 0:
        surplus_list.append(A - B)

surplus_list.sort(reverse=True)
cur_sum_surplus = 0
for surplus in surplus_list:
    if cur_sum_surplus >= debt_sum:
        break
    else:
        cur_sum_surplus += surplus
        num_changed += 1
print(num_changed)

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

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


def solve(N, As, Bs):
    A_sum = sum(As)
    B_sum = sum(Bs)
    if A_sum < B_sum:
        return -1
    margin = A_sum - B_sum
    
    ans = 0
    need = 0
    extra = []
    for a, b in zip(As, Bs):
        if b > a:
            need += b - a
            ans += 1
        elif a > b:
            extra.append(a - b)
    extra.sort()
    extra.reverse()
    i = 0
    while need > 0:
        need -= extra[i]
        i += 1
        ans += 1
    return ans

print(solve(N, As, Bs))

Submission status: Accepted

Solution code:
n = input()
a = map(int, raw_input().split())
b = map(int, raw_input().split())
greater = []
req = 0
ans = 0
for i in xrange(n):
	if a[i] > b[i]:
		greater.append(a[i]-b[i])
	elif a[i] < b[i]:
		req += b[i] - a[i]
		ans += 1
if sum(greater) < req:
	print -1
else:
	greater.sort(reverse = True)
	ptr = 0
	while req > 0:
		req -= greater[ptr]
		ptr += 1
		ans += 1
	print ans

Submission status: Accepted

Solution code:
import sys
N, = map(int, raw_input().split())
A = map(int, raw_input().split())
B = map(int, raw_input().split())
if sum(A)<sum(B):
  print -1
  sys.exit(0)
dl = []
dm = []
for i in xrange(N):
  d = A[i]-B[i]
  if d>0:
    dm.append(d)
  if d<0:
    dl.append(d)
dm.sort()
sdl=abs(sum(dl))
j=0
sdm=0
for dmj in dm[::-1]:
  if sdm>=sdl:
    break
  j+=1
  sdm+=dmj
print len(dl)+j

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

C = []
minus = 0
ans = 0

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

C.sort(reverse = True)
for c in C:
    if minus >= 0:
        break
    minus += c
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
X=sum(A)
Y=sum(B)
if X<Y:
    print(-1)
    exit()
C=[A[i]-B[i] for i in range(N)]
L=[]
R=[]
for i in range(N):
    if C[i]<0:
        L.append(-C[i])
    else:
        R.append(C[i])
R.sort(reverse=True)
Z=sum(L)
K=0
M=len(R)
l=-1
for i in range(M):
    K+=R[i]
    if K>=Z:
        l=i+1
        break
if len(L)==0:
    print(0)
else:
    print(len(L)+l)

Submission status: Accepted

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

need = 0
small_num = 0
large = []
for a, b in zip(A, B):
    if a > b:
        large.append(a - b)
    elif a < b:
        need += b - a
        small_num += 1
large.sort()
for i, x in enumerate(reversed(large)):
    if need <= 0:
        print(i + small_num)
        quit()
    need -= x
if need <= 0:
    print(len(large) + small_num)
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()
if all(a>=b for a,b in zip(A,B)):
    print(0)
    exit()

lack_v = lack_n = 0
gains = []
for a,b in zip(A,B):
    if a<b:
        lack_n += 1
        lack_v += b-a
    else:
        gains.append(a-b)

ans = lack_n
for g in sorted(gains, reverse=True):
    lack_v -= g
    ans += 1
    if lack_v <= 0:
        print(ans)
        exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
Over = []
Minus = 0
Change = 0
for i in range(N):
    if A[i] >= B[i]: Over.append(A[i]-B[i])
    else:
        Minus += B[i] - A[i]
        Change += 1
if Minus == 0: print(0)
else:
    Over.sort(reverse = True)
    for o in Over:
        if Minus >= o:
            Minus -= o
        else: Minus = 0
        Change += 1
        if Minus == 0: break
    if Minus > 0: print(-1)
    else: print(Change)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect

setrecursionlimit(10**7)

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()
A = reads()
B = reads()

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

failed = [B[i] - A[i] for i in range(N) if B[i] - A[i] > 0]
succ = [A[i] - B[i] for i in range(N) if A[i] - B[i] > 0]

ans = len(failed)
rem = sum(failed)
succ.sort()
while rem > 0:
  rem -= succ.pop()
  ans += 1
print(ans)

Submission status: Accepted

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

X = [] # 足りてる
d = 0
c = 0

for i in range(N):
    if A[i] > B[i]:
        X.append(A[i]-B[i])
    elif A[i] < B[i]:
        d += B[i] - A[i]
        c += 1

if sum(X) < d:
    print(-1)
else:
    X = sorted(X)[::-1]
    i = 0
    while d > 0:
        d -= X[i]
        i += 1
        c += 1
        
    print(c)

Submission status: Accepted

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

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

yoyuu = [A[i]-B[i] for i in range(N)]
yoyuu.sort(reverse=True)
# print(yoyuu)
if sum(yoyuu) < 0:
    print(-1)
    sys.exit()
    
nega_sum = 0
ans = 0
for tmp in yoyuu:
    if tmp < 0:
        nega_sum -= tmp
        ans += 1
tmp_sum = 0
for tmp in yoyuu:
    if tmp_sum < nega_sum and tmp > 0:
        tmp_sum += tmp
        ans += 1
        
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys
#sys.setrecursionlimit(200000)
#input = sys.stdin.readline
n = int(input())
#x,y = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = []
mai = 0
maigo = 0
for i in range(n):
    c.append(a[i]-b[i])
    if a[i]-b[i]<0:
        mai += 1
        maigo -= a[i]-b[i]
c.sort(key=lambda x:-x)
if mai == 0:
    print(0)
    exit()
ans = mai
for i in range(n):
    if c[i] < 0:
        print(-1)
        exit()
    maigo -= c[i]
    ans += 1
    if maigo <= 0:
        print(ans)
        exit()

Submission status: Accepted

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

ans = 0
com = 0

for i in range(n):
    diff[i] = A[i] - B[i]
    if A[i] - B[i] < 0:
        ans += 1
        com += A[i] - B[i]

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

diff.sort()
d = -1
while d >= -1 * n and diff[d] > 0:
    ans += 1
    com += diff[d]
    d -= 1
    if com >= 0:
        print(ans)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    a = map(int, stdin.readline().split(), repeat(10, n))
    b = map(int, stdin.readline().split(), repeat(10, n))
    if sum(a) < sum(b):
        print -1
        return
    ans = s = 0
    p = []
    for i in xrange(n):
        if a[i] < b[i]:
            s += b[i] - a[i]
            ans += 1
        elif a[i] > b[i]:
            p.append(a[i] - b[i])
    p.sort(reverse=True)
    for x in p:
        if s <= 0:
            break
        s -= x
        ans += 1
    print ans
main()

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
xlst = []
ylst = []
for a, b in zip(alst, blst):
    if a < b:
        xlst.append(b - a)
    if a > b:
        ylst.append(a - b)

total = sum(xlst)
ans = len(xlst)
ylst.sort(reverse=True)
if total == 0:
    print(ans)
else:
    for y in ylst:
        total -= y
        ans += 1
        if total <= 0:
            print(ans)
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

n=int(input())
a=list(mii())
b=list(mii())

need=0
ans=0
helper=[]
for i in range(n):
    if a[i]>=b[i]:
        helper.append(a[i]-b[i])
    else:
        need+=b[i]-a[i]
        ans+=1

helper.sort()
while need>0 and helper:
    need-=helper[-1]
    helper.pop(-1)
    ans+=1

if need>0: print(-1)
else: print(ans)

Submission status: Runtime Error

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

import sys
# sys.setrecursionlimit(100000)
import heapq


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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = input_int_list()
    B = input_int_list()

    if sum(A) < sum(B):
        print(-1)
        return
    plus = []
    minus = []

    # a < b はすべて調整の必要あり
    # a < b の補填は それぞれの b > a のうち b-a が大きいところから貪欲に補填する
    for i in range(n):
        d = B[i] - A[i]
        if d > 0:
            minus.append(d)
        else:
            heapq.heappush(plus, d)

    ans = 0
    charge = 0
    for d in minus:
        while d > 0:
            if charge == 0:
                if heapq:
                    charge = -heapq.heappop(plus)
                    ans += 1
                else:
                    print(-1)
                    return
            if d <= charge:
                charge -= d
                d = 0
            else:
                charge = 0
                d -= charge

    print(len(minus) + ans)
    return


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

import sys
# sys.setrecursionlimit(100000)
import heapq


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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = input_int_list()
    B = input_int_list()

    if sum(A) < sum(B):
        print(-1)
        return
    plus = []
    minus = []

    # a < b はすべて調整の必要あり
    # a < b の補填は それぞれの b > a のうち b-a が大きいところから貪欲に補填する
    for i in range(n):
        d = B[i] - A[i]
        if d > 0:
            minus.append(d)
        else:
            heapq.heappush(plus, d)

    ans = 0
    charge = 0
    for d in minus:
        while d > 0:
            if charge == 0:
                charge = -heapq.heappop(plus)
                ans += 1
            if d <= charge:
                charge -= d
                d = 0
            else:
                charge = 0
                d -= charge

    print(len(minus) + ans)
    return


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

surplus = []
short = []

for i in range(n):
    if a[i] > b[i]:
        surplus.append(a[i]-b[i])
    elif b[i] > a[i]:
        short.append(b[i]-a[i])

surplus = sorted(surplus)
taken = False
cnt = 0
for i in range(len(short)):
    cost = short.pop()
    cnt += 1

    while cost > 0:
        if len(surplus) == 0:
            print(-1)
           	exit()

        take = min(cost, surplus[-1])
        cost -= take
        surplus[-1] -= take

        if not taken:
            taken = True
            cnt += 1

        if surplus[-1] == 0:
            surplus.pop()
            taken = False

print(cnt)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int64_t MOD=1000000007;
const long long INF = 1LL<<60;

int main() {
  int64_t N,M=0,ans=0; cin>>N;
  vector<int64_t> A(N),B(N),C(N);
  rep(i,N) cin>>A.at(i);
  rep(i,N) cin>>B.at(i);
  rep(i,N){
    auto a=A.at(i),b=B.at(i);
    auto c=a-b;
    if(c<0){
      ans++;
      M+=-1*c;
    }
    else C.push_back(c);
  }
  sort(C.begin(),C.end());
  reverse(C.begin(),C.end());
  int64_t i=0,t=C.size();
  while(M>0&&i<t){
    M-=C.at(i);
    ans++;
    i++;
  }
  if(M>0) cout<<-1;
  else cout<<ans;
}

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = sorted([A[i]-B[i] for i in range(N)],reverse=True)
cp = 0
cm = 0
for i in range(N):
    if C[i]>0:
        cp += C[i]
    elif C[i]<0:
        cm += -C[i]
if cm>cp:
    print(-1)
else:
    cur = 0
    tot = C[cur]
    for i in range(N):
        if C[i]<0:
            while tot-abs(C[i])<0:
                tot += C[cur+1]
                cur += 1
            tot += C[i]
print(cur+1+cm)

Submission status: Runtime Error

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

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
  while sum(c[:ans])<sum(d):
    ans+=1
print(ans+len(d))

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    cnt, nega, posi = 0, 0, []
    for x, y in zip(a, b):
        if x < y:
            nega += y - x
            cnt += 1
        else:
            posi.append(x - y)
    posi.sort(reverse=True)
    i = 0
    while nega > 0:
        nega -= posi[i]
        cnt += 1
        i += 1
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)

import numpy as np

N = int(readline())
A = np.array(readline().split(), dtype=np.int64)
B = np.array(readline().split(), dtype=np.int64)

diff = A-B
lack = -diff[diff<0]
remain = diff[diff>0]

x = lack.sum()
remain.sort()
cum = remain[::-1].cumsum()

y = 0 if x == 0 else np.searchsorted(cum,x) + 1

answer = len(lack) + y if y <= len(remain) else -1
print(answer)

Submission status: Runtime Error

Solution code:
N=input()
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a)<sum(b):
    print(-1)
    exit()

count=0

d=[]
for i in range (N):
    _d=a[i]-b[i]
    d.append(_d,i)
print(d)

Submission status: Runtime Error

Solution code:
import heapq
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
if sum(a_list)<sum(b_list):
    print(-1)
else:
    diff_list = [a_list[i]-b_list[i] for i in range(n)]
    posi_heap=heapq.heapify([])
    cnt,diff_sum = 0,0
    for diff in diff_list:
        if diff<0:
            cnt+=1
            diff_sum+=diff
        elif diff>0:
            heapq.heappush(posi_heap,-1*diff)
    diff_sum*=(-1)
    i=0
    while diff_sum>0:
        diff_sum+=heapq.heappop(posi_heap)
        i+=1
    print(i+cnt)

Submission status: Runtime Error

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

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

D = sorted(D)

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

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

Submission status: Runtime Error

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

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

D = sorted(D)

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

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

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

d.sort()

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

cnt_neg = 0
for i in range(n):
    if d[i]<0:cnt_neg+=1

if cnt_neg==0:
    print(0)
    exit()
else:
    ans = cnt_neg

last=n-1
for neg in range(n):
    print(d)
    if d[neg]>=0:break

    while d[neg]<0:
        tmp = min(-d[neg], d[last])
        d[neg] += tmp
        d[last] -= tmp
        if d[last] == 0:
            last-=1
            ans+=1

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

print(ans)

Submission status: Runtime Error

Solution code:
import collections

n, p = map(int, input().split())
res = []
x = p
y = 2
while y*y <= x:
    while x % y == 0:
        res.append(y)
        x //= y
    y += 1
if x > 1:
    res.append(x)

res = collections.Counter(res)
ans = 1
for d in res.items():
    if d[1] >= n:
        ans *= d[0]**d[1]//n
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(n)]
C.sort()

if sum(C)<0:
  print(-1)
  exit()
  
ans = 0

for collections import deque
C = deque(C)
c = 0
while 1:
  nc = C.popleft()
  if nc >=0:
    break
  c+= nc
  ans += 1
  while c<0:
    c+=C.popright()
    ans += 1
print(ans)