Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
h, w, m = map(int, input().split())

targets = []
rowsum = [0] * h
rowmax = 0
colsum = [0] * w
colmax = 0
for _ in range(m):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    targets.append((hi, wi))
    rowsum[hi] += 1
    rowmax = max(rowmax, rowsum[hi])
    colsum[wi] += 1
    colmax = max(colmax, colsum[wi])

rowindices = []
for i, v in enumerate(rowsum):
    if v == rowmax:
        rowindices.append(i)

colindices = []
for i, v in enumerate(colsum):
    if v == colmax:
        colindices.append(i)

ans = rowmax + colmax - 1
for ri in rowindices:
    for ci in colindices:
        if (ri, ci) not in targets:
            print(rowmax + colmax)
            exit()

print(ans)

Fixed submission:
from collections import defaultdict

h, w, m = map(int, input().split())

rowsum = [0] * h
rowmax = 0
colsum = [0] * w
colmax = 0
targets = defaultdict(int)
for _ in range(m):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    rowsum[hi] += 1
    rowmax = max(rowmax, rowsum[hi])
    colsum[wi] += 1
    colmax = max(colmax, colsum[wi])
    targets[(hi, wi)] = 1

rowindices = []
for i, v in enumerate(rowsum):
    if v == rowmax:
        rowindices.append(i)

colindices = []
for i, v in enumerate(colsum):
    if v == colmax:
        colindices.append(i)

ans = rowmax + colmax - 1
for ri in rowindices:
    for ci in colindices:
        if targets[(ri, ci)]:
            continue
        print(rowmax + colmax)
        exit()

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, m = map(int, input().split())
grid = [[0 for _ in range(w)] for _ in range(h)]
for _ in range(m):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    grid[hi][wi] = 1

rowsum = [0] * h
colsum = [0] * w
for i in range(h):
    for j in range(w):
        rowsum[i] += grid[i][j]
        colsum[j] += grid[i][j]

rowmax = max(rowsum)
rowindices = []
for i, v in enumerate(rowsum):
    if v == rowmax:
        rowindices.append(i)

colmax = max(colsum)
colindices = []
for i, v in enumerate(colsum):
    if v == colmax:
        colindices.append(i)

ans = 0
for ri in rowindices:
    for ci in colindices:
        ans = max(ans, rowmax + colmax - grid[ri][ci])

print(ans)

Fixed submission:
from collections import defaultdict

h, w, m = map(int, input().split())

rowsum = [0] * h
rowmax = 0
colsum = [0] * w
colmax = 0
targets = defaultdict(int)
for _ in range(m):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    rowsum[hi] += 1
    rowmax = max(rowmax, rowsum[hi])
    colsum[wi] += 1
    colmax = max(colmax, colsum[wi])
    targets[(hi, wi)] = 1

rowindices = []
for i, v in enumerate(rowsum):
    if v == rowmax:
        rowindices.append(i)

colindices = []
for i, v in enumerate(colsum):
    if v == colmax:
        colindices.append(i)

ans = rowmax + colmax - 1
for ri in rowindices:
    for ci in colindices:
        if targets[(ri, ci)]:
            continue
        print(rowmax + colmax)
        exit()

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
score = 1
for i in range(k):
    score *= a[i]

for i in range(k, n):
    if a[i] > a[i - k]:
        print('Yes')
    else:
        print('No')
    score /= a[i - k]
    score *= a[i]

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

for i in range(k, n):
    if a[i] > a[i - k]:
        print('Yes')
    else:
        print('No')

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
score = 1
for i in range(k):
    score *= a[i]

for i in range(k, n):
    if a[i] > a[i - k]:
        print('Yes')
    else:
        print('No')
    score /= a[i - k]
    score *= a[i]

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

for i in range(k, n):
    if a[i] > a[i - k]:
        print('Yes')
    else:
        print('No')

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
score = 1
for i in range(k):
    score *= a[i]

for i in range(k, n):
    if a[i] > a[i - k]:
        print('Yes')
    else:
        print('No')
    score *= a[i]

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

