Problem:


Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654



Sample input:
20 4
3 7 8 4


Sample output:
777773

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
*a, = map(int, input().split())

hon = [2,5,5,4,5,6,3,7,6]
use = set([])
from collections import defaultdict
d = defaultdict(list)
for i in a:
    use.add(hon[i-1])
    d[hon[i-1]].append(i)
for key in d.keys():
    d[key].sort(reverse=True)
use = list(use)
use.sort()

# DP[i] = i本すべてを使いきれるか
DP = [False]*(n+1)
DP[0] = True
for i in range(n):
    if DP[i]:
        for j in use:
            if i + j < n+1:
                DP[i+j] = True

honsu = n//use[0]
amari = n %use[0]
while not DP[amari]:
    honsu -= 1
    amari += use[0]

def DFS(now, count):
    global amari
    global flag
    if flag:
        return
    if now == amari:
        flag = count
        return
    if now > amari:
        return
    for i in use_2:
        new_now = now + i
        new_count = count.copy()
        new_count[i] += 1
        DFS(new_now, new_count)

ret = [[d[use[0]][0], honsu]]

if amari == 0:
    pass
else:
    use_2 = use[1:]
    flag = None
    default_count = {}
    for i in use_2:
        default_count[i] = 0
    DFS(0,default_count)
    for key, cnt in flag.items():
        ret.append([d[key][0], cnt])
    ret.sort(reverse=True)

ans = ""
for c, cnt in ret:
    ans += str(c)*cnt
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys
sys.setrecursionlimit(10**4+1)

def cat(x, y):
  a = math.floor(math.log10(y))
  return int(x*10**(1+a)+y)

cost = [0,2,5,5,4,5,6,3,7,6]
n,_ = list(map(int, input().split()))
a = list(map(int, input().split()))

#n = 10**4
#a=range(1,10)

cache = [None]*(n+1)

def f(m):
  if m == 0:
    return 0
  if cache[m] is not None:
    return cache[m]

  b = -1
  for i in a:
    if m >= cost[i]:
      r = f(m-cost[i])
      if r == 0:
        b = max(b, i)
      elif r > 0:
        b = max(b, cat(i,r), r*10+i)

  cache[m] = b
  return b

print(max(f(n),0))

Submission status: Wrong Answer

Solution code:
import math
import sys
sys.setrecursionlimit(10**4+1)

def cat(x, y):
  a = math.floor(math.log10(y))
  return int(x*10**(1+a)+y)

cost = [0,2,5,5,4,5,6,3,7,6]
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))

cache = [None]*(n+1)

def f(m):
  if m == 0:
    return 0
  if cache[m] is not None:
    return cache[m]

  b = -1
  for i in a:
    if m >= cost[i]:
      r = f(m-cost[i])
      if r == 0:
        b = max(b, i)
      elif r > 0:
        b = max(b, cat(i,f(m-cost[i])))

  cache[m] = b
  return b

print(max(f(n),0))

Submission status: Wrong Answer

Solution code:
from copy import copy

digit_costs = [2,5,5,4,5,6,3,7,6,]

W, _ = map(int, raw_input().strip().split(' '))

dp = [[] for _ in xrange(W + 1)]

values  = map(int, raw_input().strip().split(' '))
weights = [digit_costs[values[i] - 1] for i in xrange(len(values))]

def is_larger(a, b) : 
    r1, r2 = 0, 0
    for (i, x) in enumerate(a) : 
        r1 += x * 10 ** i

    for (i, x) in enumerate(b) : 
        r2 += x * 10 ** i

    return r1 > r2

for i in xrange(len(values)) : 
    vi = values[i]
    wi = weights[i]
    
    for w in xrange(0, W - wi + 1) : 
        if dp[w] or not w:
            temp = copy(dp[w])
            temp.append(vi)
            if is_larger(temp, dp[w + wi]) : 
                dp[w + wi] = sorted(temp)
        
print ''.join(map(str, sorted(dp[-1], reverse = True)))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A = list(map(int,input().split()))

c = [2,5,5,4,5,6,3,7,6]

