Problem: 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.

Submission attempts (in order):
import numpy as np
import itertools
N, M=map(int, input().split())
a =[]
c = []
for i in range(2*M):
  if i%2 == 0:
    temp,_=map(int, input().split()) 
    a.append(temp)
  else:
    temp=list(map(int, input().split()))
    c.append(temp)
a = np.array(a) 
mini = -1
for n in range(M):
    for l in list(itertools.combinations(range(M),n+1)):
        temp = []
        for nn in l:
            temp = temp +c[nn]
        temp = set(temp)
        temp = list(temp)
        if len(temp) == N:
            cost = sum(a[[np.array(l)]])
            if mini == -1 or mini > cost:
                mini = cost


print(mini)  

import numpy as np
import itertools

def main():
    N, M=map(int, input().split())
    a =[]
    c = []
    for i in range(2*M):
      if i%2 == 0:
        temp,_=map(int, input().split()) 
        a.append(temp)
      else:
        temp=list(map(int, input().split()))
        c.append(temp)
    a = np.array(a) 

    row = 2**M 
    boo = np.array([[int(j[i]) for i in range(M)] for j in  [bin(i)[2:].zfill(M) for i in range(row)]])
    table = np.zeros((row,N), dtype = 'int8')

    for i in range(row):
        ind_temp = np.array(list(set(itertools.chain.from_iterable(c*boo[i]))),dtype = int) -1
        table[i][ind_temp] = 1

    ind = np.where(np.all(table, axis=1))
    price = np.array([np.sum(a*boo[ind], axis = 1)])

    if price:
        print(np.min(price))
    else:
        print(-1)


if __name__ == "__main__":
    main()



import numpy as np
import itertools
N, M=map(int, input().split())
a =[]
c = []
for i in range(2*M):
  if i%2 == 0:
    temp,_=map(int, input().split()) 
    a.append(temp)
  else:
    temp=list(map(int, input().split()))
    c.append(temp)
a = np.array(a) 
mini = -1
for n in range(M):
    for l in list(itertools.combinations(range(M),n+1)):
        temp = []
        for nn in range(len(l)):
            temp = temp +c[l[nn]]
        temp = set(temp)
        temp = list(temp)
        if len(temp) == N:
            cost = sum(a[[np.array(l)]])
            if mini == -1 or mini > cost:
                mini = cost


print(mini)  

import numpy as np
import itertools
N, M=map(int, input().split())
a =[]
c = []
for i in range(2*M):
  if i%2 == 0:
    temp,_=map(int, input().split()) 
    a.append(temp)
  else:
    temp=list(map(int, input().split()))
    c.append(temp)
a = np.array(a) 

row = 2**M 
boo = np.array([[int(j[i]) for i in range(M)] for j in  [bin(i)[2:].zfill(M) for i in range(row)]])
table = np.zeros((row,N), dtype = 'int8')

for i in range(row):
    ind_temp = np.array(list(set(itertools.chain.from_iterable(c*boo[i]))),dtype = int) -1
    table[i][ind_temp] = 1

ind = np.where(np.all(table, axis=1))
price = np.array([np.sum(a*boo[ind], axis = 1)])

if price.any():
    print(np.min(price))
else:
    print(-1)



import numpy as np
import itertools
N, M=map(int, input().split())
a =[]
c = []
for i in range(2*M):
  if i%2 == 0:
    temp,_=map(int, input().split()) 
    a.append(temp)
  else:
    temp=list(map(int, input().split()))
    c.append(temp)
a = np.array(a) 
mini = -1

for n in range(M):
    for l in list(itertools.combinations(range(M),n+1)):
        temp = [c[nn] for nn in l]
        temp = set(itertools.chain.from_iterable(temp))
        if len(temp) == N:
            cost = sum(a[[np.array(l)]])
            if mini == -1 or mini > cost:
                mini = cost

print(mini)  


import numpy as np
import itertools
N, M=map(int, input().split())
a =[]
c = []
for i in range(2*M):
  if i%2 == 0:
    temp,_=map(int, input().split()) 
    a.append(temp)
  else:
    temp=list(map(int, input().split()))
    c.append(temp)
a = np.array(a) 
mini = -1
for n in range(M):
    for l in list(itertools.combinations(range(M),n+1)):
        temp = []
        for nn in l:
            temp = temp +c[l[nn]]
        temp = set(temp)
        temp = list(temp)
        if len(temp) == N:
            cost = sum(a[[np.array(l)]])
            if mini == -1 or mini > cost:
                mini = cost


print(mini)  

import numpy as np
import itertools


N, M=map(int, input().split())
a =[]
c = []
for i in range(2*M):
  if i%2 == 0:
    temp,_=map(int, input().split()) 
    a.append(temp)
  else:
    temp=list(map(int, input().split()))
    c.append(temp)
a = np.array(a) 

row = 2**M 
con = [bin(i)[2:].zfill(M) for i in range(row)]
boo = np.array([[int(j[i]) for i in range(M)] for j in con])
table = np.zeros((row,N), dtype = 'int8')

for i in range(row):
    ind_temp = np.array(list(set(itertools.chain.from_iterable(c*boo[i]))),dtype = int) -1
    table[i][ind_temp] = 1

ind = np.where(np.all(table, axis=1))
price = np.array([np.sum(a*boo[ind], axis = 1)])
if price:
    print(np.min(price))
else:
    print(-1)

N, M = map(int, input().split())
Inf = 10**8
Dp = [Inf]*(2**N)
Dp[0] = 0

for i in range(M): 
    a,_=map(int, input().split())
    d = sum((1<<p-1) for p in list(map(int, input().split()))) 
    for j in range(2**N):
        ope = j|d
        Dp[ope] = min(Dp[ope], Dp[j] + a)

if Dp[-1] == Inf:
    print(-1)
else:
    print(Dp[-1])

Verdicts: Time Limit Exceeded

Runtime Error

Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Runtime Error

Runtime Error

Accepted