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.
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
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.
3 1 3 0 0 1 1 2 2 3
2
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.
Migrated from old NTUJ.
No. | Testdata Range | Score |
---|