dp = [[-1 for n in range(N+1)] for m in range(M+1)]
dp[0][0] = 0
#keta
dp_r = [[-1 for n in range(N+1)] for m in range(M+1)]
#m

for m in range(1,M+1):
    for n in range(N+1):
        #dp[m][n] = max(dp[m-1][n],
        #dp[m][n-c[A[m-1]-1]] + 1 if (n-c[A[m-1]-1])>=0 and dp[m][n-c[A[m-1]-1]] != -1 else -1)
        if n-c[A[m-1]-1] >= 0 and dp[m][n-c[A[m-1]-1]] != -1:
            if dp[m-1][n] < (dp[m][n-c[A[m-1]-1]] + 1):
                dp[m][n] = dp[m][n-c[A[m-1]-1]] + 1
                dp_r[m][n] = 1
            elif dp[m-1][n] == (dp[m][n-c[A[m-1]-1]] + 1):
                dp[m][n] = dp[m][n-c[A[m-1]-1]] + 1
                dp_r[m][n] = 2
            else:
                dp[m][n] = dp[m-1][n]
                dp_r[m][n] = 0
        else:
            dp[m][n] = dp[m-1][n]
            dp_r[m][n] = 0


#import pprint
#pprint.pprint(dp,width=100)
#pprint.pprint(dp_r,width=100)

def trace(dp):
    m = len(dp)-1
    n = len(dp[0])-1
    r=[[]]
    for _ in range(N+M+2):
        if m==0 and n==0:
            return r
        elif dp_r[m][n] == 0:
            m = m-1
        elif dp_r[m][n] == 1:
            r = [r_+[m] for r_ in r]
            n = n-c[A[m-1]-1]
        elif dp_r[m][n] == 2:
            r_1 = trace([dp_[:n+1] for dp_ in dp[:m]])
            r_2 = trace([dp_[:n-c[A[m-1]-1]+1] for dp_ in dp[:m+1]])
            return r_1 + [r_+[m] for r_ in r_2]

tm = trace(dp)
#print(tm)
####   one_trace
#cr = []
#m, n = M, N
#for _ in range(N+M+2):
#    if m == 0 and n == 0:
#        break
#    elif dp_r[m][n] == 0:
#        m = m - 1
#    else:
#        #print(dp_r[m][n])
#        cr += [c[A[dp_r[m][n]-1]-1]]
#        n = n - c[A[dp_r[m][n]-1]-1]
#print(cr)

d = {}
for a in A:
    d[a] = c[a-1]

#print(d)

inv_d = {}
for k,v in d.items():
    if v in inv_d:
        if k > inv_d[v]:
            inv_d[v] = k
    else:
        inv_d[v] = k
#print(inv_d)


#from operator import itemgetter

ans = []
for tm_ in tm:
    ans_ = [inv_d[c[A[m-1]-1]] for m in tm_]
    ans_.sort(reverse=True)
    ans += [ans_]

ans.sort(reverse=True)

for ans_ in ans[0]:
    print(ans_,sep='',end='')
print()

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A = list(map(int,input().split()))

c = [2,5,5,4,5,6,3,7,6]

dp = [[-1 for n in range(N+1)] for m in range(M+1)]
dp[0][0] = 0
#keta
dp_r = [[-1 for n in range(N+1)] for m in range(M+1)]
#m

for m in range(1,M+1):
    for n in range(N+1):
        #dp[m][n] = max(dp[m-1][n],
        #dp[m][n-c[A[m-1]-1]] + 1 if (n-c[A[m-1]-1])>=0 and dp[m][n-c[A[m-1]-1]] != -1 else -1)
        if n-c[A[m-1]-1] >= 0 and dp[m][n-c[A[m-1]-1]] != -1:
            if dp[m-1][n] < (dp[m][n-c[A[m-1]-1]] + 1):
                dp[m][n] = dp[m][n-c[A[m-1]-1]] + 1
                dp_r[m][n] = 1
            elif dp[m-1][n] == (dp[m][n-c[A[m-1]-1]] + 1):
                dp[m][n] = dp[m][n-c[A[m-1]-1]] + 1
                dp_r[m][n] = 2
            else:
                dp[m][n] = dp[m-1][n]
                dp_r[m][n] = 0
        else:
            dp[m][n] = dp[m-1][n]
            dp_r[m][n] = 0


