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):
N, M = map(int, input().split())

costs = [None for i in range(M)]
opens = [None for i in range(M)]

for i in range(M):
    costs[i] = list(map(int,input().split()))[0]
    o = list(map(int, input().split()))
    x = pow(2, 12)-1
    for item in o:
        x &= ~(1 << (item-1))
    opens[i] = x

dp = [-1 for i in range(pow(2, 12))]
dp[0] = 0

def recurse(required):
    if dp[required] != -1:
        return dp[required]

    totalCost = float('inf')
    for i in range(M):
        c = required & opens[i]
        if c == required:continue
        
        totalCost = min(totalCost, costs[i] + recurse(c))

    dp[required] = totalCost
    return totalCost


val = recurse(pow(2, N)-1)
if val == float('inf'):
    print(-1)
else:
    print(val)

Solution (Python):
n,m=map(int,input().split())
ab=[]
c=[]

for i in range(m):
    ab.append(list(map(int,input().split())))
    c.append(list(map(int,input().split())))
    
for i in range(m):
    temp=0
    for j in range(len(c[i])):
        temp+=2**(c[i][j]-1)
    c[i]=temp
    
   
dp=dict()

for i in range(2**n):
    dp[i]=10**9
    
    
dp[0]=0
    

for i in range(m):
    for k,item in dp.items():
        dp[c[i]|k]=min(dp[c[i]|k],dp[k]+ab[i][0])

if dp[2**n-1]==10**9:
    print(-1)
else:
    print(dp[2**n-1])

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
l = [None] * m
li = [None] * m
for i in range(m):
    l[i] = list(map(int, input().split()))
    li[i] = list(map(int, input().split()))
dp = [[float('inf')] * (2 ** n) for _ in range(m + 1)]
dp[0][0] = 0
for i in range(m):
    for j in range(2 ** n):
        if dp[i][j] == float('inf'):
            continue
        tmp = j
        for x in li[i]:
            tmp |= 2 ** (x - 1)
        dp[i + 1][tmp] = min(dp[i + 1][j | tmp], dp[i][j] + l[i][0])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
res = dp[m][2 ** n - 1]
if res == float('inf'):
    print(-1)
else:
    print(res)

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

A = []
C = []
for i in range(M):
    a,b = list(map(int,input().split()))
    c = list(map(int,input().split()))
    A.append(a)
    d = 0
    for j in range(b):
        d += pow(2, c[j]-1)
    C.append(d)

goal = pow(2, N)-1
e_list = [[] for i in range(goal+1)]
for i in range(goal+1):
    for j in range(M):
        e_list[i].append((i|C[j],A[j]))



import heapq
vi = 0
min_d_list = [10**27 for i in range(goal+1)]
min_d_list[vi] = 0
q = []
for i in range(N):
    heapq.heappush(q,[min_d_list[i],i])

while len(q)>0:
    d,v = heapq.heappop(q)
    for e in e_list[v]:
        v1,d1=e
        if min_d_list[v1]>d+d1:
            min_d_list[v1]=d+d1
            heapq.heappush(q,[d+d1,v1])

if min_d_list[goal]>=10**27:
    print(-1)
else:
    print(min_d_list[goal])

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

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

INF = 10**18
dp = [INF] * (1<<N)
dp[0] = 0

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

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

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

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

ma = 1<<n
DP = [float("inf")]*ma
DP[0] = 0
for i in range(m):
    nxt = DP[::]
    a,b = map(int, input().split())
    mask = 0
    for c in map(int, input().split()):
        mask += 1<<(c-1)
    for j in range(ma):
        if nxt[j|mask] > DP[j] + a:
            nxt[j|mask] = DP[j] + a
    DP = nxt

ans = DP[-1]
if ans == float("inf"):
    ans = -1
print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


n, m = li()
inf = float('inf')

a = []
b = []
c = []
for _ in range(m):
    ai, bi = li()
    ci = list(li_())
    a.append(ai)
    b.append(bi)
    state = 0
    for cij in ci:
        state += pow(2, cij)
    c.append(state)


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

