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 in Kotlin (1.0.0) (judge verdict: Accepted)

import java.io.PrintWriter
import java.util.*
import java.math.*
import java.lang.*
import kotlin.comparisons.*

val pw = PrintWriter(System.out)
val MOD: Long = (1e+9 + 7).toLong()

data class Key(val cost: Int, val canOpenBoxes: Int)
fun main(args: Array<String>) {
    val (n, m) = readListOfInt()
    val numBoxStates = Math.pow(2.0, n.toDouble()).toInt()


    val keys = (0 until m).map {
        val (a, b) = readListOfInt()
        val c = readListOfInt()
        val cBit = c.fold(0) { acc, v -> acc or (1 shl v-1) }
        Key(a, cBit)
    }

    val dp = (0 .. numBoxStates).map { Int.MAX_VALUE }.toMutableList()
    dp[0] = 0

    keys.forEach { k ->
        for(i in 0 until numBoxStates) {
            dp[i or k.canOpenBoxes] = 
                Math.min(dp[i or k.canOpenBoxes], (dp[i] + k.cost).let { if(it<0) Int.MAX_VALUE else it})
        }
    }
    println(dp[numBoxStates-1].let { if(it==Int.MAX_VALUE) -1 else it })
    //val dp = (0 .. m).map { (0 until numBoxStates).map { Int.MAX_VALUE }.toMutableList() }.toMutableList()

    //dp[0][0] = 0
    //for(i in 1 .. m) {
    //    val key = keys[i-1]
    //    dp[i] = dp[i-1].toMutableList()
    //    for(bState in 0 until numBoxStates) {
    //        if(dp[i-1][bState] == Int.MAX_VALUE) continue
    //        dp[i][bState or key.canOpenBoxes] = 
    //            Math.min(dp[i-1][bState] + key.cost, dp[i][bState or key.canOpenBoxes])
    //    }
    //}

    //println(dp[m][numBoxStates-1].let { if(it == Int.MAX_VALUE) -1 else it })

    pw.flush()
}

/****** Declared Functions and Data Structures ******/
// IO
fun read() = readLine()!!
fun readInt() = read().toInt()
fun readLong() = read().toLong()
fun readDouble() = read().toDouble()
fun readListOfString() = read().split(" ")
fun readListOfInt() = readListOfString().map { it.toInt() }
fun readListOfLong() = readListOfString().map { it.toLong() }
fun readListOfDouble() = readListOfString().map { it.toDouble() }

fun Double.format(digits: Int) = String.format("%.${digits}f", this)
fun Float.format(digits: Int) = String.format("%.${digits}f", this)

fun print(value: Any) { pw.print(value) }
fun println(value : Any) { pw.println(value) }

// Extensions
fun<T> List<T>.toBuckets(): Map<T, Int>
    = this.groupBy { it }.mapValues { it.value.size }

fun<T: Comparable<T>> List<T>.upperBound(element: T, fromIndex: Int=0, toIndex: Int=this.size): Int {
    var l = fromIndex
    var r = toIndex
    while(l < r) {
        val mid = (l+r) / 2
        if(element >= this[mid]) { 
            l = mid + 1
        } else {
            r = mid
        }
    }
    return l
}

fun<T: Comparable<T>> List<T>.lowerBound(element: T, fromIndex: Int=0, toIndex: Int=this.size): Int {
    var l = fromIndex
    var r = toIndex
    while(l < r) {
        val mid = (l+r) / 2
        if(element <= this[mid]) { 
            r = mid
        } else {
            l = mid + 1
        }
    }
    return r 
}

fun <K, V> Map<K, V>.toMutableMap(): MutableMap<K, V> = HashMap(this)

// Util Functions
fun makeDivisors(n: Long): List<Long> {
    val divisors = mutableListOf<Long>()
    for(i in 1L .. Math.sqrt(n.toDouble()).toLong()+1L) {
        if(n % i == 0L) {
            divisors.add(i)
            if(i != n/i) {
                divisors.add(n/i)
            }
        }
    }
    return divisors.distinct().sorted()
}

fun<T> permutations(src: List<T>): List<List<T>> {
    if(src.size == 1) return listOf(src)
    val perms = mutableListOf<List<T>>()
    val insertElement = src[0]
    permutations(src.drop(1)).forEach { perm ->
        for(i in 0..perm.size) {
            val newPerm = perm.toMutableList()
            newPerm.add(i, insertElement)
            perms.add(newPerm.toList())
        }
    }
    return perms
}

// return nCr, if you want nCr, please access v[n][r]
fun combinations(n: Long): List<List<Long>> {
    val v = (0..n).map { (0..n).map{0L}.toMutableList() }.toMutableList()
    for(i in 0 until v.size) {
        v[i][0] = 1L
        v[i][i] = 1L
    }
    for(i in 0 until v.size) {
        for(j in 1 until i) {
            v[i][j] = (v[i-1][j-1] + v[i-1][j]) % MOD
        }
    }
    return v.map { it.toList() }.toList()
}

// should be a >= b
fun gcd(a: Long, b: Long): Long = 
    if(b == 0L) a else gcd(b, a % b)

// shoud be a >= b
fun lcm(a: Long, b: Long): Long = 
    a / gcd(a, b) * b

fun sumDigits(num_: Long): Long {
    var num = num_
    var rtn: Long = 0
    while(num != 0.toLong()) {
        val tmp = num % 10
        rtn += tmp
        num /= 10
    }
    return rtn
}

fun Double.isDecimal(): Boolean = ((this - Math.floor(this)) != 0.0)

// Data Structures
class Stack<T>(private var st: MutableList<T> = mutableListOf<T>()) {
    fun isEmpty() = st.isEmpty()
    fun top(): T = st.last()
    fun push(e: T) { st.add(e) }
    fun pop() { st = st.dropLast(1).toMutableList() }
}

class Queue<T>(private var que: MutableList<T> = mutableListOf<T>()) {
    fun isEmpty() = que.isEmpty()
    fun top(): T = que.first()
    fun push(e: T) { que.add(e) }
    fun pop() { que = que.drop(1).toMutableList() }
}