Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
lis = []
for i in range(N):
  A = int(input())
  if A in lis:
    lis.remove(A)
  else:
    lis.append(A)
print(len(lis))

Fixed submission:
N = int(input())
dic = {}
for i in range(N):
  A = int(input())
  if A not in dic:
    dic[A]=1
  else:
    dic[A]^=1
print(sum(dic.values()))
Buggy python submission (verdict: Runtime Error):
from math import gcd
def check():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    if K > max(A):
        return False
    g = A[0]
    for i in range(1,N):
        g = gcd(g,A[i])
    if K%g != 0:
        return False
    return True
if check():
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Fixed submission:
from fractions import gcd
def check():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    if K > max(A):
        return False
    g = A[0]
    for i in range(1,N):
        g = gcd(g,A[i])
    if K%g != 0:
        return False
    return True
if check():
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import groupby, accumulate, product, permutations, combinations
def solve():
  N, K = map(int, input().split())
  L = list(map(int, input().split()))
  ans = 0
  for com in combinations(L,K):
    cnt = 0
    for c in com:
      cnt += c
    ans = max(ans,cnt)
  return ans
print(solve())

Fixed submission:
from itertools import groupby, accumulate, product, permutations, combinations
def solve():
  N, K = map(int, input().split())
  L = list(map(int, input().split()))
  L.sort(reverse=True)
  ans = sum(L[:K])
  return ans
print(solve())
Buggy python submission (verdict: Runtime Error):
#最小全域木問題、プリム法
from heapq import *
def solve():
    N = int(input())
    A = [list(map(int, input().split()))+[i] for i in range(N)]
    edge = [[] for _ in range(N)]
    A.sort()
    for i in range(N-1):
        w = A[i+1][0]-A[i][0]
        edge[A[i][2]].append((w,A[i+1][2]))
        edge[A[i+1][2]].append((w,A[i][2]))
    A.sort(key=lambda x:x[1])
    for i in range(N-1):
        w = A[i+1][1]-A[i][1]
        edge[A[i][2]].append((w,A[i+1][2]))
        edge[A[i+1][2]].append((w,A[i][2]))
    bridges = []
    heapify(bridges)
    used = [False]*N
    used[0] = True
    ans = 0
    for w,v in edge[0]:
        heappush(bridges,(w,v))
    while len(bridges)>0:
        w,v = heappop(bridges)
        if used[v]==False:
            ans += w
            used[v] = True
            for w,u in edge[v]:
                if used[u]==False:
                    heappush(bridges,(w,u))
    return ans
print(solve())

Fixed submission:
def check():
  X, A, B = map(int, input().split())
  if A>=B:
    return 'delicious'
  if B-A>X:
    return 'dangerous'
  return 'safe'