#import pprint
#pprint.pprint(dp,width=100)
#pprint.pprint(dp_r,width=100)

def trace(dp):
    m = len(dp)-1
    n = len(dp[0])-1
    r=[[]]
    for _ in range(N+M+2):
        if m==0 and n==0:
            return r
        elif dp_r[m][n] == 0:
            m = m-1
        elif dp_r[m][n] == 1:
            r = [r_+[m] for r_ in r]
            n = n-c[A[m-1]-1]
        elif dp_r[m][n] == 2:
            r_1 = trace([dp_[:n+1] for dp_ in dp[:m]])
            r_2 = trace([dp_[:n-c[A[m-1]-1]+1] for dp_ in dp[:m+1]])
            return r_1 + [r_+[m] for r_ in r_2]

tm = trace(dp)
#print(tm)
####   one_trace
#cr = []
#m, n = M, N
#for _ in range(N+M+2):
#    if m == 0 and n == 0:
#        break
#    elif dp_r[m][n] == 0:
#        m = m - 1
#    else:
#        #print(dp_r[m][n])
#        cr += [c[A[dp_r[m][n]-1]-1]]
#        n = n - c[A[dp_r[m][n]-1]-1]
#print(cr)

d = {}
for a in A:
    d[a] = c[a-1]

#print(d)

inv_d = {}
for k,v in d.items():
    if v in inv_d:
        if k > inv_d[v]:
            inv_d[v] = k
    else:
        inv_d[v] = k
#print(inv_d)


from operator import itemgetter

ans = []
for tm_ in tm:
    ans_ = [inv_d[c[A[m-1]-1]] for m in tm_]
    ans_.sort(reverse=True)
    ans += [ans_]
ans.sort(reverse=True, key=itemgetter(0))

for ans_ in ans[0]:
    print(ans_,sep='',end='')
print()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools

COSTS = (99, 2, 5, 5, 4, 5, 6, 3, 7, 6)


