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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;

namespace abc142_e
{
  class Program
  {
    static void Main(string[] args)
    {
      var n = CIn.ReadInt();
      var m = CIn.ReadInt();
      var keys = new Key[m];

      for (var i = 0; i < m; i++)
      {
        var a = CIn.ReadInt();
        var b = CIn.ReadInt();
        var c = 0;

        for (var j = 0; j < b; j++)
        {
          c += (int)Math.Pow(2, CIn.ReadInt() - 1);
        }

        keys[i] = new Key(a, c);
      }

      var pow = (int)Math.Pow(2, n) - 1;
      var dp = new int[m + 1, pow + 1];

      for (var i = 1; i <= pow; i++)
      {
        dp[0, i] = -1;
      }

      for (var i = 1; i <= m; i++)
      {
        var k = keys[i - 1];

        for (var j = 1; j <= pow; j++)
        {
          var sub = j & (k.CanBeOpened ^ pow);
          if (sub == j)
          {
            dp[i, j] = dp[i - 1, j];
          }
          else
          {
            if (dp[i - 1, j] == -1)
            {
              if (dp[i, sub] == -1)
              {
                dp[i, j] = -1;
              }
              else
              {
                dp[i, j] = dp[i, sub] + k.Price;
              }
            }
            else
            {
              if (dp[i, sub] == -1)
              {
                dp[i, j] = dp[i - 1, j];
              }
              else
              {
                dp[i, j] = Math.Min(dp[i - 1, j], dp[i, sub] + k.Price);
              }
            }
          }
        }
      }

      Console.WriteLine(dp[m, pow]);
    }
  }

  class Key
  {
    public int Price { get; }
    public int CanBeOpened { get; }

    public Key(int price, int canBeOpened)
    {
      Price = price;
      CanBeOpened = canBeOpened;
    }
  }

  class CIn
  {
    private static readonly Queue<string> fieldBuffer = new Queue<string>();

    public static string Read()
    {
      if (fieldBuffer.Count == 0)
      {
        foreach (var field in Console.ReadLine().Split())
        {
          fieldBuffer.Enqueue(field);
        }
      }

      return fieldBuffer.Dequeue();
    }

    public static int ReadInt() => int.Parse(Read());
    public static long ReadLong() => long.Parse(Read());
    public static double ReadDouble() => double.Parse(Read());

    public static string[] ReadStringArray(long n)
    {
      var array = new string[n];

      for (var i = 0; i < n; i++)
      {
        array[i] = Read();
      }

      return array;
    }

    public static int[] ReadIntArray(long n)
    {
      var array = new int[n];

      for (var i = 0; i < n; i++)
      {
        array[i] = ReadInt();
      }

      return array;
    }

    public static long[] ReadLongArray(long n)
    {
      var array = new long[n];

      for (var i = 0; i < n; i++)
      {
        array[i] = ReadLong();
      }

      return array;
    }
    public static double[] ReadDoubleArray(long n)
    {
      var array = new double[n];

      for (var i = 0; i < n; i++)
      {
        array[i] = ReadDouble();
      }

      return array;
    }
  }
}