for state in range(1<<n):
    for i in range(m):
        dp[state | c[i]] = min(dp[state] + a[i], dp[state | c[i]])

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

Solution (Python):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
e=[(int(input().split()[0]),list(map(int,input().split())))for _ in range(m)]
INF=10**18
dp=[INF]*2**n
dp[0]=0
for i in range(2**n):
    if dp[i]>=INF:continue
    for a,c in e:
        bit=i
        for j in c:
            bit|=2**(n-j)
        dp[bit]=min(dp[bit],dp[i]+a)
a=dp[-1]
if a>=INF:
    print(-1)
else:
    print(dp[-1])

Solution (Python):
def main():
    N, M = map(int, input().split())
    X = 2**N
    dp = [float("inf")] * X
    dp[0] = 0
    for i in range(M):
        a, b = map(int, input().split())
        c = list(map(int, input().split()))
        bit = 0
        for item in c:
            bit |= 2**(item-1)
        for j in range(X):
            dp[j | bit] = min(dp[j | bit], dp[j] + a)
    if dp[X-1] == float("inf"):
        print(-1)
    else:
        print(dp[X-1])


if __name__ == "__main__":
    main()

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, m = na()
a = []
pts = []
for i in range(m):
    la, lb = na()
    a.append(la)
    pt = 0
    for x in na():
        pt |= 1<<x-1
    pts.append(pt)

price = [99999999999999999] * (1<<n)
for i in range(m):
    price[pts[i]] = min(price[pts[i]], a[i])

dp = [9999999999999999] * (1<<n)
dp[0] = 0
for i in range(1<<n):
    if price[i] <= 100000:
        for j in range(1<<n):
            dp[j|i] = min(dp[j|i], dp[j] + price[i])
if dp[(1<<n)-1] >= 100000000:
    print(-1)
else:
    print(dp[(1<<n)-1])

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

n,m = [int(i) for i in readline().split()]

ALL = 1<<n
dp = [10**9]*ALL
dp[0] = 0
for i in range(m):
    ndp = dp[:]
    a,b = [int(i) for i in readline().split()]
    c = [int(i)-1 for i in readline().split()]
    
    C = 0
    for i in c:
        C |= 1<<i
    
    for S in range(ALL):
        ndp[S|C] = min(ndp[S|C], dp[S] + a)
    
    dp = ndp




if dp[-1] == 10**9: print(-1)
else: print(dp[-1])

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n = I()
    print((n-(n>>1))/n)
    return


def B():
    n,k = LI()
    h = LI()
    ans = 0
    for i in h:
        if i >= k:
            ans += 1
    print(ans)
    return


def C():
    n = I()
    a = LI()
    b = [(a[i],i+1) for i in range(n)]
    b.sort()
    ans = [b[i][1] for i in range(n)]
    print(*ans)
    return


def D():
    def divisor(n):
        if n < 4:
            return set([n])
        res = set([1])
        i = 2
        m = n
        while i**2 <= n:
            if m%i == 0:
                while m%i == 0:
                    m //= i
                res.add(i)
            i += 1
        res.add(m)
        return res
    a,b = LI()
    if min(a,b) == 1:
        print(1)
        return
    s = divisor(a)
    t = divisor(b)
    s &= t
    print(len(s))
    return


def E():
    n,m = LI()
    v = []
    for i in range(m):
        a,b = LI()
        c = LI()
        s = 0
        for j in c:
            s |= 1<<(j-1)
        v.append((a,s))
    l = 1<<n
    dp = [[float("inf")]*l for i in range(m+1)]
    dp[0][0] = 0
    for i in range(m):
        ni = i+1
        a,c = v[i]
        for b in range(l):
            if dp[i][b] == float("inf"):
                continue
            nb = b|c
            nd = dp[i][b]+a
            if nd < dp[ni][nb]:
                dp[ni][nb] = nd
            nd = dp[i][b]
            if nd < dp[ni][b]:
                dp[ni][b] = nd
    if dp[m][-1] == float("inf"):
        print(-1)
    else:
        print(dp[m][-1])
    return


def F():

    return


if __name__ == "__main__":
    E()