alexjercan/bugnet huggingface dataset:
Problem p03777 (Python) — buggy submission, verdict: Runtime Error

a, b = input().split()
if a == "H":
    print(b)
else:
    if b == "H":
        print("D")
    else:
        print("H")


Error: ValueError: invalid literal for int() with base 10: 'H'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03777/Python/s046292016.py", line 1, in <module>
    a, b = map(int, input().split())
ValueError: invalid literal for int() with base 10: 'H'


Fixed submission:

a, b = map(int, input().split())
if a == "H":
    print(b)
else:
    if b == "H":
        print("D")
    else:
        print("H")
Problem p03778 (Python) — buggy submission, verdict: Runtime Error

w, a, b = map(int, input().split())

if b > a + w:
    print(b - a - w)
elif b + w < a:
    print(a - b - w)
else:
    print(0)


Error: EOFError: EOF when reading a line
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03778/Python/s917667929.py", line 9, in <module>
    w, a, b = map(int, input().split())
EOFError: EOF when reading a line


Fixed submission:

w, a, b = map(int, input().split())

if b > a + w:
    print(b - a - w)
elif b + w < a:
    print(a - b - w)
else:
    print(0)
w, a, b = map(int, input().split())

if b > a + w:
    print(b - a - w)
elif b + w < a:
    print(a - b - w)
else:
    print(0)
Problem p03779 (Python) — buggy submission, verdict: Time Limit Exceeded

X = int(input())

ans = 0
sums = 0
while sums < X:
    ans += 1
    sums += ans
print(ans)


Error: TLE

Fixed submission:

X = int(input())
dp = set([0])

ans = 0
while X not in dp:
    ans += 1
    for i in list(dp):
        if i + ans not in dp:
            dp.add(i + ans)
        if i - ans not in dp:
            dp.add(i - ans)
print(ans)
Problem p03779 (Python) — buggy submission, verdict: Time Limit Exceeded

X = int(input())
acc = 0
# X > 0 なのでマイナス方向にジャンプする選択肢は選ばない
for i in range(X + 1):
    acc += i
    if X <= acc:
        ans = i
        break
print(i)


Error: TLE

Fixed submission:

X = int(input())
# X > 0 なのでマイナス方向にジャンプする選択肢は選ばない
cnt = 0
while X != 0:
    if X != cnt + 1:
        X -= cnt
    cnt += 1
print(cnt - 1)
Problem p03779 (Python) — buggy submission, verdict: Runtime Error

x = int(input())

w = 0
for i in range(1, x + 1):
    w += i
    if w >= x:
        print(i)
        exit(0)


Error: 0

Fixed submission:

from itertools import combinations

int_x = int(input())

answer = int_x

for i in range(1, int_x + 1):
    c = combinations(range(1, int_x + 1), i)
    for w_c in c:
        if sum(w_c) == int_x:
            answer = min(answer, max(w_c))
print(answer)
Problem p03779 (Python) — buggy submission, verdict: Runtime Error

x = int(input())
ans = i = 0
while i < x:
    ans += 1
    i += ans
print(ans)


Error: ValueError: not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03779/Python/s896370113.py", line 1, in <module>
    n, m = map(int, input().split())
ValueError: not enough values to unpack (expected 2, got 1)


Fixed submission:

n, m = map(int, input().split())
print([2, 1][m > 1])
Problem p03779 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python
# -*- coding: utf-8 -*-

x = int(input())


total = 0
for i in range(x + 1):
    total += i
    if total >= x:
        print(i)
        break


Error: TLE

Fixed submission:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

x = int(input())
i, pos = 0, set([0])

while True:
    inner = False
    new_pos = set()
    for p in pos:
        if p + i == x or p - i == x:
            inner = True
            break
        new_pos.add(p)
        new_pos.add(p + i)
        new_pos.add(p - i)

    if inner:
        print(i)
        break

    pos = new_pos
    i += 1
