Problem:
 Score : 500 points    Problem Statement We have N locked treasure boxes, numbered 1 to N .  A shop sells M keys. The i -th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1} , c_{i2} , ... , c_{i{b_i}} . Each key purchased can be used any number of times.  Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1 .      Constraints  All values in input are integers.  1 \leq N \leq 12  1 \leq M \leq 10^3  1 \leq a_i \leq 10^5  1 \leq b_i \leq N  1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N         Input Input is given from Standard Input in the following format:  N  M  a_1  b_1  c_{11}  c_{12}  ...  c_{1{b_1}}  :  a_M  b_M  c_{M1}  c_{M2}  ...  c_{M{b_M}}       Output Print the minimum cost required to unlock all the treasure boxes.
If it is impossible to unlock all of them, print -1 .        Sample Input 1 2 3
10 1
1
15 1
2
30 2
1 2      Sample Output 1 25  We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.       Sample Input 2 12 1
100000 1
2      Sample Output 2 -1  We cannot unlock all the boxes.       Sample Input 3 4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3      Sample Output 3 69942

Solution (Python):
def main(sample_file = ''):

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter 
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)
    def make_divisors(n, reversed=False):
        """create list of divisors
        
        Args:
            number (int): number from which list of divisors is created
            reversed (bool, optional): ascending order if False. Defaults to False.
        
        Returns:
            list: list of divisors
        """
        divisors = set()
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divisors.add(i)
                divisors.add(n//i)
        return sorted(list(divisors),reverse=reversed)
    
    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    
    
    
    
    

    def BellmanFord(N, M, ABC, vertex_start, vertex_end, value_if_inf = -1, find_shortest = False):
        """to calculate furthest or shortest length between vertex_start and vertex_end using BellmanFord algorithm
        
        Args:
            N (int): number of vertices
            M (int): number of edges
            ABC (list): [(ai, bi, ci) for _ in range(N)] where i-th edge is directed from vertex ai to vertex bi and the length is ci 
            vertex_start (int): start vertex. usually use 0.
            vertex_end (int): end vertex. usually use N-1.
            value_if_inf (int or string as you like, optional): value you want when the furthest (or shortest) distance is infinite (or -infinite). Defaults to -1.
            find_shortest (bool, optional): choose False to find furthest path. Defaults to False.
        
        Returns:
            int or string: normally int (but can be str if you set value_if_inf to str)
            
        Example:
                N, M, P = R()
                ABC = [R() for _ in range(M)]
                ABC = [(a-1, b-1, c-P) for a, b, c in ABC]
                print(BellmanFord(N, M, ABC, 0, N-1, value_if_inf = 'inf'))

        """

        def make_reachable_list(N, M, ABC, vertex_start, vertex_end):
            reachable_to_direct = defaultdict(list)
            reachable_from_direct = defaultdict(list)
            reachable_from_start = [False] * N
            reachable_to_end = [False] * N
            reachable_from_start[vertex_start] = True
            reachable_to_end[vertex_end] = True
            reachable_from_both_sides = [False] * N
            dfs_from_start = []
            dfs_to_end = []
            for a, b, c in ABC:
                reachable_to_direct[a].append(b)
                reachable_from_direct[b].append(a)
                if a == vertex_start:
                    dfs_from_start.append(b)
                    reachable_from_start[b] = True
                if b == vertex_end:
                    dfs_to_end.append(a)
                    reachable_to_end[a] = True
            while dfs_from_start:
                v = dfs_from_start.pop()
                for i in reachable_to_direct[v]:
                    if not reachable_from_start[i]:
                        reachable_from_start[i] = True
                        dfs_from_start.append(i)
            while dfs_to_end:
                v = dfs_to_end.pop()
                for i in reachable_from_direct[v]:
                    if not reachable_to_end[i]:
                        reachable_to_end[i] = True
                        dfs_to_end.append(i)
            for i in range(N):
                reachable_from_both_sides[i] = reachable_from_start[i] and reachable_to_end[i]
            return reachable_from_both_sides
        
        reachable_from_both_sides = make_reachable_list(N, M, ABC, vertex_start, vertex_end)
        
        if find_shortest:
            dist = [INF for i in range(N)]
        else:
            dist = [-INF for i in range(N)]
        
        dist[vertex_start] = 0
        for i in range(N):
            updated = False
            for a, b, c in ABC:
                if not reachable_from_both_sides[a]:
                    continue
                elif find_shortest:
                    update_condition = dist[a] + c < dist[b]
                else:
                    update_condition = dist[a] + c > dist[b]
                if dist[a] != INF and update_condition:
                    dist[b] = dist[a] + c
                    updated = True
                    if i == N-1:
                        return value_if_inf
            if not updated:
                break
        return dist[vertex_end]

    """ initialize variables and set inputs
    
        
        
            
            
    
        
        
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    r_float = lambda: float(r())
    R = lambda: list(map(int, r().split()))
    R_map = lambda: map(int, r().split())
    R_float = lambda: list(map(float, r().split()))
    R_tuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    
    if sample_file:
        sys.stdin = open(sample_file)
        
    
    

    N, M = R()
    dp = [[INF]*(M+1) for _ in range(2**(N+1))]
    dp[0][0] = 0
    for m in range(M):
        a, b = R()
        C = R()
        i = 0
        for c in C:
            i += 2**(c-1)
        for j in range(2**N+1):
            dp[j][m+1] = min(dp[(i|j)-i][m]+a, dp[j][m])
    if dp[2**N-1][M] == 1000000000000000000:
        print(-1)
    else:
        print(dp[2**N-1][M])

    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """
    
    """memo: how to solve the problem
    creating simple test/answer
    greed
    simple dp
    graph
    """

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import combinations
def input():
	return sys.stdin.readline()[:-1]
n, m = map(int, input().split())
keys = []
for _ in range(m):
	a, b = map(int, input().split())
	c = list(map(int, input().split()))
	keys.append([a, sum([1<<(x-1) for x in c])])
dp = [10**30 for _ in range(1<<n)]
dp[0] = 0

for c, v in keys:
	dp[v] = dp[0]+c

for j in range(1<<n):
	for c, v in keys:
		dp[j|v] = min(dp[j|v], dp[j]+c)

if dp[(1<<n)-1] > 10**20:
	print(-1)
else:
	print(dp[(1<<n)-1])

Solution (Python):
N, M = map(int, input().split())
INF = 1001001001
key = []

for i in range(M):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    s = 0
    for j in range(b):
        c[j] -= 1
        s |= 1 << c[j]
    key.append([s, a])

dp = [INF] * (1<<N)
dp[0] = 0
for s in range(1<<N):
    for i in range(M):
        t = s | key[i][0]
        cost = dp[s] + key[i][1]
        dp[t] = min(dp[t], cost)

if dp[-1] == INF:
    print(-1)
else:
    print(dp[-1])

Solution (Python):
N,M = map(int,input().split())
Data = []
for _ in range(M):
    a,_ = map(int,input().split())
    keys = list(map(int,input().split()))
    pat = 0
    for k in keys:
        pat += 2**(k-1)
    Data.append([a,pat])

DP = [-1]*(2**N) 
DP[0] = 0
for i in range(2**N):
    if DP[i] == -1:
        continue
    for j in range(M):
        cost  = Data[j][0]
        pat   = Data[j][1]
        t = i | pat
        if DP[t] == -1 or DP[t] > DP[i]+cost:
            DP[t] = DP[i]+cost
print(DP[2**N-1])

Solution (Python):
INF = 10**8

N, M = map(int, input().split())
n = 2 ** N
dp = [0] + [INF]*(n - 1)

for _ in range(M):
  a, b = map(int, input().split())
  c = sum(map(lambda x:2**(x-1), list(map(int, input().split()))))
  for i in range(n):
    dp[i | c] = min(dp[i] + a, dp[i | c])
  
if dp[-1] == INF:
  print(-1)
else:
  print(dp[-1])

Solution (Python):
n, m = map(int, input().split())

cost = []
key = []

for _ in range(m):
    k = ["0"] * n
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    for i in c:
        k[i - 1] = "1"

    k = int("".join(k), base=2)
    cost.append(a)
    key.append(k)


inf = 10**10
dp = [inf] * (1 << n)
dp[0] = 0

for i in range(1 << n):
    if dp[i] == inf:
        continue
    for a, k in zip(cost, key):
        m = k | i
        dp[m] = min(dp[m], dp[i] + a)

ans = -1 if dp[-1] == inf else dp[-1]
print(ans)

Solution (Python):
N,M=map(int,input().split())
A=[0]*M
keys=[0]*M
for i in range(M):
  a,b=map(int,input().split())
  A[i]=a
  c=list(map(int,input().split()))
  for j in range(len(c)):
    keys[i]|=1<<(c[j]-1)
    
INF=10**9+1
dp=[INF]*(2**N)
dp[0]=0
for i in range(len(keys)):
  for j in range(len(dp)-1,-1,-1):
    if dp[j]!=INF:
      dp[j|keys[i]]=min(dp[j|keys[i]],dp[j]+A[i])
      
print((-1,dp[-1])[dp[-1]!=INF])

Solution (Python):
N,M=map(int,input().split())

price=[0]*M
keys=[0]*M
for i in range(M):
  a,b=map(int,input().split())
  price[i]=a
  c=list(map(int,input().split()))
  k=0
  for j in range(len(c)):
    k|=1<<(c[j]-1)
  keys[i]=k
  
INF=((10**5)*12+1)
dp=[INF]*(2**N)
dp[0]=0
for i in range(len(keys)):
  for j in range(len(dp)-1,-1,-1):
    if dp[j]!=INF:
      dp[j|keys[i]]=min(dp[j|keys[i]],dp[j]+price[i])

if dp[-1]==INF:
  print(-1)
else:
  print(dp[-1])

Solution (Python):
N, M = map(int, input().split())
A = []
B = []
C = [0]*M

for i in range(M):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    for c in map(int, input().split()):
        C[i] += 1<<(c-1)

inf = float("inf")

dp = [[inf]*(1<<N) for _ in range(M+1)]
dp[0][0] = 0

for i in range(M):
    for j in range(1<<N):
        if dp[i+1][j] > dp[i][j]:
            dp[i+1][j] = dp[i][j]
        if dp[i+1][j | C[i]] > dp[i][j] + A[i]:
            dp[i+1][j | C[i]] = dp[i][j] + A[i]

if dp[M][(1<<N)-1] < inf:
    print(dp[M][(1<<N)-1])
else:
    print(-1)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    N=2**n
    K=[None]*m
    for i in range(m):
        a,b=map(int,input().split())
        key=0
        for c in map(lambda x:int(x)-1,input().split()):
            key+=(1<<c)
        K[i]=(key,a)

    dp=[INF]*N
    dp[0]=0
    for key,cost in K:
        ndp=dp[:]
        for S in range(N):
            ndp[S|key]=min(ndp[S|key],dp[S]+cost)
        dp=ndp

    print(dp[-1] if(dp[-1]!=INF) else -1)
resolve()

Solution (Python):
import collections
N, M = map(int, input().split())
keys = collections.defaultdict(lambda:float('inf'))
for _ in range(M):
    cost, _ = map(int, input().split())
    key_set = 0
    for i in map(int, input().split()):
        key_set |= (1 << (i -1))
    keys[key_set] = min(keys[key_set], cost)

allstate = (1 << N) - 1
dp = collections.defaultdict(lambda:float('inf'))
dp[0] = 0

for key_set, cost in keys.items():
    ndp = collections.defaultdict(lambda:float('inf'))
    for state, val in dp.items():
        ndp[state] = min(ndp[state], val)
        ndp[state|key_set] = min(ndp[state|key_set], val + cost)
    dp = ndp

if dp[allstate] != float('inf'):
    print(dp[allstate])
else:
    print(-1)

Solution (Python):
N, M = map(int, input().split())
keys = []

for _ in range(M):
    cost, _ = map(int, input().split())
    mask = 0
    for i in map(int, input().split()):
        mask |= (1 << (i - 1))
    keys.append((cost, mask))


allstate = (1 << N) - 1
dp = [float('inf')] * (1<<N)
dp[0] = 0

for cost, key_set in keys:
    for state in range(1<<N):
        if dp[state] == float('inf'): continue
        nstate = state | key_set
        dp[nstate] = min(dp[nstate], dp[state] + cost)

print(dp[-1] if dp[-1] != float('inf') else -1)

Solution (Python):
INF = 1001001001

N, M = map(int, input().split())
key = []
for i in range(M):
    a, b = map(int, input().split())
    s = 0
    c = list(map(int, input().split()))
    for j in range(b):
        c[j] -= 1
        s |= 1<<c[j]
    key.append([s, a])
dp = [INF] * (1<<N)
dp[0] = 0
for s in range(1<<N):
    for i in range(M):
        t = s | key[i][0]
        cost = dp[s] + key[i][1]
        dp[t] = min(dp[t], cost)
ans = dp[-1]
if ans == INF:
    ans = -1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

N,M = map(int,input().split())
AB = []
C = []
for _ in range(M):
    AB.append(tuple(int(i) for i in input().split()))
    C.append(sum(1<<(int(i)-1) for i in input().split()))

INF = 10**18



dp = [INF] * (1<<N)


dp[0] = 0

for i in range(1<<N):
    for (a,b),c in zip(AB,C):
        
        state = i | c
        dp[state] = min(dp[state], dp[i]+a)

answer = dp[-1]
if answer == INF:
    answer = -1
print(answer)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N,M = map(int,input().split())
AB = []
C = []
for _ in range(M):
    AB.append(tuple(int(i) for i in input().split()))
    C.append(sum(1<<(int(i)-1) for i in input().split()))

INF = 10**18



dp = [INF] * (1<<N)


dp[0] = 0

for i in range(1<<N):
    for (a,b),c in zip(AB,C):
        
        state = i | c
        dp[state] = min(dp[state], dp[i]+a)

answer = dp[-1]
if answer == INF:
    answer = -1
print(answer)

Solution (Python):
from collections import deque
n , m = map(int, input().split())
keys = deque()
def trans(k):
    res = [0 for i in range(n)]
    for i in k:
        res[-i] = 1
    res = int("".join(map(str, res)), 2)
    return res

for i in range(m):
    a, b = list(map(int, input().split()))
    c = deque(list(map(int, input().split())))
    c.appendleft(a)
    keys.append(c)
inf = 10 ** 8
dp = [inf for i in range(2**n)]
dp[0] = 0
for i in range(m):
    key_value = keys[i].popleft()
    can_open = keys[i]
    p = trans(can_open)
    for j in range(2**n):
        next = j | p
        dp[next] = min(dp[next], dp[j] + key_value)

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

Solution (Python):
def main():
    import math
    N,M=map(int,input().split())
    a=[]
    for i in range(M):
        tmp=list(map(int,input().split()))
        tmpc=list(map(int,input().split()))
        c=0
        for j in range(len(tmpc)):
            c+=2**(tmpc[j]-1)
        tmp[1]=c
        a+=[tmp]


    dp=[[10**10 for j in range(2**N)] for i in range(M)]
    dp[0][0]=0
    dp[0][a[0][1]]=a[0][0]


    for i in range(1,M):
        dp[i]=dp[i-1]
        for j in range(2**N):
            z,p=a[i][0],a[i][1]
            q=j|p
            x=dp[i-1][j]+z
            if dp[i][q]>x: dp[i][q]=x

    ans=dp[M-1][2**N-1]
    print(ans if ans<10**10 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    dp=[10**8 for i in range(L)]
    dp[0]=0

    for _ in range(M):
        a, _ = map(int, input().split())
        key=sum([2**(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                x=dp[j]+a
                if dp[q]> x:
                    dp[q]=x


    ans=dp[L-1]
    print(ans if ans<10**8 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    dp=[10**8 for i in range(L)]
    dp[0]=0


    for _ in range(M):
        a, _ = map(int, input().split())
        key=sum([2**(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                dp[q]=min(dp[j]+a,dp[q])


    ans=dp[L-1]
    print(ans if ans<10**8 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    inf=10**8
    dp=[inf for i in range(L)]
    dp[0]=0


    for i in range(M):
        a, _ = map(int, input().split())
        key=sum([2**(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                dp[q]=min(dp[j]+a,dp[q])


    ans=dp[L-1]
    print(ans if ans<inf else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    dp=[10**8 for i in range(L)]
    dp[0]=0


    for i in range(M):
        a, _ = map(int, input().split())
        key=sum([2**(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                dp[q]=min(dp[j]+a,dp[q])


    ans=dp[L-1]
    print(ans if ans<10**8 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, m = map(int, input().split())
    p = 2**n
    inf = 10**7
    dp = [inf]*p
    dp[0] = 0
    for _ in range(m):
        a, _ = map(int, input().split())
        c = sum([1<<(int(i)-1) for i in input().split()])
        for s in range(p):
                t = s|c
                new = dp[s] + a
                if dp[t] > new:
                    dp[t] = new
    ans = dp[-1]
    if ans == inf:
        ans = -1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    dp=[10**7 for i in range(L)]
    dp[0]=0


    for i in range(M):
        a, _ = map(int, input().split())
        key=sum([2**(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                dp[q]=min(dp[j]+a,dp[q])


    ans=dp[L-1]
    print(ans if ans<10**7 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    dp=[10**7 for i in range(L)]
    dp[0]=0


    for i in range(M):
        a, _ = map(int, input().split())
        key=sum([2**(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                dp[q]=min(dp[j]+a,dp[q])


    ans=dp[L-1]
    print(ans if ans<10**7 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    dp=[10**10 for i in range(L)]
    dp[0]=0


    for i in range(M):
        a, _ = map(int, input().split())
        key=sum([1<<(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                dp[q]=min(dp[j]+a,dp[q])


    ans=dp[L-1]
    print(ans if ans<10**10 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N,M=map(int,input().split())
    L=2**N
    dp=[10**10 for i in range(L)]
    dp[0]=0


    for i in range(M):
        a, _ = map(int, input().split())
        key=sum([2**(int(i)-1) for i in input().split()])
        for j in range(L):
                q=j|key
                dp[q]=min(dp[j]+a,dp[q])


    ans=dp[L-1]
    print(ans if ans<10**10 else -1)

if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
dp = [float('inf')]*(1<<n)
dp[0] = 0
for i in range(m):
    c = 0
    a, b = map(int, input().split())
    for j in list(map(int, input().split())):
        c |= 1<<(j-1)
    for k in range(1<<n):
        dp[c|k] = min(dp[c|k], dp[k]+a)
if dp[-1] == float('inf'):
    print(-1)
else:
    print(dp[-1])

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    p = 2 ** n
    ls = [10 ** 15] * p
    ls[0] = 0
    for i in range(m):
        a, b = list(map(int, readline().split()))
        t = sum([2 ** (x - 1) for x in list(map(int, readline().split()))])
        for j in range(p):
            ls[j | t] = min(ls[j | t], ls[j] + a)
    print(ls[-1] if ls[-1] != 10 ** 15 else -1)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int, input().split())

dp = [float('inf')]*(1<<n)
dp[0] = 0

for i in range(m):
  a, b = map(int, input().split())
  c = 0
  for j in list(map(int, input().split())):
    c += 1<<(j-1)
  
  for k in range(1<<n):
    dp[k|c] = min(dp[k|c], dp[k]+a)
    
if dp[-1] == float('inf'):
  print(-1)
else:
  print(dp[-1])

Solution (Python):
n,m = map(int,input().split())
INF = 10 ** 9
cur = [INF] * (2**n)
cur[0] = 0
for i in range(m):
  a,b = map(int,input().split())
  c = 0
  for j in map(int,input().split()):
    c += 2**(j-1)
  pre,cur = cur,[INF]*(2**n)
  for j in range(2**n):
    cur[j] = min(pre[j],cur[j])
    jc = j|c
    cur[jc] = min(pre[j]+a,cur[jc])
if cur[-1] == INF:
  print(-1)
else:
  print(cur[-1])

Solution (Python):
n, m = map(int, input().split())

x = [None for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    c = map(int, input().split())
    t = sum(map(lambda x: 2 ** (x - 1), c))
    x[i] = (a, t)

INF = 10 ** 9
dp = [INF for _ in range(2 ** n)]
dp[0] = 0

for ai, ti in x:
    for j in range(2 ** n):
        dp[ti | j] = min(dp[ti | j], dp[j] + ai)

if dp[-1] >= INF:
    ans = -1
else:
    ans = dp[-1]
print(ans)

Solution (Python):
n, m = map(int, input().split())

x = [None for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    c = map(int, input().split())
    t = sum(map(lambda x: 2 ** (x - 1), c))
    x[i] = (a, t)

INF = 10 ** 9
dp = [INF for _ in range(2 ** n)]
dp[0] = 0

for ai, ti in x:
    for j in range(2 ** n):
        dp[ti | j] = min(dp[ti | j], dp[j] + ai)

if dp[-1] >= INF:
    ans = -1
else:
    ans = dp[-1]
print(ans)

Solution (Python):
N,M = map(int,input().split())
key_list = []
for i in range(M):
  a,b = map(int,input().split())
  bin_int = 0
  c = list(map(int,input().split()))
  for C in c:
    bin_int += 2 ** (C - 1)
  key_list.append([a, bin_int])

n1 = 2 ** N
dp = [[120001 for i in range(n1)] for j in range(M + 1)]
dp[0][0] = 0

for i in range(M):
  dp_i = dp[i]
  dp_i1 = dp[i + 1]
  key = key_list[i]

  for j in range(n1):
    dp_key = dp_i[j]
    dp_i1[j] = min(dp_key, dp_i1[j])
    dp_i1[j | key[1]] = min(dp_key + key[0], dp_i1[j | key[1]])
  
ans = dp[M - 1][n1 - 1]
if ans == 120001:
  print(-1)
else:
  print(dp[M - 1][n1 - 1])

Solution (Python):
N, M = map(int, input().split())
key = []
for i in range(M):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    t = 0
    for j in c:
        t |= 1 << (j - 1)  
    key.append([t, a])  

INF = 10 ** 12
dp = [INF] * (1 << N)  
dp[0] = 0  
for i in range(1 << N):
    for j in range(M):
        T = i | key[j][0]  
        cost = dp[i] + key[j][1]
        dp[T] = min(dp[T], cost)

ans = dp[-1]
if ans == INF:
    ans = -1
print(ans)

Solution (Python):
N, M = map(int, input().split())
b_cnt = 2 ** N 
INF = 10 ** 9
dp = [INF] * b_cnt
dp[0] = 0

for i in range(M):
    a, b = map(int, input().split())
    c = sum([2 ** (int(x)-1) for x in list(input().split())])
    for j in range(b_cnt):
        if dp[j|c] > dp[j] + a:
            dp[j|c] = dp[j] + a

if dp[-1] == INF:
    print(-1)
else:
    print(dp[-1])

Solution (Python):
def main():
    N, M = map(int, input().split())
    b_cnt = 2 ** N 
    INF = 10 ** 9
    dp = [INF] * b_cnt
    dp[0] = 0

    for i in range(M):
        a, b = map(int, input().split())
        c = sum([2 ** (int(x)-1) for x in list(input().split())])
        for j in range(b_cnt):
            if dp[j|c] > dp[j] + a:
                dp[j|c] = dp[j] + a

    if dp[-1] == INF:
        print(-1)
    else:
        print(dp[-1])

if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int,input().split())
a=[]
k=[]
for _ in range(m):
  a.append(list(map(int,input().split()))[0])
  k.append(sum(2**(int(i)-1)for i in input().split()))
inf=float('inf')
dp=2**n*[inf]
dp[0]=0
for i in range(2**n):
  for j in range(m):
    dp[i|k[j]]=min(dp[i|k[j]],dp[i]+a[j])
print(dp[-1]if inf!=dp[-1]else-1)