def gen(base_digit, base_cost, base_length, digits, remain):
  number = [base_digit] * base_length
  remain -= base_cost * base_length

  if remain == 0:
    print(''.join(str(d) for d in number), flush=True)
    return True

  digits = [(d, c - base_cost) for d, c in digits if d != base_digit and c - base_cost <= remain]

  if len(digits) == 0:
    return False

  max_size = min(remain // min(digits, key=lambda x: x[1])[1], len(number))
  numbers = []

  for size in range(1, max_size + 1):
    for idxs in itertools.combinations_with_replacement(range(len(digits)), size):
      if sum(digits[i][1] for i in idxs) != remain:
        continue

      replaces = [digits[i][0] for i in idxs]
      uppers = [d for d in replaces if d > base_digit]
      lowers = [d for d in replaces if d < base_digit]

      num = number[:]

      if len(uppers) > 0:
        num = uppers + num[len(uppers):]

      if len(lowers) > 0:
        num = num[:-len(lowers)] + lowers

      numbers.append(num)

  if len(numbers) == 0:
    return False

  number = max(numbers)

  print(''.join(str(d) for d in number), flush=True)

  return True


def main():
  remain, _ = list(map(int, input().split()))

  digits = {}

  for d in map(int, input().split()):
    if COSTS[d] not in digits or digits[COSTS[d]] < d:
      digits[COSTS[d]] = d

  digits = [(d, c) for c, d in digits.items()]
  base_digit, base_cost = min(digits, key=lambda x: x[1])

  for length in range(remain // base_cost, -1, -1):
    if gen(base_digit, base_cost, length, digits, remain):
      break


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

needs = [0,2,5,5,4,5,6,3,7,6]
need_to_ch = {}
for a in As:
    need = needs[a]
    need_to_ch[need] = str(a)

ans = 0
needs = list(need_to_ch.keys())
def dfs(idx, remain, used):
    global ans
    need = needs[idx]
    if idx == len(needs) - 1:
        if remain % need:
            return
        used += [remain // need]
        s = ''
        for (n, ch), use in zip(need_to_ch.items(), used):
            s += ch * use
        ans = max(ans, int(''.join(sorted(s, reverse=True))))
    else:
        for i in range(0, remain + 1, need):
            dfs(idx + 1, remain - i, used + [i // need])
dfs(0, N, [])
print(ans)

Submission status: Wrong Answer

Solution code:
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)
  
  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()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [i for i in input().split()]

# dp[i] = i本使ってできる最大の数

def mx(a, b):
    if len(a) != len(b):
        if len(a) > len(b):
            return a
        else:
            return b
    return max(a, b)

# xを作るために使うマッチの本数
h = {'1':2, '2':5, '3':5, '4':4, '5':5, '6':6, '7':3, '8':7, '9':6}

dp = [None] * (N + 1)
dp[0] = ''

for i in range(N + 1):
    for a in A:
        if i - h[a] < 0: continue
        try:
            dp[i] = mx(dp[i-h[a]] + a, dp[i])
        except:
            continue

print(dp[N])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
import sys

r = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
#    -  1  2  3  4  5  6  7  8  9

MAXINT = 0xffffffff

def chmax(n, m):
    if n == None:
        return m
    return int("".join(sorted(list(str(n)) + [str(m)], reverse=True)))

def main():
    N, _ = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    A = sorted(A, reverse=True)
    dp = [0] * (N + 9)#  N : int
    dp[0] = None 

    for i in range(0, N+1):
        if dp[i] == 0:
            continue
        for a in A:
            m = chmax(dp[i], a)
            if dp[i+r[a]] < m:
                dp[i+r[a]] = m
        print("%8d" % i, dp[i])


main()

Submission status: Wrong Answer

Solution code:
def f(x):
    k = int(x)
    return c[k], k


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = list(map(int, input().split()))
A = list(map(f, input().split()))
A = sorted(A, key=lambda x: x[0] * 10 - x[1])

# 数字を作成するのに必要なマッチ数のリストを作成
b = []
dic = {}
n = 0
for d in A:
    if n != d[0]:
        b.append(d[0])
        n = d[0]
        dic[n] = d[1]

ans = [0] * b[0]
n = b[0]
while True:
    max_ = 0
    for d in b:
        if n - d == 0:
            if dic[d] > max_:
                max_ = dic[d]
        elif n - d > 0 and ans[n - d] > 0:
            t = dic[d] * (10 ** len(str(ans[n - d]))) + ans[n - d]
            max_ = max(max_, t)

    if n == N:
        print(max_)
        break
    if max_ > 0 and (N - n) % b[0] == 0:
        print(''.join(sorted(list(str(dic[b[0]])*((N - n)//b[0]) + str(max_)), reverse=True)))
        break
    ans.append(max_)
    n += 1

Submission status: Wrong Answer

Solution code:
def f(x):
    n = int(x)
    return c[n], n


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = list(map(int, input().split()))
A = list(map(f, input().split()))
A = sorted(A, key=lambda x: x[0] * 10 - x[1])

# 数字を作成するのに必要なマッチ数のリストを作成
b = []
dic = {}
n = 0
for d in A:
    if n != d[0]:
        b.append(d[0])
        n = d[0]
        dic[n] = d[1]

ans = [0] * b[0]
n = b[0]
while True:
    max_ = 0
    for d in b:
        if n - d == 0:
            if dic[d] > max_:
                max_ = dic[d]
        elif n - d > 0:
            if ans[n - d] > 0 and dic[d] * (10 ** len(str(ans[n - d]))) + ans[n - d] > max_:
                max_ = dic[d] * (10 ** len(str(ans[n - d]))) + ans[n - d]

    if n == N:
        print(max_)
        break
    if max_ > 0 and (N - n) % b[0] == 0:
        print(''.join(sorted(list(str(dic[b[0]])*((N - n)//b[0]) + str(max_)), reverse=True)))
        break
    ans.append(max_)
    n += 1

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

cost = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
cd = {}
for a in A:
    if cost[a] in cd:
        cd[cost[a]] = max(cd[cost[a]], a)
    else:
        cd[cost[a]] = a

dp = [(0, [])]
for i in range(1, N + 1):
    mx, mxl = int(-1e10), []
    for c in cd:
        if i - c >= 0 and mx < dp[i - c][0] + 1 and 0 <= dp[i - c][0]:
            mx, mxl = dp[i - c][0] + 1, dp[i - c][1] + [cd[c]]
    dp.append((mx, mxl))

ans = dp[-1][1]
ans.sort(reverse=True)
print(''.join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

cost = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
cd = {}
for a in A:
    if cost[a] in cd:
        cd[cost[a]] = max(cd[cost[a]], a)
    else:
        cd[cost[a]] = a

dp = [(0, [])]
for i in range(1, N + 1):
    mx, k = 0, 0
    for j, d in enumerate(dp):
        if i - j in cd.keys() and mx < d[0] + 1:
            mx, k = d[0] + 1, j
    if i - k in cd:
        dp.append((mx, dp[k][1] + [cd[i - k]]))
    else:
        dp.append((mx, dp[k][1]))

ans = dp[-1][1]
ans.sort(reverse=True)
print(''.join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
import numpy as np
def num_match(n):
    if n==1:
        return 2
    elif n==2 or n==3 or n==5:
        return 5
    elif n==4:
        return 4
    elif n==6:
        return 6
    elif n==7:
        return 3
    elif n==8:
        return 7
    elif n==9:
        return 6
N,M = map(int, input().split())
A = list(map(int,input().split()))
match = np.array([[a,num_match(a)] for a in A])
sorted_num = match[np.argsort(match[:,0])[::-1]]
sorted_match = match[np.argsort(match[:,1])]
min_num_match = sorted_match[0][1]
max_k = int(N/min_num_match)#一番小さいマッチ数の整数を使える数
rem = N%min_num_match + min_num_match
#rem+match[min_match_index][1]*kがmin以外の組み合わせの合計
ans_exist = False
ans = 0
used_match = []
for k in range(1,max_k+1):#remを更新していく？
    if rem in sorted_match[:,1]:
        ans_exist = True
        used_match.append(rem)
        break
    else:
        for n in sorted_match[:,1]:
            if n < rem and n!=min_num_match :#同じマッチ数なら大きいほうだけ使う
                used_match.append(n)
                rem = rem-n
                break
            elif n > rem:
                max_k = max_k-1
                break
    if ans_exist or k==max_k:
        break
    else:
        rem = rem + min_num_match
used_match.append(min_num_match)
used_num = []
for um in used_match:
    for i in range(sorted_num.shape[0]):
        if um == sorted_num[i][1]:
            used_num.append(sorted_num[i][0])
            break
base_num=used_num.pop()
sorted_used_num = sorted(used_num,reverse=True)
upper  =[]
lower = []
for s in sorted_used_num:
    if s > base_num:
        upper.append(s)
    else:
        lower.append(s)
lower = sorted(lower)
ans_list = [base_num]*max_k
if len(upper) > 0:
    for i in range(len(upper)):
        ans_list[i] = upper[i]
if len(lower) > 0:
    for j in range(len(lower)):
        ans_list[len(ans_list)-1-j] = lower[j]
ans = ""
for a in ans_list:
    ans = ans + str(a)
ans = int(ans)
if not ans_exist:
    print("no ans")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def num_match(n):
    if n==1:
        return 2
    elif n==2 or n==3 or n==5:
        return 5
    elif n==4:
        return 4
    elif n==6:
        return 6
    elif n==7:
        return 3
    elif n==8:
        return 7
    elif n==9:
        return 6
N,M = map(int, input().split())
A = list(map(int,input().split()))
match = np.array([[a,num_match(a)] for a in A])
sorted_num = match[np.argsort(match[:,0])[::-1]]
sorted_match = match[np.argsort(match[:,1])]
min_num_match = sorted_match[0][1]
max_k = int(N/min_num_match)#一番小さいマッチ数の整数を使える数
rem = N%min_num_match + min_num_match
#rem+match[min_match_index][1]*kがmin以外の組み合わせの合計
ans_exist = False
ans = 0
used_match = []
for k in range(max_k+1):#remを更新していく？
    if rem in sorted_match[:,1]:
        ans_exist = True
        used_match.append(rem)
        break
    else:
        for n in sorted_match[:,1]:
            if n < rem and n!=min_num_match :#同じマッチ数なら大きいほうだけ使う
                used_match.append(n)
                rem = rem-n
                break
            elif n > rem:
                max_k = max_k-1
                break
    if ans_exist or k==max_k:
        break
    else:
        rem = rem + min_num_match
used_match.append(min_num_match)
used_num = []
for um in used_match:
    for i in range(sorted_num.shape[0]):
        if um == sorted_num[i][1]:
            used_num.append(sorted_num[i][0])
            break
base_num=used_num.pop()
sorted_used_num = sorted(used_num,reverse=True)
upper  =[]
lower = []
for s in sorted_used_num:
    if s > base_num:
        upper.append(s)
    else:
        lower.append(s)
lower = sorted(lower)
ans_list = [base_num]*max_k
if len(upper) > 0:
    for i in range(len(upper)):
        ans_list[i] = upper[i]
if len(lower) > 0:
    for j in range(len(lower)):
        ans_list[len(ans_list)-1-j] = lower[j]
ans = ""
for a in ans_list:
    ans = ans + str(a)
ans = int(ans)
if not ans_exist:
    print("no ans")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def num_match(n):
    if n==1:
        return 2
    elif n==2 or n==3 or n==5:
        return 5
    elif n==4:
        return 4
    elif n==6:
        return 6
    elif n==7:
        return 3
    elif n==8:
        return 7
    elif n==9:
        return 6
N,M = map(int, input().split())
A = list(map(int,input().split()))
match = np.array([[a,num_match(a)] for a in A])
sorted_num = match[np.argsort(match[:,0])[::-1]]
sorted_match = match[np.argsort(match[:,1])]
min_num_match = sorted_match[0][1]
max_k = int(N/min_num_match)#一番小さいマッチ数の整数を使える数
rem = N%min_num_match + min_num_match
#rem+match[min_match_index][1]*kがmin以外の組み合わせの合計
ans_exist = False
ans = 0
used_match = []
for k in range(max_k+1):#remを更新していく？
    if rem in sorted_match[:,1]:
        ans_exist = True
        used_match.append(rem)
        break
    else:
        for n in sorted_match[:,1]:
            if n < rem and n!=min_num_match :#同じマッチ数なら大きいほうだけ使う
                used_match.append(n)
                rem = rem-n
                break
            elif n > rem:
                max_k = max_k-1
                break
    if ans_exist:
        max_k = max_k-k
        break
    elif k==max_k:
        break
    else:
        rem = rem + min_num_match
used_match.append(min_num_match)
used_num = []
for um in used_match:
    for i in range(sorted_num.shape[0]):
        if um == sorted_num[i][1]:
            used_num.append(sorted_num[i][0])
            break
base_num=used_num.pop()
sorted_used_num = sorted(used_num,reverse=True)
upper  =[]
lower = []
for s in sorted_used_num:
    if s > base_num:
        upper.append(s)
    else:
        lower.append(s)
lower = sorted(lower)
ans_list = [base_num]*max_k
if len(upper) > 0:
    for i in range(len(upper)):
        ans_list[i] = upper[i]
if len(lower) > 0:
    for j in range(len(lower)):
        ans_list[len(ans_list)-1-j] = lower[j]
ans = ""
for a in ans_list:
    ans = ans + str(a)
ans = int(ans)
if not ans_exist:
    print("no ans")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def num_match(n):
    if n==1:
        return 2
    elif n==2 or n==3 or n==5:
        return 5
    elif n==4:
        return 4
    elif n==6:
        return 6
    elif n==7:
        return 3
    elif n==8:
        return 7
    elif n==9:
        return 6
N,M = map(int, input().split())
A = list(map(int,input().split()))
match = np.array([[a,num_match(a)] for a in A])
sorted_num = match[np.argsort(match[:,0])[::-1]]
sorted_match = match[np.argsort(match[:,1])]
min_num_match = sorted_match[0][1]
max_k = int(N/min_num_match)#一番小さいマッチ数の整数を使える数
rem = N%min_num_match + min_num_match
#rem+match[min_match_index][1]*kがmin以外の組み合わせの合計
ans_exist = False
ans = 0
used_match = []
for k in range(max_k):#remを更新していく？
    if rem in sorted_match[:,1]:
        ans_exist = True
        used_match.append(rem)
        break
    else:
        for n in sorted_match[:,1]:
            if n < rem and n!=min_num_match :#同じマッチ数なら大きいほうだけ使う
                used_match.append(n)
                rem = rem-n
                break
            elif n > rem:
                max_k = max_k-1
                break
    if ans_exist:
        max_k = max_k-k
        break
    else:
        rem = rem + min_num_match
 
used_match.append(min_num_match)
used_num = []
for um in used_match:
    for i in range(sorted_num.shape[0]):
        if um == sorted_num[i][1]:
            used_num.append(sorted_num[i][0])
            break
base_num=used_num.pop()
sorted_used_num = sorted(used_num,reverse=True)
upper  =[]
lower = []
for s in sorted_used_num:
    if s > base_num:
        upper.append(s)
    else:
        lower.append(s)
lower = sorted(lower)
ans_list = [base_num]*max_k
if len(upper) > 0:
    for i in range(len(upper)):
        ans_list[i] = upper[i]
if len(lower) > 0:
    for j in range(len(lower)):
        ans_list[len(ans_list)-1-j] = lower[j]
ans = ""
for a in ans_list:
    ans = ans + str(a)
ans = int(ans)
if not ans_exist:
    print("no ans")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def num_match(n):
    if n==1:
        return 2
    elif n==2 or n==3 or n==5:
        return 5
    elif n==4:
        return 4
    elif n==6:
        return 6
    elif n==7:
        return 3
    elif n==8:
        return 7
    elif n==9:
        return 6
N,M = map(int, input().split())
A = list(map(int,input().split()))
match = np.array([[a,num_match(a)] for a in A])
sorted_num = match[np.argsort(match[:,0])[::-1]]
sorted_match = match[np.argsort(match[:,1])]
min_num_match = sorted_match[0][1]
max_k = int(N/min_num_match)#一番小さいマッチ数の整数を使える数
rem = N%min_num_match + min_num_match
#rem+match[min_match_index][1]*kがmin以外の組み合わせの合計
ans_exist = False
ans = 0
used_match = []
for k in range(max_k):#remを更新していく？
    if rem in sorted_match[:,1]:
        ans_exist = True
        used_match.append(rem)
        break
    else:
        for n in sorted_match[:,1]:
            if n < rem and n!=min_num_match :#同じマッチ数なら大きいほうだけ使う
                used_match.append(n)
                rem = rem-n
                break
            elif n > rem:
                max_k = max_k-k
                break
    if ans_exist:
        max_k = max_k-k
        break
    else:
        rem = rem + min_num_match

used_match.append(min_num_match)
used_num = []
for um in used_match:
    for i in range(sorted_num.shape[0]):
        if um == sorted_num[i][1]:
            used_num.append(sorted_num[i][0])
            break
base_num=used_num.pop()
sorted_used_num = sorted(used_num,reverse=True)
upper  =[]
lower = []
for s in sorted_used_num:
    if s > base_num:
        upper.append(s)
    else:
        lower.append(s)
lower = sorted(lower)
ans_list = [base_num]*max_k
if len(upper) > 0:
    for i in range(len(upper)):
        ans_list[i] = upper[i]
if len(lower) > 0:
    for j in range(len(lower)):
        ans_list[len(ans_list)-1-j] = lower[j]
ans = ""
for a in ans_list:
    ans = ans + str(a)
ans = int(ans)
if not ans_exist:
    print("no ans")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def num_match(n):
    if n==1:
        return 2
    elif n==2 or n==3 or n==5:
        return 5
    elif n==4:
        return 4
    elif n==6:
        return 6
    elif n==7:
        return 3
    elif n==8:
        return 7
    elif n==9:
        return 6
N,M = map(int, input().split())
A = list(map(int,input().split()))
match = np.array([[a,num_match(a)] for a in A])
sorted_num = match[np.argsort(match[:,0])[::-1]]
sorted_match = match[np.argsort(match[:,1])]
min_num_match = sorted_match[0][1]
max_k = int(N/min_num_match)#一番小さいマッチ数の整数を使える数
rem = N%min_num_match + min_num_match
#rem+match[min_match_index][1]*kがmin以外の組み合わせの合計
ans_exist = False
ans = 0
used_match = []
for k in range(max_k):#remを更新していく？
    if rem in sorted_match[:,1]:
        ans_exist = True
        used_match.append(rem)
        break
    else:
        for n in sorted_match[:,1]:
            if n < rem and n!=min_num_match :#同じマッチ数なら大きいほうだけ使う
                used_match.append(n)
                rem = rem-n
                break
    if ans_exist:
        max_k = max_k-k
        break
    else:
        rem = rem + min_num_match

used_match.append(min_num_match)
used_num = []
for um in used_match:
    for i in range(sorted_num.shape[0]):
        if um == sorted_num[i][1]:
            used_num.append(sorted_num[i][0])
            break
base_num=used_num.pop()
sorted_used_num = sorted(used_num,reverse=True)
upper  =[]
lower = []
for s in sorted_used_num:
    if s > base_num:
        upper.append(s)
    else:
        lower.append(s)
lower = sorted(lower)
ans_list = [base_num]*max_k
if len(upper) > 0:
    for i in range(len(upper)):
        ans_list[i] = upper[i]
if len(lower) > 0:
    for j in range(len(lower)):
        ans_list[len(ans_list)-1-j] = lower[j]
ans = ""
for a in ans_list:
    ans = ans + str(a)
ans = int(ans)
if not ans_exist:
    print("no ans")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def num_match(n):
    if n==1:
        return 2
    elif n==2 or n==3 or n==5:
        return 5
    elif n==4:
        return 4
    elif n==6:
        return 6
    elif n==7:
        return 3
    elif n==8:
        return 7
    elif n==9:
        return 6
#N,M = map(int, input().split())
#A = list(map(int,input().split()))
N,M=15,3
A=[5,4,6]
match = np.array([[a,num_match(a)] for a in A])
sorted_num = match[np.argsort(match[:,0])[::-1]]
sorted_match = match[np.argsort(match[:,1])]
min_num_match = sorted_match[0][1]
max_k = int(N/min_num_match)#一番小さいマッチ数の整数を使える数
rem = N%min_num_match + min_num_match
#rem+match[min_match_index][1]*kがmin以外の組み合わせの合計
ans_exist = False
ans = 0
used_match = []
for k in range(max_k):#remを更新していく？
    if rem in sorted_match[:,1]:
        ans_exist = True
        used_match.append(rem)
        break
    else:
        for n in sorted_match[:,1]:
            if n < rem and n!=min_num_match :#同じマッチ数なら大きいほうだけ使う
                used_match.append(n)
                rem = rem-n
                break
    if ans_exist:
        max_k = max_k-k
        break
    else:
        rem = rem + min_num_match

used_match.append(min_num_match)
used_num = []
for um in used_match:
    for i in range(sorted_num.shape[0]):
        if um == sorted_num[i][1]:
            used_num.append(sorted_num[i][0])
            break
base_num=used_num.pop()
sorted_used_num = sorted(used_num,reverse=True)
upper  =[]
lower = []
for s in sorted_used_num:
    if s > base_num:
        upper.append(s)
    else:
        lower.append(s)
lower = sorted(lower)
ans_list = [base_num]*max_k
if len(upper) > 0:
    for i in range(len(upper)):
        ans_list[i] = upper[i]
if len(lower) > 0:
    for j in range(len(lower)):
        ans_list[len(ans_list)-1-j] = lower[j]
ans = ""
for a in ans_list:
    ans = ans + str(a)
ans = int(ans)
if not ans_exist:
    print("no ans")
else:
    print(ans)