Training the army
Difficulty: Hard

In the magical kingdom of Kasukabe, people strive to possess skillsets. 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 $C_i$ people possessing $i^{th}$ skill set, where $i \in [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 **lists** of skill sets associated with them, $A$ and $B$. He can only transform the skill set of person whose initial skill set belongs to the list $A$ to one of the final skill set which belongs to the list $B$. That is, if $A = [2, 3, 6]$ and $B = [1, 2]$ then following transformation can be done by that trainer.

$$\begin{align*}
2 \rightarrow 1\\\
2 \rightarrow 2\\\
3 \rightarrow 1\\\
3 \rightarrow 2\\\
6 \rightarrow 1\\\
6 \rightarrow 2\\\
\end{align*}$$

Once a transformation is done, both skill is removed from the respective lists. In the above example, if he perform  $3 \rightarrow 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 further transformations.

Few points to note are:

- One person can possess only one skill set.
- A wizard can perform zero 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. $2 \rightarrow 2$ in the above example.

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

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, $C_1\ C_2\ \ldots\ C_N$,  where $C_i$ represents the number of people with $i^{th}$ skill.
Then follows $2 \times 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$. 

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:
    3 3
    3 0 0
    1 1
    2 2 3
    1 2
    1 3
    1 1
    1 2 

Sample Output:
	2  

Python 3 starter code:


Reference solution (C++):
###C++  
```cpp  
#include<stdio.h>
#include<math.h>
#include<complex>
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cassert>
#include<map>
#include<vector>
using namespace std;

using namespace std;
const int MAXA = 400005;
const int MAXV = 1005;

int A,V,source,dest,id,oo = 1000000001;
int cap[MAXA],flow[MAXA],ady[MAXA],next[MAXA],last[MAXV];
int now[MAXA],level[MAXV],Q[MAXV];

void ADD(int u,int v,int c)
{
  cap[A] = c;flow[A] = 0;ady[A] = v;next[A]=last[u];last[u]=A++;
  cap[A] = 0;flow[A] = 0;ady[A] = u;next[A]=last[v];last[v]=A++;
}

bool BFS(int fuente,int dest)
{
  int ini,end,u,v;
  memset(level,-1,sizeof(level));

  ini = end = level[fuente] = 0;
  Q[end++] = source;

  while(ini<end && level[dest]==-1)
  {
   u = Q[ini++];
   for(int i=last[u];i!=-1;i=next[i])
   {
     v = ady[i];
     if(level[v]==-1 && flow[i]<cap[i])
     {
       level[v] = level[u] + 1;
       Q[end++] = v;
     }
   }
  }

 return level[dest]!=-1;
}

int DFS(int u,int auxflow)
{
  int res,v;
  if(u == dest) return auxflow;
  for(int i = now[u];i!=-1;now[u] = i = next[i])
  {
   v = ady[i];
   if(level[v]>level[u] && flow[i] < cap[i])
   {
     res = DFS(v, min(auxflow,cap[i]-flow[i]));
     if(res>0)
     {
       flow[i]+=res;
       flow[i^1]-=res;
       return res;
     }
   }
  }
 return 0;
}


long long Dinic()
{
 register long long flow = 0;
 register long long aum;

 while(BFS(source,dest))
 {
   for(int i=0;i<=V;i++) now[i] = last[i];
   while((aum = DFS(source,oo))>0) flow+=aum;
 }
 return flow;
}

int n, m, v, cc;
int main() {
    memset(last,-1,sizeof(last));
    scanf("%d%d", &n, &m);

    assert(n >= 1 && n <= 200 && m >= 0 && m <= 30);

    source = 0;
    dest = n + m + 1;
    V = dest;

    for(int i = 1;i<=n;i++) {
        scanf("%d", &v);
        assert(v >= 0 && v <= 10);
        if(v)
            ADD(source, i, v);
        ADD(i, dest, 1);
    }

    for(int i = 1;i<=m;i++) {
        scanf("%d", &cc);
        assert(cc >= 0 && cc <= 50);
        for(int j = 0;j<cc;j++) {
            scanf("%d", &v);
            assert(v>= 1 && v <= n);
            ADD(v, n + i, 1);
        }

        scanf("%d", &cc);
        assert(cc >= 0 && cc <= 50);
        for(int j = 0;j<cc;j++) {
            scanf("%d", &v);
            assert(v>= 1 && v <= n);
            ADD(n + i, v, 1);
        }
    }

    printf("%lld\n", Dinic());
}
```