Problem p03128 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
from itertools import product

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


COST = np.array([2, 5, 5, 4, 5, 6, 3, 7, 6])

N, M = ds_in()
A_list = np.array(ds_in())
A_cost = COST[A_list - 1]  # 0始まりで取得

min_cost = min(A_cost)
base_num = max(A_list[A_cost == min_cost])

if N % min_cost == 0:
    print(int(str(base_num) * (N//min_cost)))
    exit()

# 本数をmin_costのmodを考え、cost最小のものを記憶
mod_cost = {i: [-1, 10**6] for i in range(1, min_cost)}
for a, c in zip(A_list, A_cost):
    mod = c % min_cost
    if mod > 0:
        if c < mod_cost[mod][1]:
            mod_cost[mod][1] = c
            mod_cost[mod][0] = a
        elif c == mod_cost[mod][1]:
            mod_cost[mod][0] = max(a, mod_cost[mod][0])

# 存在しないmodは除く
for i in range(1, min_cost):
    if mod_cost[i][1] > 10:
        del mod_cost[i]

N_mod = N % min_cost
upper = []
for m in mod_cost.keys():
    upper.append([i for i in range(0, min_cost + 1)])


ans = []
for cand in product(*upper):
    if N_mod == sum(map(lambda x, y: x*y, mod_cost.keys(), cand)) % min_cost:
        temp_N = N
        num_list = []
        for x, y in zip(mod_cost.keys(), cand):
            num_list.extend([mod_cost[x][0]] * y)
            temp_N -= mod_cost[x][1] * y
        if temp_N >= 0:
            num_list.extend([base_num] * (temp_N // min_cost))
            num_list.sort()
            ans.append(int(''.join(map(str, num_list[::-1]))))

print(max(ans))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
from itertools import product

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


COST = np.array([2, 5, 5, 4, 5, 6, 3, 7, 6])

N, M = ds_in()
A_list = np.array(ds_in())
A_cost = COST[A_list - 1]  # 0始まりで取得

min_cost = min(A_cost)
base_num = max(A_list[A_cost == min_cost])

if N % min_cost == 0:
    print(int(str(base_num) * (N//min_cost)))
    exit()

# 本数をmin_costのmodを考え、cost最小のものを記憶
mod_cost = {i: [-1, 10**6] for i in range(1, min_cost)}
for a, c in zip(A_list, A_cost):
    mod = c % min_cost
    if mod > 0:
        if c < mod_cost[mod][1]:
            mod_cost[mod][1] = c
            mod_cost[mod][0] = a
        elif c == mod_cost[mod][1]:
            mod_cost[mod][0] = max(a, mod_cost[mod][0])

# 存在しないmodは除く
for i in range(1, min_cost):
    if mod_cost[i][1] == 10**6:
        mod_cost.pop(i)

N_mod = N % min_cost
upper = []
for m in mod_cost.keys():
    upper.append([i for i in range(0, N_mod // m + 1)])


ans = []
for cand in product(*upper):
    if N_mod == sum(map(lambda x, y: x*y, mod_cost.keys(), cand)):
        temp_N = N
        num_list = []
        for x, y in zip(mod_cost.keys(), cand):
            num_list.extend([mod_cost[x][0]] * y)
            temp_N -= mod_cost[x][1] * y
        if temp_N >= 0:
            num_list.extend([base_num] * (temp_N // min_cost))
            num_list.sort()
            ans.append(int(''.join(map(str, num_list[::-1]))))

print(max(ans))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
from itertools import product

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


COST = np.array([2, 5, 5, 4, 5, 6, 3, 7, 6])

N, M = ds_in()
A_list = np.array(ds_in())
A_cost = COST[A_list - 1]  # 0始まりで取得

min_cost = min(A_cost)
base_num = max(A_list[A_cost == min_cost])

if N % min_cost == 0:
    print(int(str(base_num) * (N//min_cost)))
    exit()

# 本数をmin_costのmodを考え、cost最小のものを記憶
mod_cost = {i: [-1, 10**6] for i in range(1, min_cost)}
for a, c in zip(A_list, A_cost):
    mod = c % min_cost
    if mod > 0:
        if c < mod_cost[mod][1]:
            mod_cost[mod][1] = min(c, mod_cost[mod][1])
            mod_cost[mod][0] = a
        elif c == mod_cost[mod][1]:
            mod_cost[mod][0] = max(a, mod_cost[mod][0])

# 存在しないmodは除く
for i in range(1, min_cost):
    if mod_cost[i][1] == 10**6:
        mod_cost.pop(i)

N_mod = N % min_cost
upper = []
for m in mod_cost.keys():
    upper.append([i for i in range(0, N_mod // m + 1)])


ans = []
for cand in product(*upper):
    if N_mod == sum(map(lambda x, y: x*y, mod_cost.keys(), cand)):
        temp_N = N
        num_list = []
        for x, y in zip(mod_cost.keys(), cand):
            num_list.extend([mod_cost[x][0]] * y)
            temp_N -= mod_cost[x][1] * y
        num_list.extend([base_num] * (temp_N // min_cost))
        num_list.sort()
        ans.append(int(''.join(map(str, num_list[::-1]))))

print(max(ans))