for i in range(k, n):
    if a[i] > a[i - k]:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Runtime Error):
orz

Fixed submission:
n = int(input())
a = list(map(int, input().split()))

money = 1000
for x, y in zip(a[:-1], a[1:]):
    if x < y:
        stocks = money // x
    else:
        stocks = 0
    money += (y - x) * stocks

print(money)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left, bisect_right

n = int(input())
a = list(map(int, input().split()))
a.sort()
m = a[-1]
c = [0] * (m + 1)

for ai in a:
    for i in range(ai, m + 1, ai):
        if bisect_right(a, i) - bisect_left(a, i) == 1:
            c[i] += 1

print(c.count(1))

Fixed submission:
x = list(map(int, input().split()))
print(15 - sum(x))
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left, bisect_right

n = int(input())
a = list(map(int, input().split()))
a.sort()
m = a[-1]
c = [0] * (m + 1)

for ai in a:
    for i in range(ai, m + 1, ai):
        if bisect_right(a, i) - bisect_left(a, i) == 1:
            c[i] += 1

print(c.count(1))

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
m = a[-1]
c = [0] * (m + 1)

for ai in a:
    for i in range(ai, m + 1, ai):
        c[i] += 1

ans = 0
for ai in a:
    if c[ai] == 1:
        ans += c[ai]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
m = max(a)
c = [0] * (m + 1)

for ai in a:
    for i in range(ai, m + 1, ai):
        if i in a:
            c[i] += 1

print(c.count(1))

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
m = a[-1]
c = [0] * (m + 1)

for ai in a:
    for i in range(ai, m + 1, ai):
        c[i] += 1

ans = 0
for ai in a:
    if c[ai] == 1:
        ans += c[ai]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()

for i in range(n):
    if a[i] == 0:
        continue
    m = a[i]
    for j in range(i + 1, n):
        if a[j] == m:
            a[i] = 0
        if a[j] % m == 0:
            a[j] = 0

ans = sum(x > 0 for x in a)
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
m = a[-1]
c = [0] * (m + 1)

for ai in a:
    for i in range(ai, m + 1, ai):
        c[i] += 1

ans = 0
for ai in a:
    if c[ai] == 1:
        ans += c[ai]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()

for i in range(n):
    if a[i] == 0:
        continue
    m = a[i]
    for j in range(i + 1, n):
        if a[j] == m:
            a[i] = 0
        if a[j] % m == 0:
            a[j] = 0

ans = sum(x > 0 for x in a)
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
m = a[-1]
c = [0] * (m + 1)

for ai in a:
    for i in range(ai, m + 1, ai):
        c[i] += 1

ans = 0
for ai in a:
    if c[ai] == 1:
        ans += c[ai]

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
h, n = map(int, input().split())
spells = [list(map(int, input().split())) for _ in range(n)]
INF = 10 ** 9
dp = [INF] * (h + 1)
dp[0] = 0

for i in range(h):
    for a, b in spells:
        ni = min(i + a, h)
        dp[ni] = min(dp[ni], dp[i] + b)

print(dp[h])

Fixed submission:
h, n = map(int, input().split())
spells = [list(map(int, input().split())) for _ in range(n)]
INF = 10 ** 9
dp = [INF] * (h + 1)
dp[0] = 0

for i in range(h):
    for a, b in spells:
        ni = min(i + a, h)
        dp[ni] = min(dp[ni], dp[i] + b)

print(dp[h])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7

ans = 0
for i in range(60):
    x = [(bool)(ai & 1 << i) for ai in a]
    sx = sum(x)
    count = sx * (n - sx)
    ans += count << i
    ans %= MOD

print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7

ans = 0
for i in range(60):
    x = [(bool)(ai & 1 << i) for ai in a]
    sx = sum(x)
    count = sx * (n - sx)
    ans += count << i
    ans %= MOD

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
links = [[] for _ in range(n)]
colors = [0] * (n - 1)

for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    links[a].append((b, i))

