TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
import sys 
stdin = sys.stdin
ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : map(int, stdin.readline().split())

n = ni()
a = list(na())

def gcd(n, m):
  while m:
    n, m = m, n%m
  return n

ans = 1
for e in a:
  ans = gcd(ans, e)
print(ans)

import sys 
stdin = sys.stdin
ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : map(int, stdin.readline().split())

n = ni()
a = list(na())

def gcd(n, m):
  while m:
    n, m = m, n%m
  return n

ans = a[0]
for e in a[1:]:
  ans = gcd(ans, e)
print(ans)


Verdicts: Wrong Answer

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
n = int(input())
a = sorted(list(map(int, input().split())))
while True:
    a = list(set(a))
    tmp = a[:]
    for i in tmp:
        if i == a[0]:
            continue
        elif i % a[0] == 0:
            a.remove(i)
        else:
            a.append(i % a[0])
    a.sort()
    if len(a) == 1:
        break
print(a[0])


n = int(input())
a = sorted(list(map(int, input().split())))
while True:
    a = sorted(list(set(a)))
    tmp = sorted(a[:])
    for i in tmp:
        if i == a[0]:
            continue
        elif i % a[0] == 0:
            a.remove(i)
        else:
            a.append(i % a[0])
            if i % a[0] == 1:
                print(1)
                exit()
            a.remove(i)
    if len(a) == 1:
        break
print(a[0])


Verdicts: Time Limit Exceeded

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
import fraction as math

from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

input()

li = [int(i) for i in input().split(" ")]

print(gcd(*li))

from functools import reduce

def mgcd(a, b):
    while b:
        a, b = b, a % b
    return a

def gcd(*numbers):
    return reduce(mgcd, numbers)

input()

li = [int(i) for i in input().split(" ")]

print(gcd(*li))


Verdicts: Runtime Error

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))

dp = [[max(A) + 1 for _ in range(N)] for _ in range(max(A) + 1)]
for i in range(N):
    dp[0][i] = A[i]
for i in range(max(A)):
    for j in range(N):
        for k in range(N):
            if not j == k:
                if not dp[i][j] - dp[i][k] <= 0:
                    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] - dp[i][k])
result = max(A)
for i in dp:
    result = min(result, min(i))
print(result)


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


def dcg(a, b):
    while not (a == 0 or b == 0):
        if a > b:
            a %= b
        else:
            b %= a
    return a + b


for i in range(N-1):
    A[i+1] = dcg(A[i], A[i+1])
print(A[-1])


Verdicts: Wrong Answer

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
import random

N = int(input())
hps = list(map(int, input().split()))

def is_fighting(hps):
    suverve = 0
    for hp in hps:
        if hp > 0:
            suverve += 1

    if suverve == 1:
        return False
    return True

def can_atack(hp):
    if hp <= 0:
        return False
    return True

while is_fighting(hps):
    a = random.randrange(N)
    b = random.randrange(N)
    while a == b:
        b = random.randrange(N)

    if not can_atack(hps[a]) or not can_atack(hps[b]):
        continue
    hps[b] = hps[b] - hps[a] 
    
for hp in hps:
    if hp > 0:
        print(hp)
        exit()

N = int(input())
hps = sorted(list(map(int, input().split())))

while len(hps) > 1:
    for i in range(1, len(hps)):
        hps[i] = hps[i] % hps[0]
    hps = sorted(set(hps))
    if 0 in hps:
        hps.remove(0)
print(hps[0])

Verdicts: Wrong Answer

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
N =int(input())
A = list(map(int, input().split()))



def gcd(x,y): 
    while y != 0:
        x , y = y , x % y 
    return x






def gcd(A):
    ans = A[0]
    for i in range(1,N) #range(1,N)= 1,2,...,N-1
        ans = gcd(ans, A[i])
    return ans
print(gcd(A))

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



def gcd2(x,y): 
    while y != 0:
        x , y = y , x % y 
    return x






def gcd(A):
    ans = A[0]
    for i in range(1,N): #range(1,N)= 1,2,...,N-1
        ans = gcd2(ans, A[i])
    return ans
print(gcd(A))

Verdicts: Runtime Error

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
n = map(int, input().split())
x = list(map(int, input().split()))
s = math.gcd(x[0],x[1])
for i in x:
    s = math.gcd(s,i)

print(s)

import math
n = int(input())
l = [int(i) for i in input().split()]

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) 
s = gcd(l[0],l[1])
for i in l:
    s = gcd(s,i)
   

