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



Sample input:
2 3
10 1
1
15 1
2
30 2
1 2


Sample output:
25

Submission status: Time Limit Exceeded

Solution code:
# 所持している鍵の集合をbitで表現する
#　鍵の集合を表す2進数が少なくなる方向に遷移することはないから，for文for文でかける

N, M = map(int, input().split())
status_money = [float('inf')] * (1 << N)

# 初期状態からの遷移を記録
for _ in range(M) :
    a, b = map(int, input().split())
    C = 0
    for c in input().split() :
        C |= (1 << int(c) - 1)
    status_money[C] = min(status_money[C], a)
    
# ある状態とある状態の組合せを全部取る
for c in range(1 << N):
    # どんな鍵の組合せでも到達できない宝箱の組合せがあるかもしれない
    if status_money[c] == float('inf'):
        continue
    for d in range(1 << N):
        # c と d の論理和をとって新しく生成される状態 e を考える
        e = c | d
        # eにあらかじめ格納されている最小コストと，今生成したコストとのminをとる
        status_money[e] = min(status_money[c] + status_money[d], status_money[e])

# そもそも到達できない場合の処理を記述       
if status_money[-1] == float('inf'):
    print(-1)
else:
    print(status_money[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    a = []
    b = []
    c = []
    for i in range(M):
        a_, b_ = map(int, input().split())
        a.append(a_)
        b.append(b_)
        c_ = np.array(list(map(int, input().split()))) - 1
        c.append(c_)

    INF = int(1e10)
    dp = [INF] * (1 << N)
    dp[0] = 0

    for i in range(M):
        key = 0
        for j in range(b[i]):
            key |= (1 << c[i][j])
        for bits in range(1 << N):
            dp[bits | key] = min(dp[bits | key], dp[bits] + a[i])
    
    if dp[(1 << N) - 1] == INF:
        print(-1)
    else:
        print(dp[(1 << N) - 1])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# 解説放送の解法

INF = 1001001001

N, M = map(int, input().split())
key = []
for i in range(M):
    a, b = map(int, input().split())
    s = 0
    for c in list(map(int, input().split())):
        c -= 1
        s |= 1 << c
    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)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



N, M =two_int()

key_list=[]
treasures=[]

for i in range(M):
    mon,_ =two_int()
    key_list.append(mon)
    
    temp=[0 for i in range(N)]
    for j in many_int():
        temp[N-j-1]=1
    treasures.append("".join(list(map(str,temp))))

max_index=2**N
INF=10**15

dp=[INF for i in range(max_index)]
dp[0]=0

for i in range(max_index):
    for j in range(M):
        new_i = int(bin(int(treasures[j],2) | i),2)
        cost = dp[i] + key_list[j]
        dp[new_i] = min(dp[new_i], cost) 

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

Submission status: Time Limit Exceeded

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N, M =two_int()

key_list=[]
treasures=[]

for i in range(M):
    mon,_ =two_int()
    key_list.append(mon)
    
    temp=[0 for i in range(N)]
    for j in many_int():
        temp[N-j-1]=1
    treasures.append("".join(list(map(str,temp))))

dp=[10**15 for i in range(2**N)]
dp[0]=0

for i in range(2**N):
    for j in range(M):
        new_i = int(bin(int(treasures[j],2) | i),2)
        cost = dp[i] + key_list[j]
        dp[new_i] = min(dp[new_i], cost) 

goal_index=2**N-1
if dp[goal_index]!=10**15:
    print(dp[goal_index])
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

INF = 10 ** 8 + 1
n, m = map(int, input().split())
state_num = 1 << n
key = []
for i in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    bt = 0
    for e in c:
        bt |= 1 << (e - 1)
    key.append((bt, a))

dp = [INF for _ in range(state_num)]
dp[0] = 0

for i in range(state_num):
    for j in range(m):
        ks, kc = key[j]
        state = i | ks
        cost = dp[i] + kc
        dp[state] = min(dp[state], cost)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

INF = 10 ** 8 + 1
n, m = map(int, input().split())
state_num = 1 << n
key = []
for i in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    bt = 0
    for e in c:
        bt |= 1 << (e - 1)
    key.append((bt, a))

dp = [INF for _ in range(state_num)]
dp[0] = 0

for i in range(state_num):
    for j in range(m):
        ks, kc = key[j]
        state = i | ks
        cost = dp[i] + kc
        dp[state] = min(dp[state], cost)

if dp[-1] == INF:
    ans = -1
else:
    ans = dp[-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

INF = 10 ** 8 + 1
n, m = map(int, input().split())
state_num = 1 << n
key = []
for i in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    bt = 0
    for e in c:
        bt |= 1 << (e - 1)
    key.append((bt, a))

dp = [INF for _ in range(state_num)]
dp[0] = 0

for i in range(state_num):
    for j in range(m):
        state = i | key[j][0]
        cost = dp[i] + key[j][1]
        dp[state] = min(dp[state], cost)

if dp[-1] == INF:
    ans = -1
else:
    ans = dp[-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

INF = 10 ** 8 + 1
n, m = map(int, input().split())
key = []
for i in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    bt = 0
    for e in c:
        bt |= 1 << (e - 1)
    key.append((bt, a))

dp = [INF for _ in range(1 << n)]
dp[0] = 0

for i in range(1 << n):
    for j in range(m):
        state = i | key[j][0]
        cost = dp[i] + key[j][1]
        dp[state] = min(dp[state], cost)

if dp[-1] == INF:
    ans = -1
else:
    ans = dp[-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
key = []
for i in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    bt = 0
    for e in c:
        bt |= 1 << (e - 1)
    key.append((bt, a))

dp = [float("inf") for _ in range(1 << n)]
dp[0] = 0

for i in range(1 << n):
    for j in range(m):
        state = i | key[j][0]
        cost = dp[i] + key[j][1]
        dp[state] = min(dp[state], cost)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
ab = []
c = []
key = []
for i in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    bt = 0
    for e in c:
        bt |= 1 << (e - 1)
    key.append((bt, a))

dp = [float("inf") for _ in range(1 << n)]
dp[0] = 0

for i in range(1 << n):
    for j in range(m):
        state = i | key[j][0]
        cost = dp[i] + key[j][1]
        dp[state] = min(dp[state], cost)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

for i in range(m):
    a, b = map(int, input().split())
    c = sum(1<<(ci-1) for ci in map(int, input().split()))

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


if dp[(1<<n) - 1] == float("inf"):
    ans = -1
else:
    ans = dp[(1<<n) - 1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
INF = float('inf')

def read_key():
  a, _ = map(int, input().split())
  m = 0
  for c in map(int, input().split()):
    m |= 1 << (c - 1)
  return (a, m)

N, M = map(int, input().split())
keys = [read_key() for _ in range(M)]

dp = [[INF] * (1 << N) for _ in range(M + 1)]
dp[0][0] = 0
for i in range(M):
  a, m = keys[i]
  for j in range(1 << N):
    if dp[i][j] == INF:
      continue
    dp[i + 1][j | m] = min(dp[i + 1][j | m], dp[i][j] + a)
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
if dp[M][(1 << N) - 1] == INF:
  print(-1)
else:
  print(dp[M][(1 << N) - 1])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

for i in range(m):
    a, b = map(int, input().split())
    c_list = list(map(int, input().split()))
    c = 0
    for j in range(b):
        c += 1 << (c_list[j]-1)

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


if dp[(1<<n) - 1] == float("inf"):
    ans = -1
else:
    ans = dp[(1<<n) - 1]

print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(m):
    a, b = map(int, input().split())
    c_list = list(map(int, input().split()))
    c = 0
    for j in range(b):
        c += 1 << (c_list[j]-1)

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

if dp[(1<<n) - 1] == float("inf"):
    ans = -1
else:
    ans = dp[(1<<n) - 1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ab = []
d = [0 for _ in range(M)]
for i in range(M):
  ab.append(list(map(int, input().split())))
  for k in list(map(int, input().split())):
    d[i] += 2**(k-1)
  
infty = 10**8
  
dp = [infty for j in range(2**N)]
#i番目の鍵まで使って状態jにできる最小のコスト
dp[0] = 0

for i in range(M):
  for j in range(2**N):
    dp[j | d[i]] = min(dp[j | d[i]], dp[j] + ab[i][0])

ans = dp[2**N-1] if dp[2**N-1] != infty else -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
L=2**N
l=[float("inf")]*L
l[0]=0
r=(1,2,4,8,16,32,64,128,256,512,1024,2048)
for i in range(M):
    s=0
    a,b=map(int,input().split())
    c=tuple(map(int,input().split()))
    for j in c:
        s+=r[j-1]
    for j in range(L):
        l[j|s]=min(l[j|s],l[j]+a)
if l[-1]==float("inf"):
    print(-1)
else:
    print(l[-1])

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int, input().split())
L = []
C = []
for i in range(M):
    L.append([int(l) for l in input().split()])
    C.append([int(c) for c in input().split()])
    
dp = [[10**8+1 for j in range(2**N)] for i in range(M+1)]

for i in range(M+1):
    dp[i][0] = 0

    
for i in range(1, M+1):
    temp = 0
    for k in range(len(C[i-1])):
        temp += 2**(C[i-1][k]-1)
    if i == 1:
        for j in range(1, 2**N):
            if j | temp == temp:
                dp[i][j] = L[i-1][0]
                l = i+1
                while l <= M:
                    dp[l][j] = dp[i][j]
                    l += 1

    else:
        for j in range(2**N):
            dp[i][temp | j] = min(dp[i-1][temp | j], dp[i-1][j]+L[i-1][0], dp[i][temp | j])
            l = i+1
            while l <= M:
                dp[l][temp | j] = dp[i][temp | j]
                l += 1


if dp[-1][-1] >= 10**8+1:
    print(-1)
else:
    print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
key = []

for i in range(m):
    a, _ = list(map(int, input().split()))
    s = 0

    cs = list(map(int, input().split()))
    for c in cs:
        s |= 1<<(c-1)
    key.append([s, a])

INF = 10**7
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:
  print(-1)
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
#%%
N, M = map(int, input().split())

a = [0] * M
b = [0] * M
c = [[] for _ in range(M)]
for i in range(M):
    a[i], b[i] = map(int, input().split())
    c[i] = list(map(int, input().split()))

inf = float('inf') 
cost = [inf] * (2**N)
cost[0] = 0

movement = []
for i in range(M):
    move = 0
    for j in range(len(c[i])):
        move += 2 ** (c[i][j]-1)
    movement.append(move)

for i in range(2**N):
    for j in range(M):
        move2 = movement[j]
        cost[i|move2] = min(cost[i|move2], cost[i]+a[j])

if cost[2**N-1] == inf:
    print(-1)
else:
    print(cost[2**N-1])

Submission status: Time Limit Exceeded

Solution code:
#%%
N, M = map(int, input().split())

a = [0] * M
b = [0] * M
c = [[] for _ in range(M)]
for i in range(M):
    a[i], b[i] = map(int, input().split())
    c[i] = list(map(int, input().split()))

inf = (10**5) * 13
cost = [inf] * (2**N)
cost[0] = 0

movement = []
for i in range(M):
    move = 0
    for j in range(len(c[i])):
        move += 2 ** (c[i][j]-1)
    movement.append(move)

for i in range(2**N):
    for j in range(M):
        move2 = movement[j]
        cost[i|move2] = min(cost[i|move2], cost[i]+a[j])

if cost[2**N-1] == inf:
    print(-1)
else:
    print(cost[2**N-1])

Submission status: Time Limit Exceeded

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

C = []
dp = [10 ** 9 for i in range (1 << N)]
dp [0] = 0
for i in range(M):
  a, b = map(int, input().split())
  c = list(map(lambda x: int(x) - 1, input().split()))
  b = 0
  for i in c:
    b |= 1 << i
  C.append((b,a))
for s in range(1 << N):
  for c in range(M):
    t = s | C[c][0]
    dp[t] = min(dp[t], dp[s] + C[c][1])
print (dp[-1] if dp[-1] != 10 ** 9 else -1)

Submission status: Time Limit Exceeded

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

C = []
dp = [10 ** 9 for i in range (1 << N)]
dp [0] = 0
for i in range(M):
  a, b = map(int, input().split())
  c = list(map(lambda x: int(x) - 1, input().split()))
  b = 0
  for i in c:
    b |= 1 << i
  C.append((b,a))
for s in range(1 << N):
  for c in range(M):
    t = s | C[c][0]
    cost = dp[s] + C[c][1]
    dp[t] = min(dp[t], cost)
print (dp[-1] if dp[-1] != 10 ** 9 else -1)

Submission status: Time Limit Exceeded

Solution code:
# O(MN + 4^N) 解
# 参考: https://twitter.com/ei1333/status/1177951676818325504
def main():
    import sys
    input = sys.stdin.readline
    N, M = map(int, input().split())
    dp = [float("inf")] * (1<<N)
    for _ in range(M):
        a, _ = map(int, input().split())
        C = list(map(int, input().split()))
        s = 0
        for c in C:
            s |= 1 << c-1
        dp[s] = min(dp[s], a)
    for i in range(1<<N):
        for j, dp_j in enumerate(dp):
            dp[i|j] = min(dp[i|j], dp[i]+dp_j)
    ans = dp[-1]
    print(ans if ans!=float("inf") else -1)

main()

Submission status: Time Limit Exceeded

Solution code:
# O(MN + 4^N) 解
# 参考: https://twitter.com/ei1333/status/1177951676818325504
def main():
    import sys
    input = sys.stdin.readline
    N, M = map(int, input().split())
    dp = [float("inf")] * (1<<N)
    for _ in range(M):
        a, _ = map(int, input().split())
        C = list(map(int, input().split()))
        s = 0
        for c in C:
            s |= 1 << c-1
        dp[s] = min(dp[s], a)
    for i in range(1<<N):
        for j in range(1<<N):
            dp[i|j] = min(dp[i|j], dp[i]+dp[j])
    ans = dp[-1]
    print(ans if ans!=float("inf") else -1)

main()

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
K = [[0, 0] for i in range(M)]
for i in range(M) :
    a,b = map(int,input().split())
    cx = tuple(map(int,input().split()))
    s = 0
    for c in cx :
        s = s | (1 << (c-1))
    K[i] = [s, a]

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

for bit in range(1 << N) :
    for i in range(M) :
        s,a = K[i]
        t = bit | s
        cost = dp[bit] + a
        dp[t] = min(dp[t], cost)

ans = dp[-1] if dp[-1] != INF else -1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

import sys
# input = sys.stdin.readline
sys.setrecursionlimit(100000)
MOD = 10 ** 9 + 7
INF = float('INF')
#N = int(input())
#A = [int(x) for x in input().split()]
#V = [[0] * 100 for _ in range(100)]

N, M = [int(x) for x in input().split()]
COSTS = [INF] * (1 << N)
COSTS[0] = 0
KEYS = [0]
A = [0]
for _ in range(M):
    a, b = [int(x) for x in input().split()]
    C = [int(x) for x in input().split()]
    key = 0
    for c in C:
        key |= (1 << (c-1))
    A.append(a)
    KEYS.append(key)

for pat in range(1 << N):
    for a, key in zip(A, KEYS):
        COSTS[pat | key] = min(COSTS[pat | key], COSTS[pat] + a)

ans = COSTS[(1 << N) - 1] 
if ans == INF:
    ans = -1 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
keys=[]
inf=12*(10**5)+1
for i in range(M):
    a,b=map(int,input().split())
    c=sum([1<<(i-1) for i in list(map(int,input().split()))])
    keys.append((a,c))

#print(keys)

dp=[inf for j in range(2**N)]
dp[0]=0

for key in keys:
    for j in range(2**N):
        dp[j|key[1]]=min(dp[j|key[1]],dp[j]+key[0])
#    print(dp)

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

Submission status: Time Limit Exceeded

Solution code:
I=lambda:map(int,input().split())
n,m=I()
d=[0]+[9**9]*2**n
while m:
 m-=1;a,_=I();s=sum(1<<c-1for c in I())
 for j in range(2**n):d[s|j]=min(d[s|j],d[j]+a)
print(d[-2]%9**9or-1)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
keys=[]
for i in range(M):
    a,b=map(int,input().split())
    c=list(map(int,input().split()))
    cans=0
    for i in c:
        cans+=1<<(i-1)
    keys.append((a,cans))

#print(keys)

dp=[12*(10**5)+1 for j in range(2**N)]
dp[0]=0

for key in keys:
    for j in range(2**N):
        dp[j|key[1]]=min(dp[j|key[1]],dp[j]+key[0])
#    print(dp)

print(dp[2**N-1] if dp[2**N-1]!=12*(10**5)+1 else -1)

Submission status: Time Limit Exceeded

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

#print(keys)

dp=[12*(10**5)+1 for j in range(2**N)]
dp[0]=0

for key in keys:
    for j in range(2**N):
        dp[j|key[1]]=min(dp[j|key[1]],dp[j]+key[0])
#    print(dp)

print(dp[2**N-1] if dp[2**N-1]!=12*(10**5)+1 else -1)

Submission status: Time Limit Exceeded

Solution code:
def view():
    global keyboxes
    for keybox in keyboxes:
        for key in keybox:
            print(key.value,key.cans)
        print()

class Key():
    def __init__(self,value,cans):
        self.value=value
        self.cans=cans
    
    def __eq__(self,other):
        return self.value==other.value
    
    def __lt__(self,other):
        return self.value<other.value
    
    def __gt__(self,other):
        return self.value>other.value
    
N,M=map(int,input().split())
keys=[]
for i in range(M):
    a,b=map(int,input().split())
    c=list(map(int,input().split()))
    cans=0
    for i in c:
        cans+=2**(i-1)
    keys.append(Key(a,cans))

dp=[12*(10**5)+1 for j in range(2**N)]
dp[0]=0

for key in keys:
    for j in range(2**N):
        dp[j|key.cans]=min(dp[j|key.cans],dp[j]+key.value)

print(dp[2**N-1] if dp[2**N-1]!=12*(10**5)+1 else -1)

Submission status: Time Limit Exceeded

Solution code:
I=lambda:map(int,input().split());n,m=I();d=[0]+[9**9]*2**n
while m:
 m-=1;a,_=I();s=sum(1<<c-1for c in I())
 for j in range(2**n):d[s|j]=min(d[s|j],d[j]+a)
print(d[-2]%9**9or-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
k = []
for _ in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    k.append((a, sum([2 ** (i-1) for i in c])))

C = 2 ** n
INF = 10 ** 6

dp = [[INF for _ in range(C)] for __ in range(m + 1)]
for i in range(m+1):
    dp[i][0] = 0

for i, ki in enumerate(k):
    for j in range(C):
        dp[i+1][j] = dp[i][j]
    for j in range(C):
        dp[i+1][j | ki[1]] = min(dp[i+1][j | ki[1]], dp[i][j] + ki[0])

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
k = []
for _ in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    k.append((a, sum([2 ** (i-1) for i in c])))

C = 2 ** n
INF = 10 ** 6

dp = [[INF for _ in range(C)] for __ in range(m + 1)]
for i in range(m+1):
    dp[i][0] = 0

for i, ki in enumerate(k):
    for j in range(C):
        dp[i+1][j] = dp[i][j]
    for j in range(C):
        dp[i+1][j | k[i][1]] = min(dp[i+1][j | k[i][1]], dp[i][j] + k[i][0])

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
ac = []
for _ in range(m):
    a,_ = map(int, input().split())
    c = sum([2**(int(i)-1) for i in input().split()])
    ac.append([a, c])
dp = [float('inf')] * 2**n
dp[0] = 0
for s in range(2**n):
    for a, c in ac:
        t = s|c
        cost = dp[s] + a
        dp[t] = min(dp[t], cost)
ans = dp[-1]
if ans == float('inf'):
    ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:

"""
bitDPというらしい
dp[i][j] = i番目の入力に対して、状態jを実現する最小値
j : 2^12通り。それぞれの宝箱をbitと見立てる
"""

N, M = map(int, input().split())
INF = 10**5+1


dp = [[INF for i in range(2**N+1)] for j in range(1100)]
dp[0][0] = 0

for i in range(M):
    dp[i+1] = [v for v in dp[i]]
    cost,b = map(int, input().split())
    boxes = list(map(int, input().split()))
    status = 0
    for box in boxes:
        status += 2**(box-1)
    for j in range(2**N):
        if dp[i][j] != INF:
            nxt = status | j
            dp[i+1][nxt] = min(dp[i][j] + cost, dp[i][nxt], dp[i+1][nxt])

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

# for i in range(M):
#     print(dp[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

INF = 10**12
N,M = map(int,input().split())
K = []
for i in range(M) :
    a,b = map(int,input().split())
    s = 0 #setをbitで管理.
    cx = list(map(int,input().split()))
    for c in cx :
        s = s | (1 << (c-1)) #setの和集合
    K.append([s, a])

#dp[bit]: 集合=bit のときのコストの最小値
dp = [INF]*(1 << N)
dp[0] = 0

for bit in range(1 << N) :
    for i in range(M) :
        s,a = K[i]
        t = bit | s
        cost = dp[bit] + a
        dp[t] = min(dp[t], cost)
ans = -1 if dp[-1] == INF else dp[-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#E
n, m = map(int, input().split())
a=[]
b=[]
c=[]
for i in range(m):
    ak, bk = map(int, input().split())
    a.append(ak)
    b.append(bk)
    ck = list(map(int,input().split()))
    c.append(ck)

#愚直に鍵の組み合わせを全通り試す。

ans = float('inf')
flg = 0#全ての箱を開けられたか判定

for i in range(2 ** m):
    cost = 0
    hako = set()
    for j in range(m):
        if (i>>j)&1 == 1:
            cost += a[j]
            hako = hako | set(c[j])
#    print("hakoは"+str(hako) + "costは"+str(cost) + "iは"+str(i))

    if len(hako) == n:#箱が全て開けられたら
        flg = 1
        if ans > cost:#そのときの費用が、これまでの最小記録より小さいとき
            ans = cost#答え（の候補）になる
if flg == 1:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
#Thanks, snuke!

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

dp = [10**12 for i in range(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 dp[-1] != 10**12 else -1

print(ans)

Submission status: Time Limit Exceeded

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

keys = []

for i in range(m):
    cost, num = map(int, input().split())
    C = list(map(int, input().split()))
    can_open = 0
    for c in C:
        can_open += 2 ** (c-1)
    keys.append([cost, can_open])

DP = {}
for i in range(2**n):
    DP[i] = float('INF')
DP[0] = 0

for cost, can_open in keys:
    for key in DP.keys():
        pre_cost = DP[key]
        next_key = key | can_open
        DP[next_key] = min(DP[next_key], pre_cost+cost)


full_open = (1 << n) - 1

if DP[full_open] == float('INF'):
    print(-1)
else:
    print(DP[full_open])

Submission status: Time Limit Exceeded

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

keys = []

for i in range(m):
    cost, num = map(int, input().split())
    C = list(map(int, input().split()))
    can_open = 0
    for c in C:
        can_open += 2 ** (c-1)
    keys.append([cost, can_open])

DP = {}
for i in range(2**n):
    DP[i] = float('INF')
DP[0] = 0

for cost, can_open in keys:
    for key in DP.keys():
        pre_cost = DP[key]
        next_key = key | can_open
        if next_key in DP.keys():
            DP[next_key] = min(DP[next_key], pre_cost+cost)
        else:
            DP[next_key] = pre_cost + cost

full_open = (1 << n) - 1

if DP[full_open] == float('INF'):
    print(-1)
else:
    print(DP[full_open])

Submission status: Time Limit Exceeded

Solution code:
import sys
I=lambda:list(map(int,sys.stdin.readline().split()))
n,m=I()
e=[(I()[0],[1<<n-i for i in I()])for _ in'_'*m]
I=9**9
dp=[I]*2**n
dp[0]=0
for i,d in enumerate(dp):
    if d==I:continue
    for a,c in e:
        b,x=i,d+a
        for j in c:b|=j
        if dp[b]>x:dp[b]=x
print(-(dp[-1]==I)or dp[-1])

Submission status: Time Limit Exceeded

Solution code:
I=lambda:list(map(int,input().split()))
n,m=I()
e=[(I()[0],[1<<n-i for i in I()])for _ in'_'*m]
I=9**9
dp=[I]*2**n
dp[0]=0
for i,d in enumerate(dp):
    if d==I:continue
    for a,c in e:
        b,x=i,d+a
        for j in c:b|=j
        if dp[b]>x:dp[b]=x
print(-(dp[-1]==I)or dp[-1])

Submission status: Time Limit Exceeded

Solution code:
I=lambda:list(map(int,input().split()))
n,m=I()
e=[(I()[0],[2**(n-i)for i in I()])for _ in'_'*m]
I=9**9
dp=[I]*2**n
dp[0]=0
for i,d in enumerate(dp):
    if d==I:continue
    for a,c in e:
        b,x=i,d+a
        for j in c:b|=j
        if dp[b]>x:dp[b]=x
print(-(dp[-1]==I)or dp[-1])

Submission status: Time Limit Exceeded

Solution code:
n, m = input().split(' ')
n = int(n)
m = int(m)
dp = [float('inf')]*(2**n)
dp[0] = 0
for i in range(m):
    a, b = input().split(' ')
    a = int(a)
    b = int(b)
    M = 0
    for j in input().split(' '):
        M += 2**(int(j)-1)

    for j in range(2**n):
        dp[j|M] = min(dp[j]+a, dp[j|M])
        # dp[j|M] = dp[j] + a if dp[j] + a < dp[j|M] else dp[j|M]

print(dp[2**n-1] if dp[2**n-1] < float('inf') else -1)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split(' '))
dp = [float('inf')]*(2**n)
dp[0] = 0
for i in range(m):
    a, b = input().split(' ')
    a = int(a)
    b = int(b)
    M = 0
    for j in input().split(' '):
        M += 2**(int(j)-1)

    for j in range(2**n):
        dp[j|M] = min(dp[j]+a, dp[j|M])

print(dp[2**n-1] if dp[2**n-1] < float('inf') else -1)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().strip().split())
dp = [float('inf')]*(2**n)
dp[0] = 0
for i in range(m):
    a, b = input().strip().split()
    a = int(a)
    b = int(b)
    M = 0
    for j in input().strip().split():
        M += 2**(int(j)-1)

    for j in range(2**n):
        dp[j|M] = min(dp[j]+a, dp[j|M])

print(dp[2**n-1] if dp[2**n-1] < float('inf') else -1)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().strip().split())
dp = [float('inf')]*(2**n)
dp[0] = 0
for i in range(m):
    a, b = input().strip().split()
    a = int(a)
    b = int(b)
    M = 0
    for i in input().strip().split():
        M += 2**(int(i)-1)

    for j in range(2**n):
        dp[j|M] = min(dp[j]+a, dp[j|M])

print(dp[2**n-1] if dp[2**n-1] < float('inf') else -1)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
keys, keyskey = [10000000] * (2**N + 1), set()
for i in range(M):
    a, b = map(int, input().split())
    key = 0
    for x in set(map(int, input().split())):
        key += 2 ** (x-1)
    if keys[key] > a:
        keys[key] = a
        keyskey.add(key)

dp = [[10000000 for _ in range(2**N)] for _ in range(len(keyskey))]
i, l = keyskey.pop(), 0
dp[0][i] = keys[i]

for i in keyskey:
    l += 1
    v = keys[i]
    dp[l][i] = min(v, dp[l-1][i])
    for j in range(1, 2**N):
        if dp[l-1][j] != 10000000:
            if dp[l][i | j] == 10000000:
                dp[l][i | j] = min(dp[l-1][i | j], dp[l-1][j] + v)
            else:
                dp[l][i | j] = min(dp[l][i | j], dp[l-1][j] + v)

            if dp[l][j] == 10000000:
               dp[l][j] = dp[l-1][j]

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ABC = []
for i in range(M):
    ab = list(map(int,input().split()))
    c = list(map(int, input().split()))
    ab.append(c)
    ABC.append(ab)
    
INF = float('inf')
dp = [INF] * (2**N)
dp[0] = 0
for i in range(M):
    d = 0
    for c in ABC[i][2]:
        d |= (1 << (c-1))
        
    for j in range(2**N):
        dp[j|d] = min(dp[j|d], dp[j]+ABC[i][0])
        
if dp[-1] == INF:
    print(-1)
else:
    print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, m = map(int, input().strip().split())
dp = [float('inf')] * (2**n)
dp[0] = 0
for i in range(m):
    a, b = input().strip().split()
    a = int(a)
    b = int(b)
    c = input().strip().split()
    mask = 0
    for i in c:
        mask += 2**(int(i) - 1)

    for j in range(2**n):
        dp[j | mask] = min(dp[j] + a, dp[j | mask])

print(dp[2**n - 1] if dp[2**n - 1] < float('inf') else -1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, m = map(int, input().strip().split())
dp = [float('inf')] * (2**n)
dp[0] = 0
for i in range(m):
    a, b = map(int, input().strip().split())
    c = map(int, input().strip().split())
    mask = 0
    for i in c:
        mask += 2**(i - 1)

    for j in range(2**n):
        dp[j | mask] = min(dp[j] + a, dp[j | mask])

print(dp[2**n - 1] if dp[2**n - 1] < float('inf') else -1)