def dfs(a, color):
    c = 1
    for b, i in links[a]:
        if c == color:
            c += 1
        colors[i] = c
        dfs(b, c)
        c += 1


dfs(0, 0)
print(max(colors))
for color in colors:
    print(color)

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

n = int(input())
links = [[] for _ in range(n)]
colors = [0] * (n - 1)

for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    links[a].append((b, i))

def dfs(a, color):
    c = 1
    for b, i in links[a]:
        if c == color:
            c += 1
        colors[i] = c
        dfs(b, c)
        c += 1


dfs(0, 0)
print(max(colors))
for color in colors:
    print(color)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
x = list(map(int, input().split()))
x.sort(reverse=True)

ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        a, b = x[i], x[j]
        left, right = j + 1, n
        while right - left > 1:
            k = (left + right) // 2
            if a < b + x[k]:
                left = k
            else:
                right = k
        if a < b + x[left]:
            ans += (left - j)

print(ans)

Fixed submission:
n = int(input())
x = list(map(int, input().split()))
x.sort(reverse=True)

ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        a, b = x[i], x[j]
        left, right = j + 1, n
        while right - left > 1:
            k = (left + right) // 2
            if a < b + x[k]:
                left = k
            else:
                right = k
        if a < b + x[left]:
            ans += (left - j)

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
b = [0] * n

for i in reversed(range(n)):
    for j in range(i + 1, n, i + 1):
        x = 1
        k = j
        while k < n:
            x ^= b[k - 1]
            k += j
        if x == a[i]:
            b[i] = 1

print(sum(b))
for i in range(n):
    if b[i] == 1:
        print(i + 1)

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

b = sorted(a, reverse=True)
max1 = b[0]
max2 = b[1]

for ai in a:
    if ai != max1:
        print(max1)
    else:
        print(max2)
Buggy python submission (verdict: Runtime Error):
factorial = [0] * 2000
factorial[0] = 1
factorial[1] = 1
for i in range(2, 2000):
    factorial[i] = i * factorial[i - 1]


def combination(n, r):
    return factorial[n] / (factorial[r] * factorial[n - r])


n, k = map(int, input().split())
MOD = 10 ** 9 + 7

for i in range(k):
    x = int(combination(n - k + 1, n - k - i) % MOD)
    y = int(combination(k - 1, i) % MOD)
    print(x * y % MOD)

Fixed submission:
n, k = map(int, input().split())
MOD = 10 ** 9 + 7

