Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max = max(a)

for i in range(n):
    if a[i] == a_max:
        tmp = a.pop(i)
        print(max(a))
        a.insert(i, tmp)
    else:
        print(a_max)


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max_1 = max(a)
b = sorted(a, reverse=True)
a_max_2 = b[1]

for i in range(n):
    if a[i] == a_max_1:
        print(a_max_2)
    else:
        print(a_max_1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

from collections import deque

n = int(input())
a = deque()
for _ in range(n):
    a.append(int(input()))

for _ in range(n):
    tmp = a.popleft()
    print(max(a))
    a.append(tmp)


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max_1 = max(a)
b = sorted(a, reverse=True)
a_max_2 = b[1]

for i in range(n):
    if a[i] == a_max_1:
        print(a_max_2)
    else:
        print(a_max_1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]

for _ in range(n):
    tmp = a.pop(0)
    print(max(a))
    a.append(tmp)


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max_1 = max(a)
b = sorted(a, reverse=True)
a_max_2 = b[1]

for i in range(n):
    if a[i] == a_max_1:
        print(a_max_2)
    else:
        print(a_max_1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for _ in range(n)]

for _ in range(n):
    tmp = a.pop(0)
    print(max(a))
    a.append(tmp)


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max_1 = max(a)
b = sorted(a, reverse=True)
a_max_2 = b[1]

for i in range(n):
    if a[i] == a_max_1:
        print(a_max_2)
    else:
        print(a_max_1)
Buggy python submission (verdict: Time Limit Exceeded):
L, R = map(int, input().split())

ans = []
for i in range(L, R + 1):
    for j in range(i + 1, R + 1):
        v = i * j % 2019
        ans.append(v)
        if v == 0:
            break

print(min(ans))

Fixed submission:
L, R = map(int, input().split())

ans = []
for i in range(L, R + 1):
    for j in range(i + 1, R + 1):
        v = i * j % 2019
        ans.append(v)
        if v == 0:
            break
    else:
        continue
    break
    
print(min(ans))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def dfs(v, p):
        for u in edge_extend[p][v]:
            next_p = 0 if p == 2 else p + 1
            if dist[next_p][u] <= dist[p][v] + 1:
                continue
            else:
                dist[next_p][u] = dist[p][v] + 1
                dfs(u, next_p)

    n, m = map(int, input().split())
    edge = [[] for _ in range(n)]
    for _ in range(m):
        u, v = map(int, input().split())
        edge[u - 1].append(v - 1)
    s, t = map(int, input().split())

    edge_extend = []
    for _ in range(3):
        edge_extend.append(edge)

    dist = [[f_inf] * n for _ in range(3)]
    dist[0][s - 1] = 0
    dfs(s - 1, 0)
    print(dist[0][t - 1] // 3 if dist[0][t - 1] != f_inf else -1)


if __name__ == '__main__':
    resolve()


Fixed submission:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())
    edge = [[] for _ in range(n)]
    for _ in range(m):
        u, v = map(int, input().split())
        edge[u - 1].append(v - 1)
    s, t = map(int, input().split())

    dist = [[f_inf] * 3 for _ in range(n)]
    que = deque([])
    que.append([s - 1, 0])
    dist[s - 1][0] = 0
    while que:
        v, l = que.popleft()
        for u in edge[v]:
            nl = (l + 1) % 3
            if dist[u][nl] == f_inf:
                dist[u][nl] = dist[v][l] + 1
                que.append([u, nl])

    res = dist[t - 1][0]
    print(-1 if res == f_inf else res // 3)


if __name__ == '__main__':
    resolve()
Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 1
    for i in range(n + 1):
        for j in range(m + 1):
            if i - 1 >= 0:
                dp[i][j] += dp[i - 1][j]
            if j - 1 >= 0:
                dp[i][j] += dp[i][j - 1]
            if i - 1 >= 0 and j - 1 >= 0:
                dp[i][j] -= dp[i - 1][j - 1]
                if S[i - 1] == T[j - 1]:
                    dp[i][j] += dp[i - 1][j - 1]
            dp[i][j] %= mod
    print(dp[-1][-1])


if __name__ == '__main__':
    resolve()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 1
    for i in range(n + 1):
        for j in range(m + 1):
            if i - 1 >= 0:
                dp[i][j] += dp[i - 1][j]
            if j - 1 >= 0:
                dp[i][j] += dp[i][j - 1]
            if i - 1 >= 0 and j - 1 >= 0:
                dp[i][j] -= dp[i - 1][j - 1]
                if S[i - 1] == T[j - 1]:
                    dp[i][j] += dp[i - 1][j - 1]
            dp[i][j] %= mod
    print(dp[-1][-1])


if __name__ == '__main__':
    resolve()
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9+7
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if i not in a:
        dp[i] = (dp[i-1] + dp[i-2]) % MOD
print(dp[n])


Fixed submission:
MOD = 10**9+7
n, m = map(int, input().split())
A = [int(input()) for _ in range(m)]

safe = [True] * (n+1)
for a in A:
    safe[a] = False

dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if safe[i]:
        dp[i] = (dp[i-1] + dp[i-2]) % MOD

print(dp[n])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

MOD = 10**9+7
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
dp = [0] * (n+1)
dp[0] = 1
if 1 in a:
    dp[1] = 0

for i in range(1, n+1):
    if i not in a:
        dp[i] = (dp[i-1] + dp[i-2])

print(dp[n] % MOD)


Fixed submission:
MOD = 10**9+7
n, m = map(int, input().split())
A = [int(input()) for _ in range(m)]

safe = [True] * (n+1)
for a in A:
    safe[a] = False

dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if safe[i]:
        dp[i] = (dp[i-1] + dp[i-2]) % MOD

print(dp[n])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

MOD = 10**9+7
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if i not in a:
        if i == 1:
            dp[1] = 1
        else:
            dp[i] = (dp[i-1] + dp[i-2])

print(dp[n] % MOD)


Fixed submission:
MOD = 10**9+7
n, m = map(int, input().split())
A = [int(input()) for _ in range(m)]

safe = [True] * (n+1)
for a in A:
    safe[a] = False

dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if safe[i]:
        dp[i] = (dp[i-1] + dp[i-2]) % MOD

print(dp[n])

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9+7
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if i in a:
        dp[i] = 0
    else:
        if i == 1:
            dp[1] = 1
        else:
            dp[i] = (dp[i-1] + dp[i-2]) % MOD

print(dp[n])


Fixed submission:
MOD = 10**9+7
n, m = map(int, input().split())
A = [int(input()) for _ in range(m)]

safe = [True] * (n+1)
for a in A:
    safe[a] = False

dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if safe[i]:
        dp[i] = (dp[i-1] + dp[i-2]) % MOD

print(dp[n])
Buggy python submission (verdict: Runtime Error):
s = list(input())

i = 0
res = 0
cnt = 0
while i != len(s) - 2:
    if s[i] == "A" and s[i + 1] == "A":
        cnt += 1
        i += 1
    elif s[i] == "A" and s[i + 1] == "B" and s[i + 2] == "C":
        res += cnt + 1
        s[i], s[i + 1], s[i + 2] = "B", "C", "A"
        cnt = 0
        i += 1
    else:
        cnt = 0
        i += 1
print(res)


Fixed submission:
s = input()
s = s.replace('BC', 'D')

ans = a = 0
for i in s:
    if i == "A":
        a += 1
    elif i == "D":
        ans += a
    else:
        a = 0
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
C = []
for _ in range(m):
    b, c = map(int, input().split())
    C.extend([c] * b)
C = sorted(C, reverse=True)

ans = 0
for i in range(n):
    try:
        ans += max(A[i], C[i])
    except IndexError:
        ans += A[i]

print(ans)


Fixed submission:
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = sorted([list(map(int, input().split())) for _ in range(m)], key=lambda x: x[1], reverse=True)

ans = 0
j = 0
for b, c in BC:
    for i in range(b):
        if j < n:
            ans += max(c, A[j])
            j += 1
        else:
            break

if j < n:
    for i in range(n - j):
        ans += A[n - 1 - i]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
C = []
for _ in range(m):
    b, c = map(int, input().split())
    for _ in range(b):
        C.append(c)
C = sorted(C, reverse=True)

ans = 0
for i in range(n):
    try:
        ans += max(A[i], C[i])
    except IndexError:
        ans += A[i]

print(ans)


Fixed submission:
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = sorted([list(map(int, input().split())) for _ in range(m)], key=lambda x: x[1], reverse=True)

ans = 0
j = 0
for b, c in BC:
    for i in range(b):
        if j < n:
            ans += max(c, A[j])
            j += 1
        else:
            break

if j < n:
    for i in range(n - j):
        ans += A[n - 1 - i]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(m)]
heapq.heapify(a)

for i in range(m):
    b, c = query[i][0], query[i][1]
    for j in range(b):
        t = heapq.heappop(a)
        if t < c:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, t)

