Problem p02774 (python)

Fixed submission:
import numpy as np
import sys
 
n,k=map(int,sys.stdin.readline().split())
a=np.array(list(map(int, input().split())))
 
a.sort()
neg=a[a<0]
zero=a[a==0]
pos=a[a>0]
 
def count(x):
    cnt=0
    if x>=0:
        cnt+=len(zero)*n
    cnt+=np.searchsorted(a,x//pos,side='right').sum()
    cnt+=(n-np.searchsorted(a,(-x-1)//(-neg),side='right')).sum()
    cnt-=np.count_nonzero(a*a<=x)
    return cnt//2
 
 
left=-10**18
right=10**18
 
while left+1<right:
    x=(left+right)//2
    out=count(x)
    if out>=k:
        right=x
    if out<k:
        left=x
print(right)

Buggy submission (verdict: Runtime Error):
import math
import itertools
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n,k=map(int,input().split())
a=list(map(int, input().split()))
a=sorted(a)
mlist=[]
zlist=[]
plist=[]
for i in a:
  if i<0:
    mlist.append(i)
  elif i==0:
    zlist.append(i)
  else:
    plist.append(i)
mnum=len(mlist)
znum=len(zlist)
pnum=len(plist)
mans=mnum*pnum
zans=znum*(pnum+mnum)
if znum>=2:
  zans+=combinations_count(znum,2)
pans=0
if pnum>=2:
  pans+=combinations_count(pnum,2)
if mnum>=2:
  pans+=combinations_count(mnum,2)

if k<mans:
  ans=[]
  l=mlist[0]*plist[-1]
  r=mlist[-1]*plist[0]
  while True:
    x=(l+r)//2
    c=0
    for m in mlist:
      for p in plist[::-1]:
        if m*p<x:
          c+=1
        else:
          break
    if c<k:
      l=x
    else:
      r=x
    if r==l+1:
      break
  print(l)
elif k<mans+zans:
  print(0)
else:
  k=k-(mans+zans)
  mlist=mlist[::-1]
  if mnum<2:
    l=plist[0]*plist[1]
    r=plist[-1]*plist[-2]
  elif pnum<2:
    l=mlist[0]*mlist[1]
    r=mlist[-1]*mlist[-2]
  else:
    if mlist[0]*mlist[1]<plist[0]*plist[1]:
      l=mlist[0]*mlist[1]
    else:
      l=plist[0]*plist[1]
    if mlist[-1]*mlist[-2]>plist[-1]*plist[-2]:
      r=mlist[-1]*mlist[-2]
    else:
      r=plist[-1]*plist[-2]
  #print(k)
  while True:
    x=(l+r)//2
    c=0
    max=0
    if mnum>=2:
      for i,m in enumerate(mlist[:-1]):
        for j in mlist[i:]:
          n=m*j
          if n<x:
            c+=1
            if n>=max:max=n
          else:
            break
    if pnum>=2:
      for i,p in enumerate(plist[:-1]):
        for j in plist[i:]:
          n=p*j
          if n<x:
            c+=1
            if n>=max:max=n
          else:
            break
    #print("c"+str(c))
    if c<k:
      l=x
      #print("l"+str(l))
    else:
      r=x
      #print("r"+str(r))
    if r==l+1:
      break
    if c==k:break
  print(l)

Buggy submission (verdict: Runtime Error):
import math
import itertools
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n,k=map(int,input().split())
a=list(map(int, input().split()))
a=sorted(a)
mlist=[]
zlist=[]
plist=[]
for i in a:
  if i<0:
    mlist.append(i)
  elif i==0:
    zlist.append(i)
  else:
    plist.append(i)
mnum=len(mlist)
znum=len(zlist)
pnum=len(plist)
mans=mnum*pnum
zans=znum*(pnum+mnum)
if znum>=2:
  zans+=combinations_count(znum,2)
pans=0
if pnum>=2:
  pans+=combinations_count(pnum,2)
if mnum>=2:
  pans+=combinations_count(mnum,2)

if k<mans:
  ans=[]
  for m in mlist:
    for p in plist:
      ans.append(m*p)
  ans=sorted(ans)
  print(ans[k-1])
elif k<mans+zans:
  print(0)
else:
  k=k-(mans+zans)
  ans=[]
  for i in itertools.combinations(plist, 2):
    ans.append(i[0]*i[1])
  for i in itertools.combinations(mlist, 2):
    ans.append(i[0]*i[1])
  ans=sorted(ans)
  print(ans[k-1])