print(s)

Verdicts: Runtime Error

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
from functools import reduce

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)

numbers = [int(x) for x in input().split()]
print(reduce(gcd, numbers))

from functools import reduce

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)

n = int(input())
numbers = [int(x) for x in input().split()]
print(reduce(gcd, numbers))

Verdicts: Wrong Answer

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  l=list(set(l))

  if len(l)==1:
    return l[0]

  f=False
  for x in l:
    if x%2==0:
      f=True

  if f:
    return 1
  return 2

print(main())


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

# GCD
def gcd(x,y):
  while y:
    x,y=y,x%y
  return x

def main():
  n=I()
  l=LI()

  a=l[0]
  for x in l[1:]:
    a=gcd(a,x)

  return a

print(main())


Verdicts: Wrong Answer

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
#C
n = int(input())
aa = list(map(int,input().split()))

print(aa)
def mod(xx):
    bb=[]
    for i in range(len(xx)):
        b = xx[i] if (0<xx[i] and xx[i] <= min(xx)) else xx[i] % min(xx)
        if b>0:
            bb.append(b)
    return bb

while True:
    bb = mod(aa)
    # print(bb)
    if min(aa)==min(bb):
        break
    aa=bb
print(min(bb))

n = int(input())
aa = list(map(int,input().split()))

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

ans=aa[0]
for i in range(1,n):
    ans = gcd(ans, aa[i])
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
n=int(input())
l=list(map(int,input().split()))
ans=0
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
if n==1:
    ans=x[0]
else:
    element = gcd(x[0], x[1])
    if n==2:
      ans=element
    else:
        for i in range(2,n):
            element=gcd(element,x[i])
        ans=element
print(ans)

n=int(input())
l=list(map(int,input().split()))
ans=0
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
if n==1:
    ans=l[0]
else:
    element = gcd(l[0], l[1])
    if n==2:
      ans=element
    else:
        for i in range(2,n):
            element=gcd(element,l[i])
        ans=element
print(ans)

Verdicts: Runtime Error

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
N = int(input())
l = [int(i) for i in input().split()]

l = sorted(l)

sema = True
for i in range(len(l)):
    for j in range(i + 1,len(l)):
        if not l[j] % l[i] == 0:
            sema = False
            break
pre = min(l)
if sema == True:
    print(pre)
else:
    m = l[1] % l[0]
    for i in range(2, len(l)):
        a = l[i] % m
        if a < m and a >= 1:
            m = a
        else:
            print(m)

N = int(input())
l = [int(i) for i in input().split()]

l = sorted(l)


pre = min(l)
ans = pre
if all([i % pre == 0 for i in l]):
    print(pre)
else:

    m = l[1] % l[0]
    for i in range(2, len(l)):
        if i ==2:
            m = l[1] % l[0]
        if m != 0:
            a = l[i] % m
            if a < m and a >= 1:
                m = a
            else:
                ans = m
print(ans)


from fractions import gcd
from functools import reduce
input()
def gcd(*numbers):
    return reduce(math.gcd, numbers)
print(gcd(*[int(i) for i in input().split()]))

N = int(input())
l = [int(i) for i in input().split()]

l = sorted(l)


sema = True
mod_list = []
for i in range(len(l)):
    for j in range(i + 1,len(l)):
        if not l[j] % l[i] == 0:
            sema = False
            mod_list.append(l[j] % l[i])
mod_list = set(mod_list)
if sema == True:
    print(min(l))
else:
    ans = min(l)
    print(min([i % m for i in l for m in mod_list if i % m != 0]))


# import math
from functools import reduce
input()
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def gcds(*numbers):
    return reduce(gcd, numbers)
print(gcds(*[int(i) for i in input().split()]))

Verdicts: Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
def main():
    _ = input()
    monsters = [int(n) for n in input().split(" ")]
    max_gcd = reduce(math.gcd, monsters)
    print(max_gcd)

if __name__ == "__main__":
    main()

from functools import reduce

def gcd(a, b):
    x = max(a, b)
    y = min(a, b)
    if x % y == 0:
        return y
    else:
        while x % y != 0:
            z = x % y
            x = y 
            y = z
        else:
            return z

def main():
    _ = input()
    monsters = [int(n) for n in input().split(" ")]
    max_gcd = reduce(gcd, monsters)
    print(max_gcd)

if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
from sys import stdin, exit
from math import factorial
input = stdin.readline
lmi = lambda: list(map(int, input().split()))
mi = lambda: map(int, input().split())
si = lambda: input().strip('\n')
ssi = lambda: input().strip('\n').split()