print(sum(a))


Fixed submission:
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = sorted([list(map(int, input().split())) for _ in range(m)], key=lambda x: x[1], reverse=True)

ans = 0
j = 0
for b, c in BC:
    for i in range(b):
        if j < n:
            ans += max(c, A[j])
            j += 1
        else:
            break

if j < n:
    for i in range(n - j):
        ans += A[n - 1 - i]

print(ans)
Buggy python submission (verdict: Runtime Error):
def dfs(v, p, c):
    for u, w in tree[v]:
        res[v] = c
        if u == p:
            continue
        else:
            if w % 2 == 0:
                dfs(u, v, c)
            else:
                dfs(u, v, c + 1)


n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    tree[u - 1].append([v - 1, w])
    tree[v - 1].append([u - 1, w])

res = [0 for _ in range(n)]
dfs(0, -1, 0)

for i in res:
    print(i)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)


def dfs(v, p, dist):
    for u, w in tree[v]:
        if u == p:
            continue
        else:
            dist[u] = dist[v] + w
            dfs(u, v, dist)


n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    tree[u - 1].append((v - 1, w))
    tree[v - 1].append((u - 1, w))

dist = [0 for _ in range(n)]
dfs(0, -1, dist)

for i in dist:
    if i % 2 == 0:
        print(0)
    else:
        print(1)

