Jack and the Beanbag

Find the minimum number of cows Jack needs so that, against adversarial farms, he secures the required count of each bean kind.

Hard8Game theoryDynamic programmingGreedyNo attempts yetTime limit3sMemory limit512 MB

Problem

A stranger handed Jack a bag of ordinary beans and told him that if he collects the required number of beans of every kind, he can grow a giant beanstalk and climb it to the treasure at the top.

Jack has to get the missing beans from the other farms in his village. Each farm grows a fixed set of bean kinds. When Jack asks a farm for a bean, that farm gives him exactly one bean, but Jack is not paying for it, so the farm picks the kind. It can hand over any kind it grows, and it can pick a different kind on every visit. Jack may visit any farm as many times as he likes.

The stranger takes cows too. For one cow Jack gets one bean of any kind he names.

Jack wants no chance of failure. Assume every farm picks the kind that hurts Jack the most, every time. What is the smallest number of cows Jack must bring so that he ends up with at least the required number of beans of every kind, whatever the farms pick?

Input

  • The first line contains an integer BB (1B201 \le B \le 20), the number of bean kinds.
  • The second line contains BB integers V1,,VBV_1, \dots, V_B (0Vi1000 \le V_i \le 100), the number of beans required of each kind.
  • The third line contains TT (1T1001 \le T \le 100), the number of other farms in the village.
  • Each of the next TT lines describes one farm. The line starts with an integer MM (1MB1 \le M \le B), the number of bean kinds that farm grows, followed by MM distinct integers t1,,tMt_1, \dots, t_M (1tiB1 \le t_i \le B), the kinds it grows.

Output

  • Print one integer, the smallest number of cows Jack must bring to be certain of ending the day with enough beans to grow the beanstalk.