TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

In the magical kingdom of Kasukabe, people strive to possess only one skillset. Higher the number of skillset present among the people, the more content people will be.



There are N types of skill set present and initially there exists Ci people possessing ith skill set, where i∈[1,N].



There are T wizards in the kingdom and they have the ability to transform the skill set of a person into another skill set. Each of the these wizards has two list of skill sets associated with them, A and B. He can only transform the skill set of person whose initial skill set lies in list A and that final skill set will be an element of list B. That is, if A=[2,3,6] and B=[1,2] then following transformation can be done by that trainer.

212231326162




Once a transformation is done, both skill is removed from the respective lists. In the above example, if he perform 3→1 transformation on a person, list A will be updated to [2,6] and list B will be [2]. This updated list will be used for next transformation and so on.



Few points to note are:


  • A wizard can perform 0 or more transformation as long as they satisfies the above criteria.

  • A person can go through multiple transformation of skill set.

  • Same class transformation is also possible. That is a person' skill set can be transformed into his current skill set. Eg. 22 in the above example.


Your goal is to design a series of transformation which results into maximum number of skill set with non-zero acquaintance.

Input Format

The first line contains two numbers, N T, where N represent the number of skill set and T represent the number of wizards.

Next line contains N space separated integers, C1 C2 … CN, where Ci represents the number of people with ith skill. Then follows 2×T lines, where each pair of line represent the configuration of each wizard.

First line of the pair will start with the length of list A and followed by list A in the same line. Similarly second line of the pair starts with the length of list B and then the list B.

Constraints
1N200
0T30
0Ci10
0|A|50
1AiN
AiAj,1i<j|A|
0|B|50
1BiN
BiBj,1i<j|B|

Output Format

The output must consist of one number, the maximum number of distinct skill set that can the people of country learn, after making optimal transformation steps.

Sample Input 1

3 1
3 0 0
1 1
2 2 3

Sample Output 1

2

Hints

There are 3 types of skill set present and only 1 wizard. Initially all three people know 1st skill set, while no one knows 2nd and 3rd skill set.



First, and only, wizard' initial list are: A=[1] and B=[2,3]. So he can perform any of the 1→2 or 1→3 transformation. Suppose he go for 1→2 transformation on any of person with 1st skill set, then list A will be updated to an empty list [] and and list B will be [3]. Now he can't perform another transformation as list A is empty. So there will be two people with 1st skill set, and 1 people with 2nd skill set. So there are two skill sets available in the kingdom.

Problem Source

Migrated from old NTUJ.

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 2000 65536 131072
1 2000 65536 131072
2 2000 65536 131072
3 2000 65536 131072
4 2000 65536 131072
5 2000 65536 131072
6 2000 65536 131072
7 2000 65536 131072
8 2000 65536 131072
9 2000 65536 131072
10 2000 65536 131072
11 2000 65536 131072
12 2000 65536 131072
13 2000 65536 131072
14 2000 65536 131072
15 2000 65536 131072
16 2000 65536 131072
17 2000 65536 131072
18 2000 65536 131072
19 2000 65536 131072
20 2000 65536 131072
21 2000 65536 131072
22 2000 65536 131072
23 2000 65536 131072
24 2000 65536 131072