inv = [0] * 2000
inv[1] = 1
for i in range(2, 2000):
    inv[i] = (MOD - (MOD // i) * inv[MOD % i] % MOD) % MOD

fact = [0] * 2000
finv = [0] * 2000
fact[0] = 1
finv[0] = 1
for i in range(1, 2000):
    fact[i] = (i * fact[i - 1]) % MOD
    finv[i] = (inv[i] * finv[i - 1]) % MOD


def modcomb(n, r):
    if n < r or r < 0:
        return 0
    return fact[n] * finv[r] % MOD * finv[n - r] % MOD


for i in range(k):
    x = modcomb(n - k + 1, i + 1)
    y = modcomb(k - 1, i)
    print(x * y % MOD)

Buggy python submission (verdict: Runtime Error):
factorial = [0] * 2000
factorial[0] = 1
factorial[1] = 1
for i in range(2, 2000):
    factorial[i] = i * factorial[i - 1]


def combination(n, r):
    return factorial[n] / (factorial[r] * factorial[n - r])


n, k = map(int, input().split())
MOD = 10 ** 9 + 7

for i in range(k):
    x = int(combination(n - k + 1, i + 1) % MOD)
    y = int(combination(k - 1, i) % MOD)
    print(x * y % MOD)

Fixed submission:
n, k = map(int, input().split())
MOD = 10 ** 9 + 7

inv = [0] * 2000
inv[1] = 1
for i in range(2, 2000):
    inv[i] = (MOD - (MOD // i) * inv[MOD % i] % MOD) % MOD

fact = [0] * 2000
finv = [0] * 2000
fact[0] = 1
finv[0] = 1
for i in range(1, 2000):
    fact[i] = (i * fact[i - 1]) % MOD
    finv[i] = (inv[i] * finv[i - 1]) % MOD


def modcomb(n, r):
    if n < r or r < 0:
        return 0
    return fact[n] * finv[r] % MOD * finv[n - r] % MOD


for i in range(k):
    x = modcomb(n - k + 1, i + 1)
    y = modcomb(k - 1, i)
    print(x * y % MOD)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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

dp = [0] * (n + 1)
dp[0] = 1
if 1 in a:
    dp[1] = 0
else:
    dp[1] = 1

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

print(dp[-1])

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

dp = [0] * (n + 1)
dp[0] = 1
if not a[1]:
    dp[1] = 1

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

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
MOD = 10 ** 9 + 7

dp = [0] * (n + 1)
dp[0] = 1
if 1 in a:
    dp[1] = 0
else:
    dp[1] = 1

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

print(dp[n])

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

dp = [0] * (n + 1)
dp[0] = 1
if not a[1]:
    dp[1] = 1

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

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
MOD = 10 ** 9 + 7

dp = [0] * (n + 1)
dp[0] = 1
if 1 in a:
    dp[1] = 0
else:
    dp[1] = 1

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

print(dp[-1])

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

dp = [0] * (n + 1)
dp[0] = 1
if not a[1]:
    dp[1] = 1

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

print(dp[-1])
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1], reverse=True)


def f():
    s = sum(a)
    i = 0
    for v in bc:
        b, c = map(int, v)
        while b > 0:
            if c < a[i] or i >= n:
                return s
            s += c - a[i]
            b -= 1
            i += 1
    return s


print(f())

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


def f():
    s = sum(a)
    i = 0
    for v in bc:
        b, c = map(int, v)
        while b > 0:
            if i >= n:
                return s
            if c <= a[i]:
                return s
            s += c - a[i]
            b -= 1
            i += 1
    return s


print(f())

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc += [c] * b

a.sort()
bc.sort(reverse=True)
sum_a = sum(a)

ans = sum_a
for i, j in zip(a, bc):
    if i >= j:
        break
    ans += j - i

print(ans)

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


def f():
    s = sum(a)
    i = 0
    for v in bc:
        b, c = map(int, v)
        while b > 0:
            if i >= n:
                return s
            if c <= a[i]:
                return s
            s += c - a[i]
            b -= 1
            i += 1
    return s


print(f())

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc += [c] * b

a.sort()
bc.sort(reverse=True)
sum_a = sum(a)

ans = sum_a
for i, j in zip(a, bc):
    if i >= j:
        break
    ans += j - i

print(ans)

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


def f():
    s = sum(a)
    i = 0
    for v in bc:
        b, c = map(int, v)
        while b > 0:
            if i >= n:
                return s
            if c <= a[i]:
                return s
            s += c - a[i]
            b -= 1
            i += 1
    return s


print(f())
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 6)


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


def xgcd(a):
    size = len(a)
    if size == 1:
        return a[0]
    if size == 2:
        return gcd(a[0], a[1])

    la = a[:size // 2]
    ra = a[size // 2:]

    return gcd(xgcd(la), xgcd(ra))


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


ans = 0
for i in range(n):
    tmp = a[i]
    a[i] = a[i - 1]
    ans = max(ans, xgcd(a))
    a[i] = tmp

print(ans)

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


n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
c = [0] * (n + 1)

for i in range(n):
    b[i + 1] = gcd(b[i], a[i])
    c[i + 1] = gcd(c[i], a[-i - 1])

ans = 0
for i in range(n):
    ans = max(ans, gcd(b[i], c[n - i - 1]))

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = [ai + bi for bi in b[:k] for ai in a[:k]]
ab.sort(reverse=True)

s = [ci + abi for abi in ab[:k] for ci in c[:k]]
s.sort(reverse=True)

for si in s[:k]:
    print(si)

Fixed submission:
import sys
input = sys.stdin.readline

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = [ai + bi for bi in b[:k] for ai in a[:k]]
ab.sort(reverse=True)

s = [ci + abi for abi in ab[:k] for ci in c[:k]]
s.sort(reverse=True)

for si in s[:k]:
    print(si)

Buggy python submission (verdict: Time Limit Exceeded):
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = [ai + bi for bi in b for ai in a]
ab.sort(reverse=True)
ab = ab[:k]

s = [ci + abi for abi in ab for ci in c]
s.sort(reverse=True)

for si in s[:k]:
    print(si)

Fixed submission:
import sys
input = sys.stdin.readline

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = [ai + bi for bi in b[:k] for ai in a[:k]]
ab.sort(reverse=True)

s = [ci + abi for abi in ab[:k] for ci in c[:k]]
s.sort(reverse=True)

for si in s[:k]:
    print(si)

Buggy python submission (verdict: Time Limit Exceeded):
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = [ai + bi for bi in b for ai in a]
s = [ci + abi for abi in ab for ci in c]
s.sort(reverse=True)

for si in s[:k]:
    print(si)

Fixed submission:
import sys
input = sys.stdin.readline

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = [ai + bi for bi in b[:k] for ai in a[:k]]
ab.sort(reverse=True)

s = [ci + abi for abi in ab[:k] for ci in c[:k]]
s.sort(reverse=True)

for si in s[:k]:
    print(si)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 10)
dp[0] = 0

for i in range(n):
    for ai in a:
        dp[i + cost[ai]] = dp[i] + 1 if dp[i] > 0 else 1

ans = []
i = n
while dp[i] > 0:
    for ai in a:
        if dp[i - cost[ai]] == dp[i] - 1:
            ans.append(str(ai))
            i -= cost[ai]
            break

print(''.join(ans))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 10)
dp[0] = 0

for i in range(n):
    for ai in a:
        if dp[i] >= 0:
            dp[i + cost[ai]] = dp[i] + 1

ans = []
i = n
while dp[i] > 0:
    for ai in a:
        if dp[i - cost[ai]] == dp[i] - 1:
            ans.append(str(ai))
            i -= cost[ai]
            break

print(''.join(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 10)
dp[0] = 0

for i in range(n):
    for ai in a:
        dp[i + cost[ai]] = dp[i] + 1 if dp[i] > 0 else 1

ans = []
i = n
while dp[i] > 0:
    for ai in a:
        if dp[i - cost[ai]] == dp[i] - 1:
            ans.append(str(ai))
            i -= cost[ai]
            break

print(''.join(ans))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 10)
dp[0] = 0

for i in range(n):
    for ai in a:
        if dp[i] >= 0:
            dp[i + cost[ai]] = dp[i] + 1

ans = []
i = n
while dp[i] > 0:
    for ai in a:
        if dp[i - cost[ai]] == dp[i] - 1:
            ans.append(str(ai))
            i -= cost[ai]
            break

print(''.join(ans))
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

lenbin = len(bin(max(max(a), k))) - 2
bitcnt = [0] * lenbin

for i in range(lenbin):
    for ai in a:
        if ai & (1 << i):
            bitcnt[i] += 1

x = 0
for i in range(n - 1, -1, -1):
    if bitcnt[i] <= n // 2 and x + (1 << i) <= k:
        x += (1 << i)

ans = 0
for ai in a:
    ans += (x ^ ai)

print(ans)

Fixed submission:
#!/usr/bin/python3

n, k = map(int, input().split())
a = list(map(int, input().split()))

lenbin = len(bin(max(max(a), k))) - 2
bitcnt = [0] * lenbin

for i in range(lenbin):
    for ai in a:
        if ai & (1 << i):
            bitcnt[i] += 1

x = 0
for i in range(lenbin - 1, -1, -1):
    if bitcnt[i] <= n // 2 and x + (1 << i) <= k:
        x += (1 << i)

ans = 0
for ai in a:
    ans += (x ^ ai)

print(ans)