Under construction

Still being built. Things may change

Submitting Assignments

Time limit1sMemory limit1024 MB

Summary
Given N knowledge pieces that each expire after a set number of days and M problems needing certain pieces, find the fewest studies needed to solve all problems in some order.
Level

Medium5 of 10

Topics
Brute force, Greedy, Simulation
Solved
No attempts yet

Problem

Seonghyeon's course covers NN pieces of knowledge, numbered from 1 to NN.

Seonghyeon must solve all MM problems and submit them. Solving one problem takes one day, and Seonghyeon chooses the order of the problems. So Seonghyeon solves exactly one problem on each of day 1, day 2, ⋯\cdots, day MM.

To solve problem ii, Seonghyeon needs kik_i pieces of knowledge: ai,1a_{i,1}, ai,2a_{i,2}, ⋯\cdots, ai,kia_{i,k_i}.

Knowledge fades over time. If Seonghyeon studies piece nn on day xx, then on day (x+dn)(x+d_n) Seonghyeon has forgotten piece nn. On that day Seonghyeon may need to study it again. Seonghyeon may learn several pieces of knowledge on the same day.

Find the minimum number of times Seonghyeon must study pieces of knowledge so that all MM problems are solved.

Input

The first line contains NN and MM, separated by a space.

The second line contains d1d_1, d2d_2, ⋯\cdots, dNd_N, separated by spaces. dnd_n is the number of days after which piece nn is forgotten after it is studied.

Each of the next MM lines contains kik_i, followed by the kik_i integers ai,1a_{i,1}, ai,2a_{i,2}, ⋯\cdots, ai,kia_{i,k_i}, separated by spaces. ai,ja_{i,j} is the number of a piece of knowledge needed to solve problem ii.

Output

Print the minimum number of times Seonghyeon must study pieces of knowledge.

Constraints

  • 1≤N≤1 0001 \leq N \leq 1\,000
  • 1≤M≤71 \leq M \leq 7
  • 1≤di≤M1 \leq d_i \leq M
  • Every did_i is an integer.
  • 1≤ki≤N1 \leq k_i \leq N
  • 1≤ai,j≤N1 \leq a_{i,j} \leq N
  • For every 1≤i≤M1 \leq i \leq M, ai,j<ai,j+1a_{i,j} < a_{i,j+1} holds. (1≤j≤ki−1)(1 \leq j \leq k_i-1)

Examples2

  1. Example 1

    Input
    3 3
    3 1 2
    2 1 3
    2 2 3
    1 1
    
    Expected output
    3
    
  2. Example 2

    Input
    3 3
    2 2 2
    2 1 2
    1 3
    2 1 2
    
    Expected output
    3