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

N,M = map(int, input().split())
full = 2**N
dp = [float("inf")]*full
dp[0] = 0

for m in range(M):
    a,b = map(int, input().split())
    C = list(map(int, input().split()))
    num = 0
    for c in C:
        num += 2**(c-1)
    for i in range(full):
        if dp[i]!=float("inf"):
            dp[i|num] = min(dp[i|num],dp[i]+a)
print(dp[-1] if dp[-1]!=float("inf") else -1)


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N,M = map(int,input().split())
    full = 2**N
    dp = [float("inf")]*full
    dp[0] = 0


    for m in range(M):
        a,b = map(int, input().split())
        C = list(map(int, input().split()))
        #鍵mで開く宝箱を二進法で表記
        state = 0
        for c in C:
            state += 2**(c-1)

        #状態iに対して鍵mを使い、状態i|stateにいく場合と、元から入っている場合の大小を比較
        #状態i|state即ち状態iに対して鍵mを使った後の状態
        for i in range(full):
            if dp[i] != float("inf"):
                dp[i|state] = min(dp[i]+a,dp[i|state])

    if dp[-1]==float("inf"):
    #dp[-1]はdp[full-1]でも同じ
        return -1

    return dp[-1]

print(main())

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
dp = [float("inf")]*(2**n)
dp[0] = 0
for M in range(m):
    a,b = map(int, input().split())
    C = list(map(int, input().split()))

    state = 0
    for c in C:
        state += 2**(c-1)

    for i in range(2**n):
        if dp[i] != float("inf"):
            dp[i|state] = min(dp[i]+a,dp[i|state])

if dp[-1]==float("inf"):
    print(-1)
else:
    print(dp[-1])


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N,M = map(int,input().split())
    full = 2**N
    dp = [float("inf")]*full
    dp[0] = 0


    for m in range(M):
        a,b = map(int, input().split())
        C = list(map(int, input().split()))
        #鍵mで開く宝箱を二進法で表記
        state = 0
        for c in C:
            state += 2**(c-1)

        #状態iに対して鍵mを使い、状態i|stateにいく場合と、元から入っている場合の大小を比較
        #状態i|state即ち状態iに対して鍵mを使った後の状態
        for i in range(full):
            if dp[i] != float("inf"):
                dp[i|state] = min(dp[i]+a,dp[i|state])

    if dp[-1]==float("inf"):
    #dp[-1]はdp[full-1]でも同じ
        return -1

    return dp[-1]

print(main())

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
dp = [float("inf")]*(2**n)
dp[0] = 0
for M in range(m):
    a,b = map(int, input().split())
    C = list(map(int, input().split()))

    state = 0
    for c in C:
        state += 2**(c-1)

    for i in range(2**n):
        if dp[i|state] != float("inf"):
            dp[i|state] = min(dp[i]+a,dp[i|state])
        else:
            dp[i|state] = dp[i]+a

if dp[-1]==float("inf"):
    print(-1)
else:
    print(dp[-1])


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N,M = map(int,input().split())
    full = 2**N
    dp = [float("inf")]*full
    dp[0] = 0


    for m in range(M):
        a,b = map(int, input().split())
        C = list(map(int, input().split()))
        #鍵mで開く宝箱を二進法で表記
        state = 0
        for c in C:
            state += 2**(c-1)

        #状態iに対して鍵mを使い、状態i|stateにいく場合と、元から入っている場合の大小を比較
        #状態i|state即ち状態iに対して鍵mを使った後の状態
        for i in range(full):
            if dp[i] != float("inf"):
                dp[i|state] = min(dp[i]+a,dp[i|state])

    if dp[-1]==float("inf"):
    #dp[-1]はdp[full-1]でも同じ
        return -1

    return dp[-1]

print(main())