n = int(input())
arr = lmi()
a = min(arr)
for i in range(n):
    arr[i] %= a
print(min(arr))

from sys import stdin
input = stdin.readline
lmi = lambda: list(map(int, input().split()))
mi = lambda: map(int, input().split())
si = lambda: input().strip('\n')
ssi = lambda: input().strip('\n').split()


def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)


n = int(input())
arr = lmi()
for i in range(1, n):
    arr[i] = gcd(arr[i], arr[i-1])
print(arr[n-1])

Verdicts: Wrong Answer

Accepted
Problem: There are N monsters, numbered 1, 2, ..., N.  Initially, the health of Monster i is A_i.  Below, a monster with at least 1 health is called alive.  Until there is only one alive monster, the following is repeated:    * A random alive monster attacks another random alive monster.   * As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.  Find the minimum possible final health of the last monster alive.

Submission attempts (in order):
n=int(input())
A=list(map(int,input().split()))
def gcd(a,b):
    if b==0:
        return(a)
    else:
        return(gcd(b,b%a))
ans=A[0]
for i in range(n-1):
    ans=gcd(ans,A[i+1])
print(ans)


n=int(input())
A=list(map(int,input().split()))
def gcd(a,b):
    if b>a:
        return(gcd(b,a))
    elif b==0:
        return(a)
    else:
        return(gcd(b,a%b))
ans=A[0]
for i in range(n-1):
    ans=gcd(ans,A[i+1])
print(ans)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,m=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]*(n+1)
ans=[0]*(n+1)
num=[0,2,5,5,4,5,6,3,7,6]
INF=10**18
for i in range(1,n+1):
  stack=[]
  for j in m:
    if i-num[j]>=0:
      stack.append(dp[i-num[j]])
    else:
      stack.append(-INF)
  dp[i]=max(stack)+1
  if dp[i]>0:
    ans[i] += ans[i-num[stack.index(max(stack))]]+num[stack.index(max(stack))]*10**(dp[i]-1)
print(ans[n])

n,m=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]*(n+1)
ans=[0]*(n+1)
num=[0,2,5,5,4,5,6,3,7,6]
INF=10**18
for i in range(1,n+1):
  stack=[]
  for j in a:
    if i-num[j]>=0:
      stack.append(dp[i-num[j]])
    else:
      stack.append(-INF)
  dp[i]=max(stack)+1
  if dp[i]>0:
    ans[i] += ans[i-num[stack.index(max(stack))]]+num[stack.index(max(stack))]*10**(dp[i]-1)
print(ans[n])

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a.reverse()
dp=[0]*(n+1)
ans=[0]*(n+1)
num=[0,2,5,5,4,5,6,3,7,6]
INF=10**18
for i in range(1,n+1):
  stack=[]
  chance=[]
  for j in a:
    if i-num[j]>=0:
      stack.append(dp[i-num[j]])
      chance.append([dp[i-num[j]],ans[i-num[j]]])
    else:
      stack.append(-INF)
      chance.append([-1,-1])
  dp[i]=max(stack)+1
  if dp[i]>0:
    for v in range(len(stack)):
      if stack[v]==max(stack):
        ans[i] += chance[v][1]+a[v]*10**(dp[i]-1)
        break
print(ans[n])

Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N, M = map(int, input().split())
A_list = list(map(int, input().split()))
cost_Dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
A_Dict = {}
for x in A_list:
  A_Dict[x] = cost_Dict[x]
