Problem p02774 (python)

Fixed submission:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache
from bisect import bisect


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N,K = inputIL()
A = inputIL()

A.sort()

s = bisect(A,-1)
t = bisect(A,0)

M = A[:s]
Z = A[s:t]
P = A[t:]
M_rev = [i*(-1) for i in M[::-1]]

m = len(M)
z = len(Z)
p = len(P)

n_Z = z*(m+p) + z*(z-1)//2
n_P = p*(p-1)//2 + m*(m-1)//2
n_M = p * m

def count(n):
  ret = 0
  t = 0
  for i in P:
    while t<len(M) and M[t]*i<=n:
      t += 1
    ret += t
  return ret


def count_p(n):
  ret = 0
  t = len(P)-1
  for i in P:
    while t>=0 and P[t]*i>n:
      t -= 1
    ret += t+1
    if i*i <= n:
      ret -= 1
  t = len(M_rev)-1   
  for i in M_rev:
    while t>=0 and M_rev[t]*i>n:
      t -= 1
    ret += t+1
    if i*i <= n:
      ret -= 1
  return ret//2

if n_M < K <= n_M+n_Z:
  print(0)
  exit()
  
if n_M >= K:
  l = -10**18
  r = -1
  while True:
    c = (l+r)//2
    if count(c) < K and count(c+1) >=K:
      break
    if count(c) >= K:
      r = c
    else:
      l = c+1
  print(c+1)
  exit()
  
else:
  K = K-(n_M+n_Z)
  l = 1
  r = 10**18
  while True:
    c = (l+r)//2
    if count_p(c) < K and count_p(c+1) >=K:
      break
    if count_p(c) >= K:
      r = c
    else:
      l = c+1
  print(c+1)
  exit()

Buggy submission (verdict: Runtime Error):
l = list(map(int,input()))


l.reverse()
l.append(0)

l_copy = l.copy()

N = len(l)

pay = []
for i in range(N):
    if l[i] <= 5:
        pay.append(l[i])
    if 10 > l[i] > 5:
        pay.append(0)
        l[i+1] += 1
    if l[i] == 10:
        pay.append(0)
        l[i+1] += 1

pay_copy = pay.copy()

remain = []
for i in range(N):
    k = pay[i] - l_copy[i]
    if  k >= 0:
        remain.append(k)
    else:
        remain.append(k+10)
        pay[i+1] -= 1

result = 0
for i in pay_copy:
    result += i
for i in remain:
    result += i

print(result)