Problem p02912 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
import math
import numpy as np
import heapq
from functools import lru_cache

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

#### input
N, M = map(int, input().split())
A = list(map(lambda x:int(x)*(-1), input().split()))

# sys.stdin = open('/dev/tty')

heapq.heapify(A)

#### functions
def discount(num_coupon):
  max = -heapq.heappop(A) / 2
  heapq.heappush(A, -max)

#### execution
# if em: embed()
for i in range(0, M):
  max = math.floor(-heapq.heappop(A) / 2)
  heapq.heappush(A, -max)

print(-sum(A))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import math
import numpy as np
import heapq
from functools import lru_cache

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

#### input
N, M = map(int, input().split())
A = list(map(lambda x:int(x)*(-1), input().split()))

sys.stdin = open('/dev/tty')

heapq.heapify(A)

#### functions
def discount(num_coupon):
  max = -heapq.heappop(A) / 2
  heapq.heappush(A, -max)

#### execution
# if em: embed()
for i in range(0, M):
  max = math.floor(-heapq.heappop(A) / 2)
  heapq.heappush(A, -max)

print(-sum(A))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import math
import numpy as np
from functools import lru_cache

args = sys.argv
if len(args) == 2:
  import IPython

def embed():
  if len(args) == 2:
    sys.stdin = open('/dev/tty')
    IPython.embed()

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

#### functions
def discount(price_list, num_coupon):
  if num_coupon == 0:
    return price_list
  else:
    if len(price_list) == 1:
      price_list[0] = math.floor(price_list[0]  / 2** num_coupon)
      return price_list
    else:
      sorted_list = sorted(price_list)
      max1st = sorted_list[-1]
      max2nd = sorted_list[-2]
      num_div = min(num_coupon, math.floor(math.log2(max1st / max2nd)) + 1)
      max_i = np.argmax(price_list)
      price_list[max_i] = math.floor(price_list[max_i] / 2**num_div)
      price_list = discount(price_list, num_coupon - num_div)
      return price_list

print(sum(discount(A, M)))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import math
import numpy as np
from functools import lru_cache

args = sys.argv
if len(args) == 2:
  import IPython

def embed():
  if len(args) == 2:
    sys.stdin = open('/dev/tty')
    IPython.embed()

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

#### functions
discount_dic = {}

def discount(price_list, num_coupon):
  if num_coupon == 0:
    return price_list
  else:
    if len(price_list) == 1:
      price_list[0] = math.floor(price_list[0]  / 2** num_coupon)
      return price_list
    else:
      sorted_list = sorted(price_list)
      dic_key = "_".join(map(str, sorted_list))
      if dic_key in discount_dic:
        return discount_dic[dic_key]
      else:
        max1st = sorted_list[-1]
        max2nd = sorted_list[-2]
        num_div = min(num_coupon, math.floor(math.log2(max1st / max2nd)) + 1)
        max_i = np.argmax(price_list)
        price_list[max_i] = math.floor(price_list[max_i] / 2**num_div)
        price_list = discount(price_list, num_coupon - num_div)
        discount_dic[dic_key] = price_list
        return price_list

print(sum(discount(A, M)))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import math
import numpy as np
from functools import lru_cache

args = sys.argv
if len(args) == 2:
  import IPython

def embed():
  if len(args) == 2:
    sys.stdin = open('/dev/tty')
    IPython.embed()

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

#### functions
def discount(price_list, num_coupon):
  if num_coupon == 0:
    return price_list
  else:
    if len(price_list) == 1:
      price_list[0] = math.floor(price_list[0]  / 2** num_coupon)
      return price_list
    else:
      sorted_list = sorted(price_list)
      max1st = sorted_list[-1]
      max2nd = sorted_list[-2]
      num_div = min(num_coupon, math.floor(math.log2(max1st / max2nd)) + 1)
      max_i = np.argmax(price_list)
      price_list[max_i] = math.floor(price_list[max_i] / 2**num_div)
      price_list = discount(price_list, num_coupon - num_div)
      return price_list

print(sum(discount(A, M)))