Problem p03780 (Python) — buggy submission, verdict: Runtime Error

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

    # [ok, ng) - Maximum
    # (ng, ok] - Minimum
    # ok が 最終的な答え
    ok = N
    ng = -1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        n = mid
        dp = [0] * (K + 1)
        dp[0] = 1
        for i in range(N):
            a = A[i]
            if i == n or a >= K:
                continue
            for j in range(K, -1, -1):
                dp[min(K, j + a)] |= dp[j]
        rst = any(dp[k] for k in range(max(0, K - A[n]), K))
        if rst:
            ok = mid
        else:
            ng = mid
    print(ok)


main()


Error: 0

Fixed submission:

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


# [ok, ng) - Maximum
# (ng, ok] - Minimum
# ok が 最終的な答え
ok = N
ng = -1
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    n = mid
    dp = set()
    dp.add(0)
    ans = set(x for x in range(max(0, K - A[n] + 1), K))
    for i in range(N):
        a = A[i]
        if i == n or a >= K:
            continue
        for j in range(K - a - 1, -1, -1):
            if j in dp:
                if j + a in ans:
                    rst = True
                    break
                else:
                    dp.add(j + a)
        else:
            rst = False
    if rst:
        ok = mid
    else:
        ng = mid
print(ok)
Problem p03780 (Python) — buggy submission, verdict: Memory Limit Exceeded

import numpy as np

N, K = map(int, input().split())
A = tuple(map(int, input().split()))

dp_left = np.zeros((N + 1, K), dtype=bool)
dp_right = np.zeros((N + 1, K), dtype=bool)
dp_left[0, 0] = 1
dp_right[N, 0] = 1

for i in range(N):
    dp_left[i + 1] = dp_left[i]
    dp_right[N - i - 1] = dp_right[N - i]
    if (a := A[i]) < K:
        dp_left[i + 1, a:] |= dp_left[i, :-a]
    if (a := A[N - i - 1]) < K:
        dp_right[N - i - 1, a:] |= dp_right[N - i, :-a]

ans = 0
for i, a in enumerate(A):
    left_true = np.nonzero(dp_left[i])[0]
    right_true = np.nonzero(dp_right[i + 1])[0]

    ss = np.searchsorted(right_true, K - left_true) - 1
    if all(right_true[ss] + left_true + a < K):
        ans += 1

print(ans)


Error: MLE

Fixed submission:

import numpy as np

N, K = map(int, input().split())
A = tuple(map(int, input().split()))

dp_left = np.zeros((N + 1, K), dtype=bool)
dp_right = np.zeros((N + 1, K), dtype=bool)
dp_left[0, 0] = 1
dp_right[N, 0] = 1

for i in range(N):
    dp_left[i + 1] = dp_left[i]
    dp_right[N - i - 1] = dp_right[N - i]
    if (a := A[i]) < K:
        dp_left[i + 1, a:] |= dp_left[i, :-a]
    if (a := A[N - i - 1]) < K:
        dp_right[N - i - 1, a:] |= dp_right[N - i, :-a]

left_true = [np.nonzero(x)[0] for x in dp_left]
del dp_left
right_true = [np.nonzero(x)[0] for x in dp_right]
del dp_right

ans = 0
for i, a in enumerate(A):
    ss = np.searchsorted(right_true[i + 1], K - left_true[i]) - 1
    if all(right_true[i + 1][ss] + left_true[i] + a < K):
        ans += 1
print(ans)
Problem p03781 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3

import math

print(math.ceil((-1 + (8 * int(input()) + 1) ** 0.5) / 2))


Error: TLE

Fixed submission:

#!/usr/bin/env python3

x = int(input())
S = {0}
i = 0
while x not in S:
    i += 1
    S = S | {s - i for s in S} | {s + i for s in S}
print(i)
Problem p03781 (Python) — buggy submission, verdict: Time Limit Exceeded

from math import ceil, sqrt

