Problem p02774 (python)

Fixed submission:
#incited numpy source code from 'https://atcoder.jp/contests/abc155/submissions/10155744'

import numpy

n,k=map(int,input().split())
arr=list(map(int,input().split()))
nc=0
zc=0
pc=0
for i in range(n):
  if arr[i]<0:
    nc+=1
  elif arr[i]==0:
    zc+=1
  else:
    pc+=1
ncc=nc*pc
zcc=zc*(nc+pc)+(zc*(zc-1))//2
pcc=(nc*(nc-1))//2+(pc*(pc-1))//2
if ncc<k<=(ncc+zcc):
  print(0)
else:
  arr=numpy.array(arr,dtype=int)
  ms=numpy.msort(numpy.abs(arr[arr<0]))
  ps=numpy.msort(arr[arr>0])
  if k<=ncc:
    k=ncc-k
    l=0
    r=10**18+1
    while r-l!=1:
      mid=(l+r)//2
      cnt=numpy.searchsorted(ps,mid//ms,side='right')
      if cnt.sum()>k:
        r=mid
      else:
        l=mid
    print(-r)
  elif k<=(ncc+zcc):
    print(0)
  else:
    l=0
    r=10**18+1
    k-=(ncc+zcc)
    while r-l!=1:
      mid=(l+r)//2
      cnt1=numpy.searchsorted(ps,mid//ps,side='right')
      dup1=ps**2<=mid
      cnt2=numpy.searchsorted(ms,mid//ms,side='right')
      dup2=ms**2<=mid
      if (cnt1.sum()-dup1.sum()+cnt2.sum()-dup2.sum())//2>=k:
        r=mid
      else:
        l=mid
    print(r)

Buggy submission (verdict: Runtime Error):
#incited numpy source code from 'https://atcoder.jp/contests/abc155/submissions/10155744'

import numpy

n,k=map(int,input().split())
arr=list(map(int,input().split()))
nc=0
zc=0
pc=0
for i in range(n):
  if arr[i]<0:
    nc+=1
  elif arr[i]==0:
    zc+=1
  else:
    pc+=1
ncc=nc*pc
zcc=zc*(nc+pc)+(zc*(zc-1))//2
pcc=(nc*(nc-1))//2+(pc*(pc-1))//2
if ncc<k<=(ncc+zcc):
  print(0)
else:
  arr=numpy.array(arr,dtype=int)
  ms=numpy.msort(numpy.abs(arr[arr<0]))
  ps=numpy.msort(arr[arr>0])
  if k<=ncc:
    k=ncc-k
    l=0
    r=10**18+1
    while r-l!=1:
      mid=(l+r)//2
      cnt=numpy.searchsorted(ps,mid//ms,side='right')
      if cnt.sum()>k:
        r=mid
      else:
        l=mid
    print(-r)
  elif k<=(ncc+zcc):
    print(0)
  else:
    l=0
    r=10**18+1
    k-=(ncc+zcc)
    while r-l!=1:
      mid=(l+r)//2
      cnt1=numpy.searchsorted(ps,mid//ps,side='right')
      dup1=ps**2<=mid
      cnt2=numpy.searchsorted(ms,mid//ms,side='right')
      dup2=ms**2<=mid
      if (cnt1.sum()-dup1.sum()+cnt2.sum()-dup2.sum())//2>=k:
        r=mid
      else:
        l=mid
    print(r)

Buggy submission (verdict: Runtime Error):
import bisect

  def calc():
  n,k=map(int,input().split())
  arr=list(map(int,input().split()))
  arr=sorted(arr)
  nc=0
  zc=0
  pc=0
  for i in range(n):
    if arr[i]<0:
      nc+=1
    elif arr[i]==0:
      zc+=1
    else:
      pc+=1
  ncc=nc*pc
  zcc=zc*(nc+pc)+(zc*(zc-1))//2
  pcc=(nc*(nc-1))//2+(pc*(pc-1))//2
  if k<=ncc:
    arr1=[]
    arr2=[]
    for i in range(n):
      if arr[i]<0:
        arr1.append(-arr[i])
      elif arr[i]>0:
        arr2.append(arr[i])
    arr1=arr1[::-1]
    c1=len(arr1)
    c2=len(arr2)
    k=ncc-k
    l=0
    r=10**18+1
    while r-l!=1:
      mid=(l+r)//2
      cnt=0
      for i in range(c1):
        cnt+=bisect.bisect_right(arr2,mid//arr1[i])
      if cnt>k:
        r=mid
      else:
        l=mid
    print(-r)
  elif k<=(ncc+zcc):
    print(0)
  else:
    arr1=[]
    arr2=[]
    for i in range(n):
      if arr[i]<0:
        arr1.append(-arr[i])
      elif arr[i]>0:
        arr2.append(arr[i])
    arr1=arr1[::-1]
    c1=len(arr1)
    c2=len(arr2)
    l=0
    r=10**18+1
    k-=(ncc+zcc)
    while r-l!=1:
      mid=(l+r)//2
      cnt1=0
      for i in range(c1):
        tmp=bisect.bisect_right(arr1,mid//arr1[i])
        if arr1[i]**2<mid:
          tmp-=1
        cnt1+=tmp
      cnt1=cnt1//2
      cnt2=0
      for i in range(c2):
        tmp=bisect.bisect_right(arr2,mid//arr2[i])
        if arr2[i]**2<mid:
          tmp-=1
        cnt2+=tmp
      cnt2=cnt2//2
      cnt=cnt1+cnt2
      if cnt>=k:
        r=mid
      else:
        l=mid
    print(r)
calc()