Buggy python submission (verdict: Runtime Error):
def dfs(v, p, dist):
    for u, w in tree[v]:
        if u == p:
            continue
        else:
            dist[u] = dist[v] + w
            dfs(u, v, dist)


n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    tree[u - 1].append((v - 1, w))
    tree[v - 1].append((u - 1, w))

dist = [0 for _ in range(n)]
dfs(0, 0, dist)

for i in dist:
    if i % 2 == 0:
        print(0)
    else:
        print(1)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)


def dfs(v, p, dist):
    for u, w in tree[v]:
        if u == p:
            continue
        else:
            dist[u] = dist[v] + w
            dfs(u, v, dist)


n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    tree[u - 1].append((v - 1, w))
    tree[v - 1].append((u - 1, w))

dist = [0 for _ in range(n)]
dfs(0, -1, dist)

for i in dist:
    if i % 2 == 0:
        print(0)
    else:
        print(1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())

    tree = [[] for _ in range(n)]
    for _ in range(m):
        x, y, _ = map(int, input().split())
        tree[x - 1].append(y - 1)
        tree[y - 1].append(x - 1)

    def dfs(v):
        for u in tree[v]:
            if visited[u]:
                continue
            else:
                visited[u] = True
                dfs(u)

    visited = [False for _ in range(n)]
    res = 0
    for i in range(n):
        if not visited[i]:
            res += 1
            visited[i] = True
            dfs(i)

    print(res)


if __name__ == '__main__':
    resolve()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)

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

tree = [[] for _ in range(n)]
for _ in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [False for _ in range(n)]


def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        elif visited[u]:
            continue
        else:
            visited[u] = True
            dfs(u, v)


res = 0
for i in range(n):
    if not visited[i]:
        visited[i] = True
        dfs(i, -1)
        res += 1

print(res)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())

tree = [[] for _ in range(n)]
for _ in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [False for _ in range(n)]


def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        if visited[u]:
            continue
        else:
            visited[u] = True
            dfs(u, v)


res = 0
for i in range(n):
    if not visited[i]:
        dfs(i, -1)
        res += 1
print(res)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

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

tree = [[] for _ in range(n)]
for _ in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [False for _ in range(n)]


def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        elif visited[u]:
            continue
        else:
            visited[u] = True
            dfs(u, v)


res = 0
for i in range(n):
    if not visited[i]:
        visited[i] = True
        dfs(i, -1)
        res += 1

print(res)
Buggy python submission (verdict: Runtime Error):
n = int(input())
B = list(map(int, input().split()))

if B[0] != 1:
	print(-1)
	exit()

cnt = 1
for i in range(1, n):
	if B[i] == 1:
		cnt = 1
	
	if B[i] - 1 <= cnt:
		cnt += 1
	else:
		print(-1)
		exit()

res = []
end = n
for i in reversed(range(n)):
	if B[i] == 1:
		A = B[i + 1 : end]
		res.append(1)
		L = []
		for j in range(len(A)):
			if j + 2 != A[j]:
				L.append(A[j])
				A.pop(j)
			
		L.sort()
		for l in L:
			res.append(l)
		
		for a in A:
			res.append(a)
			
		end = i

for i in res:
	print(i)
		
	

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    B = list(map(int, input().split()))

    res = []
    for i in range(n):
        L = len(B)
        for j in reversed(range(L)):
            if j + 1 == B[j]:
                res.append(B[j])
                B.pop(j)
                break
        else:
            print(-1)
            exit()

    res = res[::-1]
    for i in res:
        print(i)


if __name__ == '__main__':
    resolve()
Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    C = list(int(input()) for _ in range(n))

    dp = [0] * (n + 1)
    dp[0] = 1
    dp2 = [-1] * (n + 1)
    for i in range(n):
        c = C[i]
        if dp2[c] >= 0 and dp2[c] < i - 1:
            dp[i + 1] = dp[i] + dp[dp2[c] + 1]
        else:
            dp[i + 1] = dp[i]
        dp2[c] = i

    print(dp[-1])