x = int(input())
print(ceil((-1 + sqrt(8 * x + 1)) / 2))


Error: TLE

Fixed submission:

x = int(input())
t = 1
ds = {0}
while True:
    dc = ds.copy()
    for d in dc:
        if d + t == x or d - t == x:
            break
        ds.add(d + t)
        ds.add(d - t)
    else:
        t += 1
        continue
    break

print(t)
Problem p03785 (Python) — buggy submission, verdict: Time Limit Exceeded

N, C, K = list(map(int, input().split()))

passengers = sorted([int(input()) for i in range(N)])
res = 0
bus_time = passengers[0]
bus_count = C
for p in passengers:
    if bus_count == 0 or p - bus_time > K:
        res += 1
        bus_time = p
        bus_count = C - 1
    else:
        bus_count -= 1
if bus_count != C:
    res += 1
print(res)


Error: TLE

Fixed submission:

N, C, K = list(map(int, input().split()))

passengers = sorted([int(input()) for i in range(N)])
res = {}

for p in passengers:
    key = False
    for k in res.keys():
        if k <= p and p <= k + K and res[k] <= C - 1:
            res[k] += 1
            key = True
            break
    if not key:
        res[p] = 1
print(len(res))
Problem p03785 (Python) — buggy submission, verdict: Runtime Error

def resolve():
    n, c, k = map(int, input().split())
    t = list(int(input()) for _ in range(n))
    t.sort()
    ans = 1
    limit = t[0] + k
    bus = 0
    for i in range(n):
        bus += 1
        if t[i] > limit or bus > c:
            limit = t[i] + k
            ans += 1
            bus = 1
    print(ans)


if __name__ == "__main__":
    resolve()


Error: 0

Fixed submission:

def resolve():
    n, c, k = map(int, input().split())
    t = list(int(input()) for _ in range(n))
    t.sort()
    ans = 0
    limit = t[0] + k
    bus = 0
    for i in range(n):
        if t[i] <= limit:
            bus += 1
            if bus == c:
                ans += 1
                bus = 0
                if i < n:
                    limit = t[i + 1] + k
        else:
            ans += 1
            bus = 1
            limit = t[i] + k
    if bus > 0:
        ans += 1
    print(ans)


if __name__ == "__main__":
    resolve()
Problem p03785 (Python) — buggy submission, verdict: Runtime Error

N, C, K = [int(i) for i in input().split()]
T = sorted([int(input()) for i in range(N)])
ans = 1
limit = 0
ti = T[0]
for i in range(N - 1):
    limit += 1
    if ti + K < T[i + 1] or limit == C:
        ti = T[i + 1]
        ans += 1
        limit = 0
print(ans)


Error: TypeError: can only concatenate list (not "int") to list
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03785/Python/s415293443.py", line 8, in <module>
    if ti + K < T[i + 1] or limit == C:
TypeError: can only concatenate list (not "int") to list


Fixed submission:

N, C, K = [int(i) for i in input().split()]
T = sorted([int(input())] for i in range(N))
ans = 1
limit = 0
ti = T[0]
for i in range(N - 1):
    limit += 1
    if ti + K < T[i + 1] or limit == C:
        ti = T[i + 1]
        ans += 1
        limit = 0
print(ans)
Problem p03786 (Python) — buggy submission, verdict: Runtime Error

from itertools import accumulate


N, *A = map(int, open(0).read().split())
A.sort()
acc = list(accumulate(A))
ans = N
for i in range(N - 1):
    if 3 * acc[i] < acc[i + 1]:
        ans = N - i - 1
print(ans)


Error: ImportError: cannot import name 'gcd' from 'fractions' (/usr/lib/python3.10/fractions.py)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03786/Python/s206249495.py", line 1, in <module>
    from fractions import gcd
ImportError: cannot import name 'gcd' from 'fractions' (/usr/lib/python3.10/fractions.py)


Fixed submission:

from fractions import gcd
from functools import reduce