print(check())
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
A = list(map(lambda x:x//400,A))
#print(A)
C = [0]*9
for a in A:
  if a>=8:
    C[8] +=1
  elif C[a]==0:
    C[a] = 1
#print(C[:-1])
mi = sum(C[:-1])
if mi == 0:
  mini = min(1,C[-1])
ma = min(8,mi+C[-1])
print(mini,ma)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
A = list(map(lambda x:x//400,A))
#print(A)
C = [0]*9
for a in A:
  if a>=8:
    C[8] +=1
  elif C[a]==0:
    C[a] = 1
#print(C[:-1])
mi = sum(C[:-1])
mini = mi
if mi == 0:
  mini = 1
ma = mi+C[-1]
print(mini,ma)
Buggy python submission (verdict: Runtime Error):
from math import ceil
def solve():
    N, A, B = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    top = max(H)+1
    total = sum(H)
    bottom = 1
    mid = (top+bottom)//2
    while bottom+1<top:
        cnt = 0
        for i in range(N):
            cnt += max(ceil((H[i]-B*mid)/(A-B)),0)
        if cnt>mid:
            bottom = mid+1
        else:
            top = mid+1
        mid = (top+bottom)//2
    ans = mid
    return ans
print(solve())

Fixed submission:
A, B = map(int, input().split())
if A+B>=10:
  print('error')
else:
  print(A+B)
Buggy python submission (verdict: Time Limit Exceeded):
from math import ceil
def solve():
    N, A, B = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    top = max(H)+1
    total = sum(H)
    bottom = 1
    mid = (top+bottom)//2
    while bottom+1<top:
        cnt = 0
        for i in range(N):
            cnt += max(ceil((H[i]-B*mid)/(A-B)),0)
        if cnt>mid:
            bottom = mid+1
        else:
            top = mid+1
        mid = (top+bottom)//2
    ans = mid
    return ans
print(solve())

Fixed submission:
from math import ceil
def solve():
    N, A, B = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    top = max(H)
    total = sum(H)
    bottom = 0
    mid = (top+bottom)//2
    while bottom+1<top:
        cnt = 0
        for i in range(N):
            cnt += max(ceil((H[i]-B*mid)/(A-B)),0)
        if cnt>mid:
            bottom = mid
        else:
            top = mid
        mid = (top+bottom)//2
    ans = top
    return ans
print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
from math import ceil
def solve():
    N, A, B = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    top = max(H)+1
    total = sum(H)
    bottom = 1
    mid = (top+bottom)//2
    while bottom+1<top:
        cnt = 0
        for i in range(N):
            cnt += max(ceil((H[i]-B*mid)/(A-B)),0)
        if cnt>mid:
            bottom = mid+1
        else:
            top = mid+1
        mid = (top+bottom)//2
    ans = mid
    return ans
print(solve())

Fixed submission:
from math import ceil
def solve():
    N, A, B = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    top = max(H)
    total = sum(H)
    bottom = 0
    mid = (top+bottom)//2
    while bottom+1<top:
        cnt = 0
        for i in range(N):
            cnt += max(ceil((H[i]-B*mid)/(A-B)),0)
        if cnt>mid:
            bottom = mid
        else:
            top = mid
        mid = (top+bottom)//2
    ans = top
    return ans
print(solve())
Buggy python submission (verdict: Runtime Error):
from math import gcd
def check():
    A, B, C = map(int, input().split())
    if C%gcd(A,B)==0:
        return 'YES'
    return 'NO'
print(check())

Fixed submission:
A, B, C = map(int, input().split())
for i in range(1,B+1):
  if i*A%B==C:
    print('YES')
    exit()
print('NO')
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    liss = []
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            if A[i]>a:
                prev = 1
            elif A[i]<a:
                prev = -1
            else:
                prev = 0
        elif ((A[i]-a)*prev<0:
            liss.append(lis)
            lis = []
        lis.append(A[i])
        a = A[i]
    liss.append(lis)
    ans = len(liss)
    return ans
print(solve(N,A))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    liss = []
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            if A[i]>a:
                prev = 1
            elif A[i]<a:
                prev = -1
            else:
                prev = 0
        elif ((A[i]>a)+(A[i]>=a)-1)*prev<0:
            liss.append(lis)
            lis = []
        lis.append(A[i])
        a = A[i]
    liss.append(lis)
    ans = len(liss)
    return ans
print(solve(N,A))
Buggy python submission (verdict: Runtime Error):
def solve():
  N, M = map(int, input().split())
  ans = [0]*N
  A = [0]*N
  B = [0]*N
  for i in range(N):
    A[i],B[i] = map(int, input().split())
  X = [0]*M
  Y = [0]*M
  for i in range(M):
    X[i],Y[i] = map(int, input().split())
  for i in range(N):
    ind = -1
    d_min = float('inf')
    for j in range(N):
      d = ((A[i]-X[j])**2+(B[i]-Y[j])**2)**0.5
      if d<d_min:
        d_min = d
        ind = j+1
    ans[i] = ind
  return ans
print(*solve(),sep='\n')

Fixed submission:
def solve():
  N, M = map(int, input().split())
  ans = [0]*N
  A = [0]*N
  B = [0]*N
  for i in range(N):
    A[i],B[i] = map(int, input().split())
  X = [0]*M
  Y = [0]*M
  for i in range(M):
    X[i],Y[i] = map(int, input().split())
  for i in range(N):
    ind = -1
    d_min = float('inf')
    for j in range(M):
      d = abs(A[i]-X[j])+abs(B[i]-Y[j])
      if d<d_min:
        d_min = d
        ind = j+1
    ans[i] = ind
  return ans
print(*solve(),sep='\n')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

ans = 0
if N%2==1:
    for i in range(0,N,2):
        # print(i)
        if i==0:
            if A.count(i)!=1:
                # print('A',A)
                break
        elif A.count(i)!=2:
            # print('B',i)
            break
    else:
        ans = pow(2,(N-1)//2,10**9+7)
else:
    for i in range(1,N,2):
        if A.count(i)!=2:
            # print(i)
            break
    else:
        ans = pow(2,N//2,10**9+7)
print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
from fractions import gcd
from math import ceil
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations
ans = 0
d = Counter(A)
if N%2==1:
    for i in range(0,N,2):
        # print(i)
        if i==0:
            if d[i]!=1:
                # print('A',A)
                break
        elif d[i]!=2:
            # print('B',i)
            break
    else:
        ans = pow(2,(N-1)//2,10**9+7)
else:
    for i in range(1,N,2):
        if d[i]!=2:
            # print(i)
            break
    else:
        ans = pow(2,N//2,10**9+7)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    ans = 0
    mod = 10**9+7
    N = int(input())
    A = list(map(int, input().split()))
    for i in range(N):
        if (N-1-i)==i:
            if A.count(0)!=1:
                return 0
        else: 
            if A.count(abs((N-1-i)-i))!=2:
                return 0
    ans = pow(2,N//2,mod)
    return ans
print(solve())

Fixed submission:
from collections import Counter
def solve():
    ans = 0
    mod = 10**9+7
    N = int(input())
    A = list(map(int, input().split()))
    c = Counter(A)
    for i in range(N):
        if (N-1-i)==i:
            if 0 not in c or c[0]!=1:
                return 0
        else:
            p = abs((N-1-i)-i)
            if p not in c or c[p]!=2:
                return 0
    ans = pow(2,N//2,mod)
    return ans
print(solve())
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def dijkstra_heap(N,s,edge):
    d = [float("inf")] * N #始点sから各頂点への最短距離
    used = [False] * N
    d[s] = 0
    used[s] = True
    edgelist = []
    #edgelist : [sからの暫定(未確定)最短距離,頂点]のリスト
    #edge[s] : sから出る枝の[重み,終点]のリスト
    for v,w in enumerate(edge[s]):
        heapq.heappush(edgelist,(w,v)) #sの隣の点は枝の重さがそのまま暫定最短距離となる
    while len(edgelist):
        #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
        minedge = heapq.heappop(edgelist)
        #minedge : [sからの(確定)最短距離,頂点]
        v = minedge[1]
        if used[v]:
            continue
        d[v] = minedge[0]
        used[v] = True
        for u,w in enumerate(edge[v]):
            if not used[u]:
                heapq.heappush(edgelist,(d[v]+w,u))
    return d


################################
A, B, C, D = map(int, input().split())
N = int(input())

d = [[0]*(N+2) for _ in range(N+2)]

X = [0]*N
Y = [0]*N
R = [0]*N
for i in range(N):
    X[i],Y[i],R[i] = map(int, input().split())
d[0][-1] = ((C-A)**2+(D-B)**2)**0.5
d[-1][0] = ((C-A)**2+(D-B)**2)**0.5
for i in range(1,N+1):
    d_s = max(0,((X[i-1]-A)**2+(Y[i-1]-B)**2)**0.5-R[i-1])
    d_e = max(0,((X[i-1]-C)**2+(Y[i-1]-D)**2)**0.5-R[i-1])
    d[0][i],d[i][0] = d_s,d_s
    d[N+1][i],d[i][N+1] = d_e,d_e
    for j in range(1,N+1):
        d[i][j] = max(((X[i-1]-X[j-1])**2+(Y[i-1]-Y[j-1])**2)**0.5-R[i-1]-R[j-1],0)
s = 0 #始点
d = dijkstra_heap(N+2,s,d)
print(d[-1])

Fixed submission:
import heapq
def dijkstra_heap(N,s,edge):
    d = [float("inf")] * N #始点sから各頂点への最短距離
    used = [False] * N
    d[s] = 0
    used[s] = True
    edgelist = []
    #edgelist : [sからの暫定(未確定)最短距離,頂点]のリスト
    #edge[s] : sから出る枝の[重み,終点]のリスト
    for v,w in enumerate(edge[s]):
        heapq.heappush(edgelist,(w,v)) #sの隣の点は枝の重さがそのまま暫定最短距離となる
    while len(edgelist):
        #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
        minedge = heapq.heappop(edgelist)
        #minedge : [sからの(確定)最短距離,頂点]
        v = minedge[1]
        if used[v]:
            continue
        d[v] = minedge[0]
        used[v] = True
        for u,w in enumerate(edge[v]):
            if not used[u]:
                heapq.heappush(edgelist,(d[v]+w,u))
    return d


################################
A, B, C, D = map(int, input().split())
N = int(input())

d = [[0]*(N+2) for _ in range(N+2)]

X = [0]*N
Y = [0]*N
R = [0]*N
for i in range(N):
    X[i],Y[i],R[i] = map(int, input().split())
d[0][-1] = ((C-A)**2+(D-B)**2)**0.5
d[-1][0] = ((C-A)**2+(D-B)**2)**0.5
for i in range(1,N+1):
    d_s = max(0,((X[i-1]-A)**2+(Y[i-1]-B)**2)**0.5-R[i-1])
    d_e = max(0,((X[i-1]-C)**2+(Y[i-1]-D)**2)**0.5-R[i-1])
    d[0][i],d[i][0] = d_s,d_s
    d[N+1][i],d[i][N+1] = d_e,d_e
    for j in range(1,N+1):
        d[i][j] = max(((X[i-1]-X[j-1])**2+(Y[i-1]-Y[j-1])**2)**0.5-R[i-1]-R[j-1],0)
s = 0 #始点
d = dijkstra_heap(N+2,s,d)
print(d[-1])
Buggy python submission (verdict: Runtime Error):
def solve():
  ans = 0
  N, A = map(int, input().split())
  X = list(map(int, input().split()))
  x = sum(X)
  dp = [[[0]*(sum(X)+50) for _ in range(N+1)] for _ in range(N+1)]
  dp[0][0][0] = 1
  for i in range(N):
    for j in range(i+1):
      for k in range(x):
        dp[i+1][j][k] += dp[i][j][k]
        dp[i+1][j+1][k+X[i]] += dp[i][j][k]
  for j in range(1,N+1):
    ans += dp[-1][j][j*A]
  return ans
print(solve())

Fixed submission:
def solve():
  ans = 0
  N, A = map(int, input().split())
  X = list(map(int, input().split()))
  x = sum(X)
  dp = [[[0]*2550 for _ in range(N+1)] for _ in range(N+1)]
  dp[0][0][0] = 1
  for i in range(N):
    for j in range(i+1):
      for k in range(x):
        dp[i+1][j][k] += dp[i][j][k]
        dp[i+1][j+1][k+X[i]] += dp[i][j][k]
  for j in range(1,N+1):
    ans += dp[-1][j][j*A]
  return ans
print(solve())
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd

def solve():
    n = int(input())
    a = list(map(int,input().split()))
    x = 1
    for ai in a:
        x = (x * ai) // gcd(x, ai)
    ans = sum([x // ai for ai in a])
    print(ans % 1000000007)

if __name__ == "__main__":
    solve()


Fixed submission:
from fractions import gcd

def solve():
    n = int(input())
    a = list(map(int,input().split()))
    x = 1
    for ai in a:
        x = (x * ai) // gcd(x, ai)
    ans = sum([x // ai for ai in a])
    print(ans % 1000000007)

if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N = map(int,input().split())
ans = 0
for r in range(N // R + 1):
    for g in range(N // G + 1):
        rest = N - r * R - g * G
        if rest >=0 and rest % B == 0:
            ans += 1
print(ans)


Fixed submission:
def solve():
    R,G,B,N = map(int,input().split())
    ans = 0
    for r in range(N // R + 1):
        for g in range(N // G + 1):
            rest = N - r * R - g * G
            if rest >=0 and rest % B == 0:
                ans += 1
                #print(r,g,rest//B)
    return ans

print(solve())
Buggy python submission (verdict: Runtime Error):
S=list(input())
red=len(list(filter(lambda e:e=='0', S)))
blue=len(list(filter(lambda e:e=='1', S)))
d=abs(red-bule)
print(red+bule-d)


Fixed submission:
S=list(input())
red=len(list(filter(lambda e:e=='0', S)))
blue=len(list(filter(lambda e:e=='1', S)))
d=abs(red-blue)
print(red+blue-d)
Buggy python submission (verdict: Time Limit Exceeded):
inf = 1 << 60
A,B,Q = map(int,input().split())
s = []
t = []
x = []
s.append(-inf*2)
s.append(-inf)
t.append(-inf*2)
t.append(-inf)
for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    x.append(int(input()))
s.append(inf)
s.append(inf*2)
t.append(inf)
t.append(inf*2)

def search(lst, target):
    min1 = 0
    max1 = len(lst)
    while max1 - min1 > 1:
        mid1 = (min1 + max1) // 2
        if lst[mid1] <= target:
            min1 = mid1
        else:
            max1 = mid1
    return min1

def solve(x):
    chk = [0] * 8
    s1 = search(s, x)

    chk[0] = abs(s[s1] - x)
    chk[1] = abs(s[s1] - x)
    chk[2] = abs(s[s1+1] - x)
    chk[3] = abs(s[s1+1] - x)

    t1 = search(t, s[s1])
    t2 = search(t, s[s1+1])
    chk[0] += abs(t[t1] - s[s1])
    chk[1] += abs(t[t1+1] - s[s1])
    chk[2] += abs(t[t2] - s[s1+1])
    chk[3] += abs(t[t2+1] - s[s1+1])

    t1 = search(t, x)

    chk[4] = abs(t[t1] - x)
    chk[5] = abs(t[t1] - x)
    chk[6] = abs(t[t1+1] - x)
    chk[7] = abs(t[t1+1] - x)

    s1 = search(s, t[t1])
    s2 = search(s, t[t1+1])
    chk[4] += abs(s[s1] - t[t1])
    chk[5] += abs(s[s1+1] - t[t1])
    chk[6] += abs(s[s2] - t[t1+1])
    chk[7] += abs(s[s2+1] - t[t1+1])

    return min(chk)

for xi in x:
    ans = solve(xi)
    print(ans)


Fixed submission:
inf = 1 << 60
A,B,Q = map(int,input().split())
s = []
t = []
x = []
s.append(-inf*2)
s.append(-inf)
t.append(-inf*2)
t.append(-inf)
for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    x.append(int(input()))
s.append(inf)
s.append(inf*2)
t.append(inf)
t.append(inf*2)

def search(lst, target):
    min1 = 0
    max1 = len(lst)
    while max1 - min1 > 1:
        mid1 = (min1 + max1) // 2
        if lst[mid1] <= target:
            min1 = mid1
        else:
            max1 = mid1
    return min1

def solve(x):
    chk = [0] * 8
    s1 = search(s, x)

    chk[0] = abs(s[s1] - x)
    chk[1] = abs(s[s1] - x)
    chk[2] = abs(s[s1+1] - x)
    chk[3] = abs(s[s1+1] - x)

    t1 = search(t, s[s1])
    t2 = search(t, s[s1+1])
    chk[0] += abs(t[t1] - s[s1])
    chk[1] += abs(t[t1+1] - s[s1])
    chk[2] += abs(t[t2] - s[s1+1])
    chk[3] += abs(t[t2+1] - s[s1+1])

    t1 = search(t, x)

    chk[4] = abs(t[t1] - x)
    chk[5] = abs(t[t1] - x)
    chk[6] = abs(t[t1+1] - x)
    chk[7] = abs(t[t1+1] - x)

    s1 = search(s, t[t1])
    s2 = search(s, t[t1+1])
    chk[4] += abs(s[s1] - t[t1])
    chk[5] += abs(s[s1+1] - t[t1])
    chk[6] += abs(s[s2] - t[t1+1])
    chk[7] += abs(s[s2+1] - t[t1+1])

    return min(chk)

for xi in x:
    ans = solve(xi)
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import copy

def main():
    N = int(input())
    A = set(list(map(int, input().split())))

    com0 = None
    while True:
        com = list(itertools.combinations(A,2))
        if com0 == com:
            break
        com0 = copy.copy(com)
        for q in com:
            if q[0] != 0 and q[1] != 0:
                if q[0] > q[1]:
                    a1 = q[1]
                    a2 = q[0]
                else:
                    a1 = q[0]
                    a2 = q[1]
                a = a2 % a1
                if a == 1:
                    print(1)
                    return
                elif a == 0:  # a2はa1の倍数
                    if a2 in A:
                        A.remove(a2)
                else:
                    A.add(a)
        #print(A)
    print(min(A))

main()


Fixed submission:
def gcm(a,b):
    while b != 0:
        a, b = b, a % b
    return a

def gcm_list(a):
    ans = a[0]
    l = len(a)
    for i in range(1, l):
        ans = gcm(ans, a[i])
    return ans

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

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import copy

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

com0 = None
while True:
    com = list(itertools.combinations(A,2))
    if com0 == com:
        break
    com0 = copy.copy(com)
    for q in com:
        if q[0] != 0 and q[1] != 0:
            a = 0
            if q[0] > q[1]:
                a = q[0] % q[1]
            else:
                a = q[1] % q[0]
            if a != 0:
                A.add(a)
print(min(A))


Fixed submission:
def gcm(a,b):
    while b != 0:
        a, b = b, a % b
    return a

def gcm_list(a):
    ans = a[0]
    l = len(a)
    for i in range(1, l):
        ans = gcm(ans, a[i])
    return ans

N = int(input())
A = list(map(int, input().split()))
print(gcm_list(A))
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    #A.insert(0)
    use = [0,2,5,5,4,5,6,3,7,6]

    # dp[i][j] = 残りj本でできる最大の数
    dp = []
    for i in range(N//2+11):
        dp.append([-1 for j in range(N+1)])
    dp[0][N] = 0

    for i in range(1,N//2+11):
        for n in range(0,N+1):
            for a in A:
                if n + use[a] > N:
                    dp[i][n] = max(dp[i][n], dp[i-1][n])
                else:
                    dp[i][n] = max(dp[i][n], dp[i-1][n+use[a]]*10+a)
        #for n in range(0,N+1):
        #    print('%8d' % dp[i][n], end='')
        #print()
        if dp[i] == dp[i-1]:
            print(dp[i][0])
            return

main()


Fixed submission:
inf = 1 << 60
N,M = map(int, input().split())
A = list(map(int, input().split()))
#A.insert(0)
use = [0,2,5,5,4,5,6,3,7,6]

# dp[i] = i本でできる最大の数
dp = [-inf for i in range(N+1)]
dp[0] = 0
for i in range(N+1):
    for a in A:
        if i < use[a]:
            continue
        dp[i] = max(dp[i], dp[i-use[a]] * 10 + a)
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    #A.insert(0)
    use = [0,2,5,5,4,5,6,3,7,6]

    # dp[i][j] = 残りj本でできる最大の数
    dp = []
    for i in range(N//2+11):
        dp.append([-1 for j in range(N+1)])
    dp[0][N] = 0

    for i in range(1,N//2+11):
        for n in range(0,N+1):
            for a in A:
                if n + use[a] > N:
                    dp[i][n] = max(dp[i][n], dp[i-1][n])
                else:
                    dp[i][n] = max(dp[i][n], dp[i-1][n+use[a]]*10+a)
        #for n in range(0,N+1):
        #    print('%8d' % dp[i][n], end='')
        #print()
        if dp[i] == dp[i-1]:
            print(dp[i][0])
            return

main()


Fixed submission:
inf = 1 << 60
N,M = map(int, input().split())
A = list(map(int, input().split()))
#A.insert(0)
use = [0,2,5,5,4,5,6,3,7,6]

# dp[i] = i本でできる最大の数
dp = [-inf for i in range(N+1)]
dp[0] = 0
for i in range(N+1):
    for a in A:
        if i < use[a]:
            continue
        dp[i] = max(dp[i], dp[i-use[a]] * 10 + a)
print(dp[N])