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):
n,m = map(int,input().split())
a_arr = []
b_arr = []
c_arr = []
 
dic = {0:0}
 
 
for i in range(m):
  a,b = map(int,input().split())
  a_arr.append(a)
  b_arr.append(b)
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for j in range(1, n+1):
    if j in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c_arr.append(int(''.join(arr),2))
  
  dic2 = dic.copy()
  
  for k,v in dic.items():
    if (k|c) in dic.keys():
      dic2[k|c] = min(dic2[k|c],a+v)
    else:
      dic2[k|c] = a+v
 
  dic = dic2
 
if ((2**n)-1) in dic.keys():
  print(dic[(2**n) -1])
else:
  print(-1)

import sys
 
n,m = map(int,input().split())
a_arr = []
b_arr = []
c_arr = []
 
dic_arr = [{i:sys.maxsize for i in range(2 ** n)}]
dic_arr[0][0] = 0
 
for i in range(m):
  a,b = map(int,input().split())
  a_arr.append(a)
  b_arr.append(b)
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for i in range(1, n+1):
    if i in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c = int(''.join(arr),2)
  
  dic_arr.append(dic_arr[i].copy())
  
  for k,v in dic_arr[i].items():
    dic_arr[i+1][k|c] = min(dic_arr[i+1][k|c],a+v)  
 
if dic_arr[m][(2**n)-1] != sys.maxsize:
  print(dic_arr[m][(2**n) -1])
else:
  print(-1)

import sys

n,m = map(int,input().split())
a_arr = []
b_arr = []
c_arr = []

dic_arr = [{}]
dic_arr[0][0] = 0

for i in range(m):
  a,b = map(int,input().split())
  a_arr.append(a)
  b_arr.append(b)
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for i in range(1, n+1):
    if i in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c_arr.append(int(''.join(arr),2))

for i in range(m):
  a = a_arr[i]
  b = b_arr[i]
  c = c_arr[i]
  
  dic_arr.append(dic_arr[i].copy())
  
  for k,v in dic_arr[i+1].items():
    if (k|c) in dic_arr[i].keys():
      dic_arr[i+1][k|c] = min(dic_arr[i][k|c],a+v)
    else:
      dic_arr[i+1][k|c] = a+v

if ((2**n)-1) in dic_arr[m]:
  print(dic_arr[m][(2**n) -1])
else:
  print(-1)

print(-1)

n,m = map(int,input().split())
 
dic = {0:0}
 
for i in range(m):
  a,b = map(int,input().split())
  a_arr.append(a)
  b_arr.append(b)
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for j in range(1, n+1):
    if j in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c = int(''.join(arr),2)
  
  dic2 = dic.copy()
  
  for k,v in dic.items():
    if (k|c) in dic2.keys():
      dic2[k|c] = min(dic2[k|c],a+v)
    else:
      dic2[k|c] = a+v
 
  dic = dic2
 
if ((2**n)-1) in dic.keys():
  print(dic[(2**n) -1])
else:
  print(-1)

import sys

n,m = map(int,input().split())
a_arr = []
b_arr = []
c_arr = []

dic_arr = [{}]
dic_arr[0][0] = 0

for i in range(m):
  a,b = map(int,input().split())
  a_arr.append(a)
  b_arr.append(b)
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for i in range(1, n+1):
    if i in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c_arr.append(int(''.join(arr),2))

for i in range(m):
  a = a_arr[i]
  b = b_arr[i]
  c = c_arr[i]
  
  dic_arr.append(dic_arr[i].copy())
  
  for k,v in dic_arr[i].items():
    if (k|c) in dic_arr[i].keys():
      dic_arr[i+1][k|c] = min(dic_arr[i][k|c],a+v)
    else:
      dic_arr[i+1][k|c] = a+v

if ((2**n)-1) in dic_arr[m]:
  print(dic_arr[m+1][(2**n) -1])
else:
  print(-1)

n,m = map(int,input().split())
 
dic = {0:0}
 
for i in range(m):
  a,b = map(int,input().split())
  tmp_arr = list(map(int, input().split()))
  
  arr = []
  for j in range(1, n+1):
    if j in tmp_arr:
      arr.append('1')
    else:
      arr.append('0')
  
  c = int(''.join(arr),2)
  
  dic2 = dic.copy()
  
  for k,v in dic.items():
    if (k|c) in dic2.keys():
      dic2[k|c] = min(dic2[k|c],a+v)
    else:
      dic2[k|c] = a+v
 
  dic = dic2
 
if ((2**n)-1) in dic.keys():
  print(dic[(2**n) -1])
else:
  print(-1)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Accepted