N, K = map(int, input().split())
A = list(map(int, input().split()))
g = reduce(gcd, A)
if K <= max(A) and K % g == 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")
Problem p03795 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
discount = 0
if N > 15:
    discount = N // 15
print(N * 800 - discount * 200)


Error: 0

Fixed submission:

N = int(input())
if N > 15:
    discount = N // 15
print(N * 800 - discount * 200)
Problem p03795 (Python) — buggy submission, verdict: Runtime Error

i = int(input())
d = i * 800
b = i // 15

if 0 <= b:
    c = d - (200 * b)
    print(c)
else:
    print(c)


Error: 0

Fixed submission:

i = int(input())
d = i * 800
b = i // 15

if 0 < b:
    c = d - (200 * b)
    print(c)
else:
    print(c)
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

import math


def main():
    N = int(input())
    ans = math.factorial(N)
    mod = 10**9 + 7
    print(ans % mod)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

def main():
    N = int(input())
    ans = 1
    mod = 10**9 + 7
    for i in range(1, N + 1):
        ans *= i
    print(ans % mod)


if __name__ == "__main__":
    main()
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

num = 1
for i in range(1, int(input()) + 1):
    num = (num * i) % (10**9 + 7)
print(num)


Error: TLE

Fixed submission:

N = range(1, int(input()) + 1)
prod = 1
for i in N:
    prod *= i
print(prod % (10**9 + 7))
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())

i = 1
power = 1
while i <= n:
    if power >= 10**9 + 7:
        power %= 10**9 + 7
    power *= i
    i += 1
print(power % (10**9 + 7))


Error: TLE

Fixed submission:

n = int(input())

i = 1
power = 1
while i <= n:
    power *= i
    i += 1
print(power % (10**9 + 7))
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

import math

N = int(input())

print(math.factorial(N) % (10**9 + 7))


Error: TLE

Fixed submission:

N = int(input())

power = 1
for i in range(1, N + 1):
    power *= i
print(power % (10**9 + 7))
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())


mod_num = pow(10, 9) + 7


def main(n):
    pre = 1
    for i in range(2, n + 1):
        pre = pre * i % mod_num
    return pre


ans = main(n)
print(ans)


Error: TLE

Fixed submission:

n = int(input())


def main(n):
    pre = 1
    for i in range(1, n):
        pre = pre * i
    return pre * n % (pow(10, 9) + 7)


ans = main(n)
print(ans)
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
n = int(input())

ans = 1

for i in range(n):
    ans *= i + 1
    ans %= 1000000007

print(ans)


Error: TLE

Fixed submission:

n = int(input())

ans = 1

for i in range(n):
    ans *= i + 1

print(ans % 1000000007)
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
n = 1000000007
p = 1
for i in range(1, N + 1):
    p *= i
    p %= n
print(p)


Error: TLE

Fixed submission:

N = int(input())
n = 1000000007
p = 1
for i in range(1, N + 1):
    p *= i
print(p % n)
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

import math

N = int(input())
print(math.factorial(N) % 1000000007)


Error: TLE

Fixed submission:

N = int(input())
p = 1
for i in range(1, N + 1):
    p *= i
print(p % 1000000007)
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

import math

n = int(input())
print(math.factorial(n) % 1000000007)


Error: TLE

Fixed submission:

n = int(input())
p = 1
for i in range(1, n + 1):
    p = p * i
print(p % 1000000007)
Problem p03796 (Python) — buggy submission, verdict: Time Limit Exceeded

INF = 1000000000 + 7
N = int(input())
ans = 1
for i in range(1, N + 1):
    ans = ans * i % INF

print(ans)


Error: TLE

Fixed submission:

INF = 1000000000 + 7
N = int(input())
ans = 1
for i in range(1, N + 1):
    ans *= i