if __name__ == '__main__':
    resolve()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    C = list(int(input()) for _ in range(n))
    MAX_C = max(C)

    dp = [0] * (n + 1)
    dp[0] = 1
    dp2 = [-1] * (MAX_C + 1)
    for i in range(n):
        c = C[i]
        if dp2[c] >= 0 and dp2[c] < i - 1:
            dp[i + 1] = dp[i] + dp[dp2[c] + 1]
        else:
            dp[i + 1] = dp[i]
        dp[i + 1] %= mod
        dp2[c] = i

    print(dp[-1] % mod)


if __name__ == '__main__':
    resolve()
Buggy python submission (verdict: Runtime Error):
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    a, b, q = map(int, input().split())
    S = list(int(input()) for _ in range(a))
    T = list(int(input()) for _ in range(b))

    for _ in range(q):
        x = int(input())
        idx_s = bisect_left(S, x)

        kouho_s = []
        if idx_s != 0:
            kouho_s.append(idx_s - 1)
        if idx_s != a:
            kouho_s.append(idx_s)

        res = f_inf
        for idx_s in kouho_s:
            dist = abs(x - S[idx_s])
            now = S[idx_s]
            idx_t = bisect_left(T, now)
            kouho_t = []
            if idx_t != 0:
                kouho_t.append(idx_t - 1)
            if idx_t != b:
                kouho_t.append(idx_t)
            for idx_t in kouho_t:
                dist += abs(now - T[idx_t])
                res = min(res, dist)

        kouho_t = []
        if idx_t != 0:
            kouho_t.append(idx_t - 1)
        if idx_t != b:
            kouho_t.append(idx_t)

        for idx_t in kouho_t:
            dist = abs(x - T[idx_t])
            now = T[idx_t]
            idx_s = bisect_left(S, now)
            kouho_s = []
            if idx_s != 0:
                kouho_s.append(idx_s - 1)
            if idx_s != b:
                kouho_s.append(idx_s)
            for idx_s in kouho_s:
                dist += abs(now - S[idx_t])
                res = min(res, dist)
        print(res)


if __name__ == '__main__':
    resolve()


Fixed submission:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    a, b, q = map(int, input().split())
    S = [-f_inf] + list(int(input()) for _ in range(a)) + [f_inf]
    T = [-f_inf] + list(int(input()) for _ in range(b)) + [f_inf]

    for _ in range(q):
        res = f_inf
        x = int(input())

        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)
        for s in [S[idx_s - 1], S[idx_s]]:
            for t in [T[idx_t - 1], T[idx_t]]:
                d1 = abs(s - x) + abs(s - t)
                d2 = abs(t - x) + abs(t - s)
                res = min(res, d1, d2)
        print(res)


if __name__ == '__main__':
    resolve()
Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    L = [(1, 2), (2, 5), (3, 5), (4, 4), (5, 5), (6, 6), (7, 3), (8, 7), (9, 6)]
    B = []
    for num, cost in L:
        if num in A:
            B.append([num, cost])
    B.sort(key=lambda x: -x[0])
    dp = [-f_inf] * (n + 1)
    dp[0] = 0
    for i in range(n + 1):
        for _, cost in B:
            if i - cost == 0:
                dp[i] = max(dp[i], dp[0] + 1)
            elif dp[i - cost] != 0:
                dp[i] = max(dp[i], dp[i - cost] + 1)
    j = 0
    k = n
    res = ""
    while k > 0 and j < n:
        num, cost = B[j]
        if k - cost >= 0 and dp[k] - dp[k - cost] == 1:
            res += str(num)
            k -= cost
        else:
            j += 1
    print(res)


if __name__ == '__main__':
    resolve()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    L = [(1, 2), (2, 5), (3, 5), (4, 4), (5, 5), (6, 6), (7, 3), (8, 7), (9, 6)]
    B = []
    for num, cost in L:
        if num in A:
            B.append([num, cost])
    B.sort(key=lambda x: -x[0])
    dp = [-f_inf] * (n + 1)
    dp[0] = 0
    for i in range(n + 1):
        for _, cost in B:
            if i - cost == 0:
                dp[i] = max(dp[i], dp[0] + 1)
            elif i - cost > 0 and dp[i - cost] != 0:
                dp[i] = max(dp[i], dp[i - cost] + 1)
    j = 0
    k = n
    res = ""
    while k > 0 and j < m:
        num, cost = B[j]
        if k - cost >= 0 and dp[k] - dp[k - cost] == 1:
            res += str(num)
            k -= cost
        else:
            j += 1
    print(res)


if __name__ == '__main__':
    resolve()