Problem p03128 (python)

Fixed submission:
def printing(a_start, a_2, n):

    a_3 = []
    mod_list = []
    for i in range(len(a_2)):
        if a_2[i][1] % a_start[1] not in mod_list and a_2[i][1] % a_start[1] != 0:
            a_3.append(a_2[i])
            mod_list.append(a_2[i][1] % a_start[1])
    a_3.sort(key=lambda x: x[1], reverse=True)


    if len(a_3) >= 2:
        if a_start[0] + a_3[-1][0] < n and a_3[0][0] < a_3[1][0] and a_3[1][0] > a_start[0]:
            a_3.pop(0)


    ans = ""
    while n % a_start[1] != 0:
        for i in range(len(a_3)):
            if n % a_start[1] >= a_3[i][1] % a_start[1]:
                n -= a_3[i][1]
                ans += str(a_3[i][0])
                break
            elif n % a_start[1] == 0:
                break
        else:
            n -= a_3[0][1]
            ans += str(a_3[0][0])

    ans = ans + str(a_start[0]) * (n // a_start[1])
    ans_list = sorted([ans[i] for i in range(len(ans))], reverse=True)
    print("".join(ans_list))

def main():
    n, m = [int(i) for i in input().split()]
    a = sorted([int(i) for i in input().split()], reverse=True)
    number = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    a_list = []
    a_2 = []

    for i in range(m):
        if number[a[i]] not in a_list:
            a_2.append([a[i], number[a[i]]])
            a_list.append(number[a[i]])
    a_2.sort(key=lambda x: x[1])
    a_start = a_2.pop(0)
    printing(a_start, a_2, n)

main()

Buggy submission (verdict: Runtime Error):
def printing(a_start, a_2, n):
  a_3 = []
  mod_list = []
  
  for i in range(len(a_2)):
    if (a_2[i][1] % a_start[1] not in mod_list) and (a_2[i][1] % a_start[1] != 0):
      a_3.append(a_2[i])
      mod_list.append(a_2[i][1] % a_start[1])
  
  a_3.sort(key=lambda x: x[1], reverse=True)
  if len(a_3) >= 2:
    if a_start[0] + a_3[-1][0] < n and a_3[0][0] < a3[1][0] and a_3[1][0] > a_start[0]:
      a_3.pop(0)

  ans = ""
  while n % a_start[1] != 0:
    for i in range(len(a_3)):
      if n % a_start[1] >= a_3[i][1] % a_start[1]:
        n -= a_3[i][1]
        ans += str(a_3[i][0])
        break
      elif n % a_start[1] == 0:
        break
    else:
      n -= a_3[0][1]
      ans += str(a_3[0][0])
      
  ans = ans + str(a_start[0]) * (n // a_start[1])
  ans_list = sorted([ans[i] for i in range(len(ans))], reverse=True)
  print(''.join(ans_list))

  
def main():
  N, M = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort(reverse=True)
  num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  a_list = []
  a_2 = []
  
  for i in range(M):
    if num[a[i]] not in a_list:
      a_2.append([a[i], num[a[i]]])
      a_list.append(num[a[i]])

  a_2.sort(key=lambda x: x[1])
  a_start = a_2.pop(0)
  printing(a_start, a_2, N)
      

if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
def printing(a_start, a_2, n):
  a_3 = []
  mod_list = []
  
  for i in range(len(a_2)):
    if (a_2[i][1] % a_start[1] not in mod_list) and (a_2[i][1] % a_start[1] != 0):
      a_3.append(a_2[i])
      mod_list.append(a_2[i][1] % a_start[1])
  
  a_3.sort(key=lambda x: x[1], reverse=True)
  if len(a_3) >= 2:
    if a_start[0] + a_3[-1][0] < n and a_3[0][0] < a3[1][0] and a_3[1][0] > a_start[0]:
      a_3.pop()

  ans = ""
  while n % a_start[1] != 0:
    for i in range(len(a_3)):
      if n % a_start[1] >= a_3[i][1] % a_start[1]:
        n -= a_3[i][1]
        ans += str(a_3[i][0])
        break
      elif n % a_start[1] == 0:
        break
    else:
      n -= a_3[0][1]
      ans += str(a_3[0][0])
      
  ans = ans + str(a_start[0]) * (n // a_start[1])
  ans_list = sorted([ans[i] for i in range(len(ans))], reverse=True)
  print(''.join(ans_list))

  
def main():
  N, M = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort(reverse=True)
  num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  a_list = []
  a_2 = []
  
  for i in range(M):
    if num[a[i]] not in a_list:
      a_2.append([a[i], num[a[i]]])
      a_list.append(num[a[i]])

  a_2.sort(key=lambda x: x[1])
  a_start = a_2.pop(0)
  printing(a_start, a_2, N)
      

if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
def printing(a_start, a_2, n):
  a_3 = []
  mod_list = []
  
  for i in range(len(a_2)):
    if (a_2[i][1] % a_start[1] not in mod_list) and (a_2[i][1] % a_start[1] != 0):
      a_3.append(a_2[i])
      mod_list.append(a_2[i][1] % a_start[1])
  
  a_3.sort(key=lambda x: x[1], reverse=True)
  if len(a_3) >= 2:
    if (a_start[0] + a_3[-1][0] < n) and (a_3[0][0] < a3[1][0]) and (a_3[1][0] > a_start[0]):
      a_3.pop()

  ans = ""
  while n % a_start[1] != 0:
    for i in range(len(a_3)):
      if n % a_start[1] >= a_3[i][1] % a_start[1]:
        n -= a_3[i][1]
        ans += str(a_3[i][0])
        break
      elif n % a_start[1] == 0:
        break
    else:
      n -= a_3[0][1]
      ans += str(a_3[0][0])
      
  ans = ans + str(a_start[0]) * (n // a_start[1])
  ans_list = sorted([ans[i] for i in range(len(ans))], reverse=True)
  print(''.join(ans_list))

  
def main():
  N, M = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort(reverse=True)
  num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  a_list = []
  a_2 = []
  
  for i in range(M):
    if num[a[i]] not in a_list:
      a_2.append([a[i], num[a[i]]])
      a_list.append(num[a[i]])

  a_2.sort(key=lambda x: x[1])
  a_start = a_2.pop(0)
  printing(a_start, a_2, N)
      

if __name__ == '__main__':
  main()