Rainbow Ride
Difficulty: Hard

Mr.Raju and his extended family are on vacation in Chennai. They visit MGM and see the Rainbow ride. They want to enjoy the ride. However, there are some problems.  
Each person in the family likes some other people in the family. Each person insists that he or she will go on the ride only if all the people whom that person likes and all the people who like that person also go on the ride. If someone doesn't like anyone and no one likes him, he may come for the ride.

You have been roped in to solve this dilemma. Given the weight of the each person in the family, and the list of people they like, and the maximum weight that the Rainbow can bear safely, calculate the maximum number of people in the family who can go on the rainbow.

**Input Format:**  
There will be multiple test cases in the input. For our convenience the family
members are numbered from 1 to n. Each test case begins with a line containing
two integers n ( 1 ≤ n ≤ 1000 ) and C ( 0 ≤ C ≤ 1000 ), where n is the number
of people in the family and C the maximum capacity of the ride in kilograms.
The next line contains n space separated integers with the ith integer giving the
weight of the i th family member. These weights are positive and less than or
equal to 200 kilograms. Then n lines follow. Each line contains a sequence of
integers separated by spaces. The first integer ki gives the number of people in
the family person i likes, followed by the persons i likes. An empty line separates
each test case. The end of input is indicated by n=0 and C=0 and it should not be
processed. There are atmost 50 test cases.

**Output Format:**  
For each test case output on a separate line the maximum number of persons
that can take the ride under the given conditions.

**Example:**    
*Input:*   
5 200  
50 50 50 50 50  
1 2  
1 3  
0  
1 5  
1 4  
3 200  
100 100 100  
1 2  
1 3  
1 1  
0 0  

*Output:*  
3  
0  

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
	#include <vector>
	#include <list>
	#include <map>
	#include <set>
	#include <deque>
	#include <queue>
	#include <stack>
	#include <bitset>
	#include <algorithm>
	#include <functional>
	#include <numeric>
	#include <utility>
	#include <sstream>
	#include <iostream>
	#include <iomanip>
	#include <cstdio>
	#include <cmath>
	#include <cstdlib>
	#include <cctype>
	#include <string>
	#include <cstring>
	#include <cstdio>
	#include <cmath>
	#include <cstdlib>
	#include <ctime>

	using namespace std;

	template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}

	const int maxsize=1000+5;

	int n,m;
	int f[maxsize];
	int father[maxsize];
	int A[maxsize];

	int getfather(int v)
	{
	return (father[v]<0)?v:(father[v]=getfather(father[v]));
	}
	void merge(int u,int v)
	{
	u=getfather(u);
	v=getfather(v);
	if (u!=v) father[u]=v;
	}
	int main()
	{
	//	freopen("input.txt","r",stdin);
	while (scanf("%d%d",&n,&m)!=-1 && n!=0)
	{
		memset(father,255,sizeof(father));
		for (int i=1;i<=n;i++) scanf("%d",&A[i]);
		for (int i=1;i<=n;i++)
		{
			int cnt;
			scanf("%d",&cnt);
			for (;cnt>0;cnt--)
			{
				int value;
				scanf("%d",&value);
				merge(i,value);
			}
		}
		memset(f,0,sizeof(f));
		for (int i=1;i<=n;i++) if (father[i]<0)
		{
			int cnt=0;
			int sum=0;
			for (int j=1;j<=n;j++) if (getfather(j)==i)
				cnt++,sum+=A[j];
			for (int k=m;k>=sum;k--)
				checkmax(f[k],f[k-sum]+cnt);
		}
		printf("%d\n",f[m]);
	}
	return 0;
	}