print(ans % INF)
Problem p03797 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
if n <= m // 2:
    m -= n * 2
    print(n + m // 4)
else:
    print(m // 2)


Error: TLE

Fixed submission:

n, m = map(int, input().split())
c = 0
while n > 0 and m > 2:
    n -= 1
    m -= 2
    c += 1

while m >= 4:
    m -= 4
    c += 1
print(c)
Problem p03797 (Python) — buggy submission, verdict: Time Limit Exceeded

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

ans = 0

ans += min(N, M // 2)

N -= ans
M -= ans * 2

ans += M // 4

print(ans)


Error: TLE

Fixed submission:

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

ans = 0

while True:
    if N >= 1 and M >= 2:
        N -= 1
        M -= 2
        ans += 1
    elif M >= 4:
        M -= 4
        ans += 1
    else:
        break
print(ans)
Problem p03797 (Python) — buggy submission, verdict: Time Limit Exceeded

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

if 2 * n >= m:
    comb += m // 2
    m -= comb * 2
else:
    comb += n
    m -= 2 * n

comb += m // 4

print(comb)


Error: TLE

Fixed submission:

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

while m >= 2 and n >= 1:
    comb += 1
    m -= 2
    n -= 1

comb += m // 4

print(comb)
Problem p03797 (Python) — buggy submission, verdict: Time Limit Exceeded

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

s, c = N, M // 2

ans = 0
if s > c:
    ans = c
else:
    ans = (M - s * 2) // 4 + s
print(ans)


Error: TLE

Fixed submission:

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

ans = 0
while N > 0 and M > 0:
    if M < 2:
        break
    N -= 1
    M -= 2
    ans += 1

while M >= 4:
    M -= 4
    ans += 1

print(ans)
Problem p03799 (Python) — buggy submission, verdict: Time Limit Exceeded

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

cnt = min(n, m // 2)
print(cnt + (m - cnt * 2) // 4)


Error: TLE

Fixed submission:

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

cnt = 0
for _ in range(n):
    if m >= 2:
        m -= 2
        cnt += 1
for _ in range(m // 4):
    if m >= 4:
        m -= 4
        cnt += 1
print(cnt)
Problem p03805 (Python) — buggy submission, verdict: Runtime Error

from itertools import permutations

N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

res = 0
for P in permutations(range(N), N):
    if P[0] != 0:
        continue
    for i in range(N - 1):
        if P[i + 1] not in G[P[i]]:
            break
    else:
        res += 1

print(res)


Error: 0

Fixed submission:

from itertools import permutations

N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(N):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

res = 0
for P in permutations(range(N), N):
    if P[0] != 0:
        continue
    for i in range(N - 1):
        if P[i + 1] not in G[P[i]]:
            break
    else:
        res += 1

print(res)
Problem p03805 (Python) — buggy submission, verdict: Time Limit Exceeded

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

edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)
    edges[b - 1].append(a - 1)


def dfs(node, track):
    if len(track) == N:
        global res
        res += 1
    else:
        for dst in [x for x in edges[node] if x not in track]:
            dfs(dst, track + tuple([dst]))


res = 0
dfs(0, tuple([0]))
print(res)


Error: TLE

Fixed submission:

import itertools


N, M = map(int, input().split())
edges = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(M)]

res = 0
if M < N - 1:
    print(res)
else:
    for path in itertools.permutations(edges, N - 1):
        vertex = [True] * N
        v = 0
        vertex[v] = False
        for edge in path:
            if v in edge:
                nv = edge[0] if v == edge[1] else edge[1]
                if vertex[nv]:
                    vertex[nv] = False
                    v = nv
                    continue
            break
        else:
            res += 1
    print(res)
Problem p03805 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    import sys
    from itertools import permutations

    # readline = sys.stdin.buffer.readline
    readlines = sys.stdin.readlines
    N, M = map(int, input().split())
    edge = [[0] * N for _ in range(N)]
    for s in readlines():
        a, b = map(int, s.split())
        a -= 1
        b -= 1
        edge[a][b] = 1
        edge[b][a] = 1

    cnt = 0
    for path in permutations(range(1, N)):
        path = [0] + list(path)
        for i in range(N - 1):
            v = path[i]
            nv = path[i + 1]
            if edge[v][nv] == 0:
                break
        else:
            cnt += 1
    print(cnt)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import itertools


N, M = map(int, input().split())
edges = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(M)]

res = 0
if M < N - 1:
    print(res)
else:
    for course in itertools.combinations(edges, N - 1):

        num_of_v = [0] * N
        for edge in course:
            num_of_v[edge[0]] += 1
            num_of_v[edge[1]] += 1
        if num_of_v[0] != 1 and max(num_of_v) != 2:
            continue

        for root in itertools.permutations(course):
            vertex = [False] + [True] * (N - 1)
            v = 0
            for edge in root:
                if v in edge:
                    nv = edge[0] if v == edge[1] else edge[1]
                    if vertex[nv]:
                        vertex[nv] = False
                        v = nv
                        continue
                break
            else:
                res += 1
    print(res)
Problem p03805 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
for _ in range(m):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

visit = [0] * (n + 1)


def dfs(now, depth):
    if visit[now]:
        return 0
    if depth == n:
        return 1

    count = 0
    visit[now] = 1
    if graph[now]:
        for i in graph[now]:
            count += dfs(i, depth + 1)
    visit[now] = 0

    return count


ans = dfs(1, 1)
print(ans)


Error: 0

Fixed submission:

import sys

sys.setrecursionlimit(500 * 500)

n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
for _ in range(n):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

visit = [0] * (n + 1)


def dfs(now, depth):
    if visit[now]:
        return 0
    if depth == n:
        return 1

    count = 0
    visit[now] = 1
    if graph[now]:
        for i in graph[now]:
            count += dfs(i, depth + 1)
    visit[now] = 0

    return count


ans = dfs(1, 1)
print(ans)
Problem p03805 (Python) — buggy submission, verdict: Runtime Error

import sys

sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
graph = [set([]) for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    graph[a - 1].add(b - 1)
    graph[b - 1].add(a - 1)


def dfs(graph, visited, v):
    if all(visited):
        return 1

    cnt = 0
    for u in graph[v]:
        if visited[u]:
            continue
        visited[u] = True
        cnt += dfs(graph, visited, u)
        visited[u] = False

    return cnt


visited = [False] * len(graph)
visited[0] = True
ans = dfs(graph, visited, 0)
print(ans)


Error: 0

Fixed submission:

import sys

sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
graph = [set([]) for _ in range(M)]
for _ in range(M):
    a, b = map(int, input().split())
    graph[a - 1].add(b - 1)
    graph[b - 1].add(a - 1)


def dfs(graph, visited, v):
    if all(visited):
        return 1

    cnt = 0
    for u in graph[v]:
        if visited[u]:
            continue
        visited[u] = True
        cnt += dfs(graph, visited, u)
        visited[u] = False

    return cnt


visited = [False] * len(graph)
visited[0] = True
ans = dfs(graph, visited, 0)
print(ans)
Problem p03806 (Python) — buggy submission, verdict: Time Limit Exceeded

inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for a, b, c in ABC:
    for i in range(410, -1, -1):
        for j in range(410, -1, -1):
            if dp[i][j] < inf:
                dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
ans = inf
n = 1
while max(Ma * n, Mb * n) < 420:
    ans = min(ans, dp[Ma * n][Mb * n])
    n += 1
print(ans if ans < inf else -1)


Error: TLE

Fixed submission:

inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))


def cost(bits):
    sa = sb = sc = 0
    for i, (a, b, c) in enumerate(ABC):
        if not bits & 1 << i:
            continue
        sa += a
        sb += b
        sc += c
    return sc if sa * Mb == sb * Ma else inf


ans = min([cost(bits) for bits in range(1, 1 << N)])
print(ans if ans < inf else -1)