min_digit = min(A_Dict, key=A_Dict.get)
min_cost = min(A_Dict.values())
min_num = min_cost
low_digit_num = (N // min_num)-1
res_matches = N - min_num*low_digit_num
X = list(A_Dict.keys())
X.sort()
X = X[::-1]
for x in X:
  if A_Dict[x] <= res_matches:
    max_digit = x
    break
ans = str(max_digit)
for i in range(low_digit_num):
  ans += str(min_digit)
print(ans)

N, M = map(int, input().split())
A_list = list(map(int, input().split()))
cost_Dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
A_Dict = {}
for x in A_list:
    A_Dict[x] = cost_Dict[x]
#def A_cost(x):
#    return A_Dict[x]
dp = [-float('inf')] * (N+1)
dp[0] = 0

A_list = list(A_Dict.keys())
A_list.sort(reverse=True)

for i in range(N+1):
    _list = []
    for x in A_list:
        if i - A_Dict[x] < 0:
            continue
        else:
            dp[i] = max(dp[i], dp[i-A_Dict[x]]+1)

ans = ""
cost = N
while cost > 0:
    for x in A_list:
        if cost - A_Dict[x] < 0:
            continue
        elif dp[cost - A_Dict[x]] == dp[cost] - 1:
            ans += str(x)
            cost -= A_Dict[x]
            break

print(ans)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n, m = map(int, input().split())
a = list(map(int, input().split()))
use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [[0] * 10 for i in range(n+1)]

def makenum(x,y):
    res = 0
    for i in range(y):
        res = res* 10 + x
    return res
    
for i in range(1,n+1):
    kouho = []
    for j in a:
        if i - use[j] >= 0:
            if dp[i-use[j]] == [0] * 10 and i - use[j] != 0: 
                continue 
            else:
                copy = dp[i-use[j]][:]
                copy[j] += 1
                kouho.append(copy)
    if kouho == []: 
        continue 
    else:
        usearray = []
        usenum = 0
        for array in kouho:
            tmpnum = 0
            for k in range(9,0,-1):
                tmpnum = tmpnum * (10**array[k]) + makenum(k,array[k])
            if tmpnum > usenum:
                usearray = array
                usenum = usenum
        dp[i] = usearray
ans = ""
for i in range(9,0,-1):
    ans += (str(i) * dp[n][i])
print(ans)

n, m = map(int, input().split())
a = list(map(int, input().split()))
use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [[0] * 10 for i in range(n+1)]
for i in range(1,n+1):
    kouho = []
    for j in a:
        if i - use[j] >= 0:
            if dp[i-use[j]] == [0] * 10 and i - use[j] != 0: 
                continue 
            else:
                copy = dp[i-use[j]][:]
                copy[j] += 1
                kouho.append(copy)
    if kouho == []: 
        continue 
    else:
        usearray = []
        accnum = "0"
        for array in kouho:
            tmpnum = ""
            for k in range(9,0,-1):
                tmpnum += (str(k) * array[k])
            if int(tmpnum) > int(accnum):
                usearray = array
                accnum = tmpnum
        dp[i] = usearray
ans = ""
for i in range(9,0,-1):
    ans += (str(i) * dp[n][i])
print(0)

n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [[0] * 10 for i in range(n+1)]
for i in range(1,n+1):
    for j in a:
        if i - use[j] >= 0:
            if dp[i-use[j]] == [0] * 10 and i - use[j] != 0: 
                continue 
            else:
                if sum(dp[i-use[j]])+1 >= sum(dp[i]):
                    copy = dp[i-use[j]][:]
                    copy[j] += 1
                    dp[i] = copy
ans = ""
for i in range(9,0,-1):
    ans += (str(i) * dp[n][i])
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort(reverse=True)
honsuu=[0,2,5,5,4,5,6,3,7,6]

def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
  

ans=""
keta=ketadp(N,M,A)
remain=keta[N]
match=N
#print(A)

while(match>0):
  for i in A:
    
    if(keta[match-honsuu[i]]==remain-1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break;
print(ans)  

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

A=list(map(int,input().split()))
A.sort(reverse=True)
honsuu=[0,2,5,5,4,5,6,3,7,6]

def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
  

ans=""
keta=ketadp(N,M,A)
remain=keta[N]
match=N
#print(keta)

while(match>0):
  for i in A:
    
    if match-honsuu[i]<0:
      continue;
    if(keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break;
print(ans)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
def max_for_str(a, b):
    if len(a) > len(b):
        return a
    elif len(a) < len(b):
        return b
    else:#This case is unlikely to happen.
        for c1, c2 in zip(a, b):
            if c1 > c2:
                return a
            elif c1 < c2:
                return b
        return a
            
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

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

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

for i in range(N+1):
    for A in As:
        if i - match[int(A)] >= 0 and not dp[i - match[int(A)]] is None:
            if dp[i] is None:
                if A >= dp[i - match[int(A)]]:
                    dp[i] = A + dp[i - match[int(A)]]
            else:
                dp[i] = max_for_str(dp[i] , A + dp[i - match[int(A)]])
                
print(dp[N])

def max_for_str(a, b):
    if len(a) > len(b):
        return a
    elif len(a) < len(b):
        return b
    else:#This case is unlikely to happen.
        for c1, c2 in zip(a, b):
            if c1 > c2:
                return a
            elif c1 < c2:
                return b
        return a
            
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

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

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

for i in range(N+1):
    for A in As:
        if i - match[int(A)] >= 0 and not dp[i - match[int(A)]] is None:
            if dp[i] is None:
                dp[i] = A + dp[i - match[int(A)]]
            else:
                dp[i] = max_for_str(dp[i] , A + dp[i - match[int(A)]])

print(dp[N])

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import sys
from operator import itemgetter
from collections import deque
input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [i for i in range(1, 10)]
C = [2, 5, 5, 4, 5, 6, 3, 7, 6]
inf = float('inf')
costs = {m: C[m-1] for m in A}
print(costs)
dp = deque([0])

for i in range(1, N+1):
        dp.append(max([dp[i-c] if i - c >= 0 else -inf for c in costs.values()])+1)

digits = list(costs.keys())
digits.sort(reverse=True)
result = deque()
num = N
for _ in range(dp[N]):
        for i in digits:
                if num >= costs[i] and dp[num-costs[i]] != -inf and dp[num-costs[i]] == dp[num]-1:
                        result.append(str(i))
                        num -= costs[i]
                        break

print(''.join(result))

import sys
from operator import itemgetter
from collections import deque
input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [i for i in range(1, 10)]
C = [2, 5, 5, 4, 5, 6, 3, 7, 6]
inf = float('inf')
costs = {m: C[m-1] for m in A}
dp = deque([0])

for i in range(1, N+1):
        dp.append(max([dp[i-c] if i - c >= 0 else -inf for c in costs.values()])+1)

digits = list(costs.keys())
digits.sort(reverse=True)
result = deque()
num = N
for _ in range(dp[N]):
        for i in digits:
                if num >= costs[i] and dp[num-costs[i]] != -inf and dp[num-costs[i]] == dp[num]-1:
                        result.append(str(i))
                        num -= costs[i]
                        break

print(''.join(result))

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
cost = [None,2,5,5,4,5,6,3,7,6]

Aset = {}
for a in A:
    c = cost[a]
    Aset[c] = max(Aset[c], a) if c in Aset else a
A = sorted([v for k,v in Aset.items()])

dp = [None]*(N+1)
dp[0] = (0,[0]*len(A))

for ia, a in enumerate(A):
    c = cost[a]
    for i in range(c, N+1):
        t = dp[i-c]
        if t and (dp[i]==None or t[0]+1>=dp[i][0]):
            dp[i] = (t[0]+1, t[1].copy())
            dp[i][1][ia] += 1
      	
l=[]
for i in range(len(A)):
    l += [A[len(A)-1-i]]*dp[N][1][len(A)-1-i]

print(*l, sep='')

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
cost = [None,2,5,5,4,5,6,3,7,6]

Aset = {}
for a in A:
    c = cost[a]
    Aset[c] = max(Aset[c], a) if c in Aset else a
A = sorted([v for k,v in Aset.items()])

dp = [None]*(N+1)
dp[0] = (0,[0]*len(A))

for ia, a in enumerate(A):
    c = cost[a]
    for i in range(c, N+1):
        t = dp[i-c]
        if t and (dp[i]==None or t[0]+1>=dp[i][0]):
            dp[i] = (t[0]+1, t[1].copy())
            dp[i][1][ia] += 1
      	
l=[]
for i in range(len(A)):
    l += [A[len(A)-1-i]]*dp[N][1][len(A)-1-i]

print(*l, sep='')

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, M = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
num = [0,2,5,5,4,5,6,3,7,6]

DP = [0] * (N+1)

for i in range(N+1):
    for j in range(M):
        n = num[A[j]]
        if i - n >= 0:
            DP[i] = max(DP[i], DP[i-n]+1)

A.sort(reverse = True)

ans = ""
remain = DP[N]
match = N
while match > 0:
    for i in range(M):
        n = num[A[i]]
        if match - n >= 0 and DP[match - n] == DP[match] - 1:
            ans += str(A[i])
            match -= n
            break
print(ans)

N, M = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
num = [0,2,5,5,4,5,6,3,7,6] 

D = {} 
for i in range(M):
  n = num[A[i]]
  if D.get(n):
    D[n] = max(D[n], A[i])
  else:
    D[n] = A[i]


DP = [(-1, -1)]*(N+1) 
DP[0] = (0, 0)
for i in range(N+1):
  for key in D:
    if i - key >= 0 and DP[i-key] != -1:
      temp = (DP[i-key][0] + 1, max(DP[i-key][1], D[key]))
      DP[i] = max(DP[i], temp)


ans = ''
now = N
while now > 0:
  ans += str(DP[now][1])
  now = now - num[DP[now][1]]
print(ans)


Verdicts: Time Limit Exceeded

Accepted
Problem: 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.

Submission attempts (in order):
n,m = map(int,input().split())
A = list(map(int,input().split()))

su = [0,2,5,5,4,5,6,3,7,6]

dp = [0]*(n+1)

for i in range(1,n+1):
  for j in A:
    if i >= su[j]:
      dp[i] = max(dp[i],dp[i-su[j]]*10+j)
      
print(dp[-1])
  





n,m = map(int,input().split())
A = list(map(int,input().split()))

su = [0,2,5,5,4,5,6,3,7,6]

dp = [-1]*(n+1)

for i in range(1,n+1):
  for j in A:
    if i >= su[j]:
      dp[i] = max(dp[i],dp[i-su[j]]*10+j)
      
print(dp[-1])


n,m = map(int,input().split())
A = list(map(int,input().split()))

su = [0,2,5,5,4,5,6,3,7,6]

dp = [-1]*(n+1)
dp[0] = 0

for i in range(1,n+1):
  for j in A:
    if i >= su[j]:
      dp[i] = max(dp[i],dp[i-su[j]]*10+j)
      
print(dp[-1])

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,m=map(int, input().split())
mt=[0,2,5,5,4,5,6,3,7,6]
a=sorted(map(int, input().split()))
dp=['']*(n+1)
dp[0]='*'
for i in range(n+1):
    if not dp[i]:continue
    for j in a:
        if i+mt[j]<n+1:
            dp[i+mt[j]]=dp[i]+str(j)
print(dp[-1][1:])

n,m=map(int, input().split())
hon=[2,5,5,4,5,6,3,7,6]
a=list(map(int, input().split()))
s=sorted(set([hon[i-1] for i in a]))
x=s[0]*s[1]+1
dp=[[-100]*(x+1) for i in range(len(s)+1)]
for i in range(1,len(s)+1):
    for j in range(x+1):
        if j%s[i-1]==0:
            dp[i][j]=max(dp[i][j],j//s[i-1])
        for k in range(1,j+1):
            if (j-k)%s[i-1]==0:
                dp[i][j]=max(dp[i][j],(j-k)//s[i-1]+dp[i-1][k])
ans=[]
if n>x:
    ans+=[min(s)]*-(-(n-x)//min(s))
    n=n+(-(n-x)//min(s))*min(s)
from itertools import product
for i in product(s,repeat=dp[-1][n]):
    if sum(i)==n:break
ans+=list(i)
import bisect
ansf=[]
for i in ans:
    t=9-hon[::-1].index(i)
    bisect.insort_left(ansf,t)
print(*ansf[::-1],sep='')

n,m=map(int, input().split())
mt=[0,2,5,5,4,5,6,3,7,6]
a=sorted(map(int, input().split()))[::-1]
dp=[-1]*(n+1)
dp[0]=0
for i in range(n+1):
    if dp[i]==-1:continue
    for j in a:
        if i+mt[j]<n+1:
            dp[i+mt[j]]=max(dp[i]*10+j,dp[i+mt[j]])
print(dp[-1])

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, M = map(int, input().split())
A = list(map(int, input().split()))

import sys
INT_MIN = sys.maxsize * -1
match = [-100,2,5,5,4,5,6,3,7,6]
CHOICES = []
for i in A:
    if (i == 6) and (9 in A):
        continue
    if (i == 3) and (5 in A):
        continue
    if (i == 2) and (3 in A or 5 in A):
        continue
    CHOICES.append([i, match[i]])
CHOICES.sort(reverse=True)

ans = [0]
#print(CHOICES)
for i in range(1,N+1):
    ans.append(INT_MIN)
    for c in CHOICES:
        if  i - c[1] == 0:
            ans[i] = max(ans[i], int(str(c[0])))
            break
        elif i - c[1] >= 0 and ans[i - c[1]] != INT_MIN:
            ans[i] = max(ans[i], int(str(c[0]) + str(ans[i - c[1]])))
            break

print(ans[N])




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

def dp(n, i):
    if n == 0:
        return 0
    if n < 0:
        return None
    if i == len(CHOICES):
        return None
    
    ret1 = dp(n - CHOICES[i][1], i)
    if ret1 == None:
        ret1 = 0
    elif ret1 == 0:
        ret1 = int(str(CHOICES[i][0]))
    else:
        ret1 = int(str(CHOICES[i][0]) + str(ret1))

    ret2 = dp(n, i+1)
    if ret2 == None:
        ret2 = 0
    else:
        ret2 = ret2
        
    return max(ret1, ret2)



match = [-100,2,5,5,4,5,6,3,7,6]
CHOICES = []
for i in A:
    if (i == 6) and (9 in A):
        continue
    if (i == 3) and (5 in A):
        continue
    if (i == 2) and (3 in A or 5 in A):
        continue
    CHOICES.append([i, match[i]])

print(CHOICES)
ans = dp(N,0)
print(ans)
    


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

import sys
INT_MIN = sys.maxsize * -1
match = [-100,2,5,5,4,5,6,3,7,6]
CHOICES = []
for i in A:
    if (i == 6) and (9 in A):
        continue
    if (i == 3) and (5 in A):
        continue
    if (i == 2) and (3 in A or 5 in A):
        continue
    CHOICES.append([i, match[i]])
CHOICES.sort(reverse=True)

ans = [0]
#print(CHOICES)
for i in range(1,N+1):
    ans.append(INT_MIN)
    for c in CHOICES:
        
        #    ans[i] = max(ans[i], int(str(c[0])))
        #elif i - c[1] >= 0 and ans[i - c[1]] != INT_MIN:
        #    ans[i] = max(ans[i], int(str(c[0]) + str(ans[i - c[1]])))

        if i - c[1] >= 0 and ans[i - c[1]] != INT_MIN:
            ans[i] = max(ans[i], c[0] + ans[i - c[1]] * 10)

print(ans[N])




Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, M = map(int, input().split())
A = list(map(int, input().split()))
cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
can_cost = sorted([cost[a] for a in A])
D = {}
for a in A:
    c = cost[a]
    D[c] = max(a, D.setdefault(c, -1))

min_cost = can_cost[0]
min_cost_num = D[min_cost]


def f2(v, l, num):
    L = list(str(v))
    min_cost = cost[int(num)]
    num = D.setdefault(min_cost + l, -1)
    if num == -1:
        return -1

    min_cost_num = D[min_cost]
    L[L.index(str(min_cost_num))] = str(num)
    L.sort(reverse=True)
    return int("".join(L))


def f(v, l):
    L = list(str(v))
    set_L = set(L)
    L2 = [f2(v, l, num) for num in set_L]
    return max(L2)

def g(K, r)

    temp = int("".join([str(min_cost_num)] * K))
    dp = [[-1 for _ in range(min(r, K) + 1)] for __ in range(r + 1)]
    dp[0][0] = temp

    for k in range(min(r, K)):
        for n in range(r):
            v = dp[n][k]
            if v == -1:
                continue
            for l in range(1, r - n + 1):
                dp[n + l][k + 1] = max(dp[n + l][k + 1], f(v, l))
    return max(dp[r])

K = N // min_cost
r = N - K * min_cost
res = g(K, r)
if g(K, r) == -1:
    res = g(K - 1, r + min_cost)
print(res)

N, M = map(int, input().split())
A = list(map(int, input().split()))
cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
can_cost = sorted([cost[a] for a in A])
D = {}
for a in A:
    c = cost[a]
    D[c] = max(a, D.setdefault(c, -1))

min_cost = can_cost[0]
min_cost_num = D[min_cost]


def f2(v, l, num):
    L = list(str(v))
    min_cost = cost[int(num)]
    num = D.setdefault(min_cost + l, -1)
    if num == -1:
        return -1

    min_cost_num = D[min_cost]
    L[L.index(str(min_cost_num))] = str(num)
    L.sort(reverse=True)
    return int("".join(L))


def f(v, l):
    L = list(str(v))
    set_L = set(L)
    L2 = [f2(v, l, num) for num in set_L]
    return max(L2)

def g(K, r):
    temp = int("".join([str(min_cost_num)] * K))
    dp = [[-1 for _ in range(min(r, K) + 1)] for __ in range(r + 1)]
    dp[0][0] = temp

    for k in range(min(r, K)):
        for n in range(r):
            v = dp[n][k]
            if v == -1:
                continue
            for l in range(1, r - n + 1):
                dp[n + l][k + 1] = max(dp[n + l][k + 1], f(v, l))
    return max(dp[r])

K = N // min_cost
r = N - K * min_cost
res = g(K, r)
while res == -1:
    K -= 1
    r += min_cost
    res = g(K, r)
print(res)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n,m=map(int, input().split())  
a=list(map(int, input().split()))  
needNum = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

a.sort()
need = list()
for i in range(m):
    need.append(needNum[a[i]])

minNum = a[need.index(min(need))]  
minNeed = min(need) の必要数

digit = n//minNeed
remain = n % minNeed


out = 0
for i in range(digit):
    if i == digit-1:
        if remain != 0:
            for j in range(m):
                if minNeed +remain == needNum[a[-1-j]]:
                    out = a[-1-j]
                    break
        else:
            out = minNum

    elif remain != 0:
        for j in range(m):
            if minNeed+remain >= needNum[a[-1-j]]:
                out = a[-1-j]
                remain -= needNum[a[-1-j]]-minNeed
                break
    else:
        out = minNum

    print( out, end="")

n,m=map(int, input().split())  
A=list(map(int, input().split()))  
num = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

A.sort(reverse=True)    

dp = [-1*float("inf")]*(n+1)    

dp[0] = 0
for a in A:
    if num[a] <= n:
        dp[num[a]] = 1

for i in range(1,n+1):
    for a in A:
        if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

ans = ""
for i in range(dp[n]):  
    for a in A: 
        if n-num[a] >= 0 and dp[n-num[a]] == dp[n]-1:
            ans += str(a)
            n -= num[a]
            break
 
print(ans)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):

N, M = map(int, input().split())
Costs = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
A = sorted(list(map(int, input().split())), reverse=True)  



dp = [-float('inf')] * (N + 1)
dp[0] = 0


for a in A:
    cost = Costs[a]
    for j in range(N + 1):
        if cost > j:
            continue
        dp[j] = max(dp[j], dp[j - cost] + 1)

print('x')

# ans = ""



#         cost = Costs[a]



#             ans += str(a)
#             marker = marker - cost
#             break
# 
# print(ans)


N, M = map(int, input().split())
Costs = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
A = sorted(list(map(int, input().split())), reverse=True)  



dp = [-float('inf')] * (N + 1)
dp[0] = 0


for a in A:
    cost = Costs[a]
    for j in range(N + 1):
        if cost > j:
            continue
        dp[j] = max(dp[j], dp[j - cost] + 1)



ans = ""
marker = N  
while dp[marker]:  
    for a in A:  
        cost = Costs[a]
        
        
        if marker - cost < 0:
            continue
        
        
        
        if dp[marker - cost] == dp[marker] - 1:
            ans += str(a)
            marker = marker - cost
            break

print(ans)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n, m = map(int, input().split())
alist = list(sorted(map(int, input().split())))[::-1]
md = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

dp = [[0, []] for _ in range(n + 1)]

for i in range(2, n+1):
  maxn = 0
  maxt = []
  for a in alist:
    if i - md[a] < 0:
      continue
    if maxn < dp[i-md[a]][0] + 1:
      maxn = dp[i-md[a]][0] + 1
      maxt = dp[i-md[a]][1][:]
      maxt.append(a)
  dp[i] = [maxn, maxt]
print(*sorted(dp[n][1], key=lambda x: -x), sep='')

n, m = map(int, input().split())
alist = list(sorted(map(int, input().split())))[::-1]
md = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

dp = [-float('inf') for _ in range(n+1)]
dp[0] = 0

for i in range(n+1):
  for a in alist:
    if i - md[a] >= 0:
      if dp[i] < dp[i - md[a]] + 1:
        dp[i] = dp[i - md[a]] + 1

ans = ''
res = n
while i > 0:
  for a in alist:
    if i >= md[a] and dp[i]-1 == dp[i-md[a]]:
      ans += str(a)
      i -= md[a]
      break

print(ans)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):

# coding: utf-8

# In[46]:


def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    
    match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    result = [-1 for _ in range(N+1)]
    result[0] = 0
    
    print(result)
    
    for i in range(1, N+1):
        for a in A:
            if i < match[a]:
                print(i,a,result)
                continue
            result[i] = max(result[i], result[i-match[a]]*10+a)
            print(i,a,result)
    
    print(result[N])


# In[47]:


main()




# coding: utf-8

# In[1]:


def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    
    match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    result = [-1 for _ in range(N+1)]
    result[0] = 0
    
    #print(result)
    
    for i in range(1, N+1):
        for a in A:
            if i < match[a]:
                #print(i,a,result)
                continue
            result[i] = max(result[i], result[i-match[a]]*10+a)
            #print(i,a,result)
    
    print(result[N])


# In[2]:


main()



Verdicts: Runtime Error

Accepted