#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	bl ccc(int a,int b){
		return a>b;
	}
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define sr(a,b,n) sort((a)+(b),(a)+(n)+(b))
	#define psr(a,b,n) sort((a)+(b),(a)+(n)+(b),ccc)
	#define maxinf 0xffffffff
	#define mininf -0xffffffff
	#define chs(a,b,c) (a)?(b):(c)
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
ll n,m,g,s,t,a[10001],head[40004],dep[40004],cnt=1,ans=0;
queue<ll> q;
struct node{
	ll to,nxt,w;
}e[40004];
void add1(ll x,ll y,ll z){
	e[++cnt].nxt=head[x];
	e[cnt].to=y;
	e[cnt].w=z;
	head[x]=cnt;
}
void add(ll x,ll y,ll z){
	add1(x,y,z);
	add1(y,x,0);
}
bl bfs(){
	memset(dep,0,sizeof(dep));
    q.push(s);
	dep[s]=1;
    while(!q.empty()){
    	ll u=q.front();
    	q.pop();
    	go(u){
    		ll v=e[i].to;
    		if(e[i].w&&!dep[v]){
    			dep[v]=dep[u]+1;
    			q.push(v);
    		}
    	}
    }
    return dep[t];
}
ll dfs(ll x,ll sum){
	if(x==t) return sum;
	ll k,res=0;
	go(x){
		ll v=e[i].to;
		if(e[i].w>0&&(dep[v]==dep[x]+1)){
			k=dfs(v,min(sum,e[i].w));
			if(!k) dep[v]=maxinf;
			e[i].w-=k;
			e[i^1].w+=k;
			res+=k;
			sum-=k;
		}
	}
	return res;
}
int main(){
	n=read();
	m=read();
	g=read();
	s=0;
	t=n+m+1;
	fr(i,1,n) a[i]=read();
	fr(i,1,n){
		ll v=read();
		if(!a[i]) add(s,i,v);
		else add(i,t,v);
	}
	fr(i,1,m){
		ll p=read(),w=read(),k=read();
		fr(j,1,k){
			ll x=read();
			if(!p) add(n+i,x,maxinf);
			else add(x,n+i,maxinf);
		}
		ll pp=read();
		if(!p) add(s,n+i,w+pp*g);
		else add(n+i,t,w+pp*g);
		ans+=w;
	}
//	write(ans);
//	puts(" ");
	while(bfs()) ans-=dfs(s,maxinf);
	write(ans);
}


#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	bl ccc(int a,int b){
		return a>b;
	}
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define sr(a,b,n) sort((a)+(b),(a)+(n)+(b))
	#define psr(a,b,n) sort((a)+(b),(a)+(n)+(b),ccc)
	#define maxinf 0xffffffff
	#define mininf -0xffffffff
	#define chs(a,b,c) (a)?(b):(c)
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
ll n,m,g,s,t,a[1000001],head[4000004],dep[4000004],cnt=1,ans=0;
queue<ll> q;
struct node{
	ll to,nxt,w;
}e[4000004];
void add1(ll x,ll y,ll z){
	e[++cnt].nxt=head[x];
	e[cnt].to=y;
	e[cnt].w=z;
	head[x]=cnt;
}
void add(ll x,ll y,ll z){
	add1(x,y,z);
	add1(y,x,0);
}
bl bfs(){
	memset(dep,0,sizeof(dep));
    q.push(s);
	dep[s]=1;
    while(!q.empty()){
    	ll u=q.front();
    	q.pop();
    	go(u){
    		ll v=e[i].to;
    		if(e[i].w&&!dep[v]){
    			dep[v]=dep[u]+1;
    			q.push(v);
    		}
    	}
    }
    return dep[t];
}
ll dfs(ll x,ll sum){
	if(x==t) return sum;
	ll k,res=0;
	go(x){
		ll v=e[i].to;
		if(e[i].w>0&&(dep[v]==dep[x]+1)){
			k=dfs(v,min(sum,e[i].w));
			if(!k) dep[v]=maxinf;
			e[i].w-=k;
			e[i^1].w+=k;
			res+=k;
			sum-=k;
		}
	}
	return res;
}
int main(){
	n=read();
	m=read();
	g=read();
	s=0;
	t=n+m+1;
	fr(i,1,n) a[i]=read();
	fr(i,1,n){
		ll v=read();
		if(!a[i]) add(s,i,v);
		else add(i,t,v);
	}
	fr(i,1,m){
		ll p=read(),w=read(),k=read();
		fr(j,1,k){
			ll x=read();
			if(!p) add(n+i,x,maxinf);
			else add(x,n+i,maxinf);
		}
		ll pp=read();
		if(!p) add(s,n+i,w+pp*g);
		else add(n+i,t,w+pp*g);
		ans+=w;
	}
	while(bfs()) ans-=dfs(s,maxinf);
	write(ans);
}


#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	bl ccc(int a,int b){
		return a>b;
	}
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define sr(a,b,n) sort((a)+(b),(a)+(n)+(b))
	#define psr(a,b,n) sort((a)+(b),(a)+(n)+(b),ccc)
	#define maxinf 0xffffffff
	#define mininf -0xffffffff
	#define chs(a,b,c) (a)?(b):(c)
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
ll n,m,g,s,t,a[1000001],head[4000004],dep[4000004],cnt=1,ans=0;
queue<ll> q;
struct node{
	ll to,nxt,w;
}e[4000004];
void add1(ll x,ll y,ll z){
	e[++cnt].nxt=head[x];
	e[cnt].to=y;
	e[cnt].w=z;
	head[x]=cnt;
}
void add(ll x,ll y,ll z){
	add1(x,y,z);
	add1(y,x,0);
}
bl bfs(){
	memset(dep,0,sizeof(dep));
    q.push(s);
	dep[s]=1;
    while(!q.empty()){
    	ll u=q.front();
    	q.pop();
    	go(u){
    		ll v=e[i].to;
    		if(e[i].w&&!dep[v]){
    			dep[v]=dep[u]+1;
    			q.push(v);
    		}
    	}
    }
    return dep[t];
}
ll dfs(ll x,ll sum){
	if(x==t||(!sum)) return sum;
	ll k,res=0;
	go(x){
		ll v=e[i].to;
		if(e[i].w>0&&(dep[v]==dep[x]+1)){
			k=dfs(v,min(sum,e[i].w));
			if(!k) dep[v]=maxinf;
			e[i].w-=k;
			e[i^1].w+=k;
			res+=k;
			sum-=k;
		}
	}
	return res;
}
int main(){
	n=read();
	m=read();
	g=read();
	s=0;
	t=n+m+1;
	fr(i,1,n) a[i]=read();
	fr(i,1,n){
		ll v=read();
		if(!a[i]) add(s,i,v);
		else add(i,t,v);
	}
	fr(i,1,m){
		ll p=read(),w=read(),k=read();
		fr(j,1,k){
			ll x=read();
			if(!p) add(n+i,x,maxinf);
			else add(x,n+i,maxinf);
		}
		ll pp=read();
		if(!p) add(s,n+i,w+pp*g);
		else add(n+i,t,w+pp*g);
		ans+=w;
	}
	while(bfs()) ans-=dfs(s,maxinf);
	write(ans);
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
#define int long long
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[10000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[100050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	assert(tot<=1e6);
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=from;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
vector<int> vec[100010];
signed main()
{//	freopen("a.in","r",stdin);
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(i,T,Val[i]);else add(S,i,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read()),vec[A[i].vec[j-1]].pb(i);
		int x=read();
		if(x==1) A[i].w+=g;
		if(!A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	cout<<Ans<<"\n";
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(!A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=n;++i)
	{
		int sz=vec[i].size();
		for(rint j=0;j<sz;++j)
		{
			for(rint k=j+1;k<sz;++k)
			{
				int a=vec[i][j],b=vec[i][k];
				if(A[a].S^A[b].S)
				{
					if(!A[a].S) add(a+n,b+n,1e9);
					else add(b+n,a+n,1e9);
				}
			}
		}
	}
	cout<<Ans-dinic();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
#define int long long
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[10000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[100050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	assert(tot<=1e6);
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=from;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
signed main()
{//	freopen("a.in","r",stdin);
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(i,T,Val[i]);else add(S,i,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read());
		int x=read();
		if(x==1) A[i].w+=g;
		if(!A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	cout<<Ans<<"\n";
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(!A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=m;++i)
	{
		int sz1=A[i].vec.size();
		for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=1;
		int fl=0;
		for(rint j=i+1;j<=m;++j)
		{
			if(A[j].S==A[i].S) continue;
			int sz2=A[j].vec.size();
			for(rint k=0;k<sz2;++k)
			if(vis[A[j].vec[k]]) fl=1;
			if(fl)
			{	if(!A[i].S) add(i+n,j+n,1e9);
				else add(j+n,i+n,1e9);
			}
		}for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=0;
	}
	cout<<Ans-dinic();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[10000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[100050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=from;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
int main()
{//	freopen("a.in","r",stdin);
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(i,T,Val[i]);else add(S,i,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read());
		int x=read();
		if(x==1) A[i].w+=g;
		if(!A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	cout<<Ans<<"\n";
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(!A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=m;++i)
	{
		int sz1=A[i].vec.size();
		for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=1;
		int fl=0;
		for(rint j=i+1;j<=m;++j)
		{
			if(A[j].S==A[i].S) continue;
			int sz2=A[j].vec.size();
			for(rint k=0;k<sz2;++k)
			if(vis[A[j].vec[k]]) fl=1;
			if(fl)
			{	if(!A[i].S) add(i+n,j+n,1e9);
				else add(j+n,i+n,1e9);
			}
		}for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=0;
	}
	cout<<Ans-dinic();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
#define int long long
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[10000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[100050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=from;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
signed main()
{//	freopen("a.in","r",stdin);
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(i,T,Val[i]);else add(S,i,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read());
		int x=read();
		if(x==1) A[i].w+=g;
		if(!A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	cout<<Ans<<"\n";
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(!A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=m;++i)
	{
		int sz1=A[i].vec.size();
		for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=1;
		int fl=0;
		for(rint j=i+1;j<=m;++j)
		{
			if(A[j].S==A[i].S) continue;
			int sz2=A[j].vec.size();
			for(rint k=0;k<sz2;++k)
			if(vis[A[j].vec[k]]) fl=1;
			if(fl)
			{	if(!A[i].S) add(i+n,j+n,1e9);
				else add(j+n,i+n,1e9);
			}
		}for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=0;
	}
	cout<<Ans-dinic();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[5000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[5050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=from;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
int main()
{//	freopen("a.in","r",stdin);
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(i,T,Val[i]);else add(S,i,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read());
		int x=read();
		if(x==1) A[i].w+=g;
		if(!A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	cout<<Ans<<"\n";
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(!A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=m;++i)
	{
		int sz1=A[i].vec.size();
		for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=1;
		int fl=0;
		for(rint j=i+1;j<=m;++j)
		{
			if(A[j].S==A[i].S) continue;
			int sz2=A[j].vec.size();
			for(rint k=0;k<sz2;++k)
			if(vis[A[j].vec[k]]) fl=1;
			if(fl)
			{	if(!A[i].S) add(i+n,j+n,1e9);
				else add(j+n,i+n,1e9);
			}
		}for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=0;
	}
	cout<<Ans-dinic();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[5000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[20050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=from;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
int main()
{//	freopen("a.in","r",stdin);
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(i,T,Val[i]);else add(S,i,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read());
		int x=read();
		if(x==1) A[i].w+=g;
		if(!A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	cout<<Ans<<"\n";
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(!A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=m;++i)
	{
		int sz1=A[i].vec.size();
		for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=1;
		int fl=0;
		for(rint j=i+1;j<=m;++j)
		{
			if(A[j].S==A[i].S) continue;
			int sz2=A[j].vec.size();
			for(rint k=0;k<sz2;++k)
			if(vis[A[j].vec[k]]) fl=1;
			if(fl)
			{	if(!A[i].S) add(i+n,j+n,1e9);
				else add(j+n,i+n,1e9);
			}
		}for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=0;
	}
	cout<<Ans-dinic();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[5000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[5050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=to;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
int main()
{
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(S,i,Val[i]);else add(i,T,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read());
		int x=read();
		if(x==1) A[i].w+=g;
		if(A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=m;++i)
	{
		int sz1=A[i].vec.size();
		for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=1;
		int fl=0;
		for(rint j=i+1;j<=m;++j)
		{
			if(A[j].S==A[i].S) continue;
			int sz2=A[j].vec.size();
			for(rint k=0;k<sz2;++k)
			if(vis[A[j].vec[k]]) fl=1;
			if(fl)
			{	if(A[i].S) add(i+n,j+n,1e9);
				else add(j+n,i+n,1e9);
			}
		}for(rint j=0;j<sz1;++j)
			vis[A[i].vec[j]]=0;
	}
	cout<<Ans-dinic();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
#define int long long
using namespace std;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
struct node{int to,next,flow;} e[10000100];
struct nd{int S,w;vector<int> vec;}A[100010];
int n,m,g,Sex[100010],Val[100010],S,T,dep[100010],vis[100050],tot=1,h[100010];
inline void add(int from,int to,int flow)
{
//	cout<<from<<" "<<to<<" "<<flow<<"\n";
	assert(tot<=1e6);
	e[++tot].next=h[from];h[from]=tot;e[tot].to=to;e[tot].flow=flow;
	e[++tot].next=h[to];h[to]=tot;e[tot].to=from;e[tot].flow=0;
}queue<int> q;
inline bool BFS()
{
	q.push(S);
	memset(vis,0,sizeof(vis));
	dep[S]=0;vis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(rint i=h[u];i;i=e[i].next)
		{
			int to=e[i].to;if(!e[i].flow) continue;if(vis[to])continue;
			dep[to]=dep[u]+1;vis[to]=1;
			q.push(to);
		}
	}return vis[T];
}
inline int DFS(int x,int y)
{
	if(x==T||!y) return y;
	int flow=0,f;
	for(rint i=h[x];i;i=e[i].next)
	{	int to=e[i].to;
		if(dep[to]==dep[x]+1&&((f=DFS(to,min(e[i].flow,y)))>0))
		{
			flow+=f;
			e[i].flow-=f;e[i^1].flow+=f;
			y-=f;
			if(!y) break;
		}
	}if(y) dep[x]=-1;
	return flow;
}
inline int dinic()
{
	int res=0;
	while(BFS())
		res+=DFS(S,1e9);
	return res;
}
vector<int> vec[100010];
signed main()
{//	freopen("a.in","r",stdin);
	n=read(),m=read(),g=read();
	S=n+m+1;T=S+1;
	for(rint i=1;i<=n;++i)
		Sex[i]=read();
	for(rint i=1;i<=n;++i)
		Val[i]=read();
	for(rint i=1;i<=n;++i)
		if(Sex[i]) add(i,T,Val[i]);else add(S,i,Val[i]);
	ll Ans=0;
	for(rint i=1;i<=m;++i)
	{
		A[i].S=read(),A[i].w=read();Ans+=A[i].w;
		int k=read();
		for(rint j=1;j<=k;++j) A[i].vec.pb(read()),vec[A[i].vec[j-1]].pb(i);
		int x=read();
		if(x==1) A[i].w+=g;
		if(!A[i].S) add(S,i+n,A[i].w);else add(i+n,T,A[i].w);
	}
//	cout<<Ans<<"\n";
//	for(rint i=1;i<=n;++i) add(i,T,Val[i]);
	for(rint i=1;i<=m;++i)
	{
//		add(S,i+n,A[i].w);
		int sz=A[i].vec.size();
		for(rint j=0;j<sz;++j)
			if(A[i].S!=Sex[A[i].vec[j]]) {if(!A[i].S)add(i+n,A[i].vec[j],1e9);else add(A[i].vec[j],i+n,1e9);}
	}
	for(rint i=1;i<=n;++i)
	{
		int sz=vec[i].size();
		for(rint j=0;j<sz;++j)
		{
			for(rint k=sz+1;k<sz;++k)
			{
				int a=vec[i][j],b=vec[i][k];
				if(A[a].S^A[b].S)
				{
					if(!A[a].S) add(a+n,b+n,1e9);
					else add(b+n,a+n,1e9);
				}
			}
		}
	}
	cout<<Ans-dinic();
	return 0;
}


#include <bits/stdc++.h>

inline int read()
{
    int x = 0;
    char ch = getchar();
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x;
}

namespace Graph
{
    #define pii std::pair<int, int>
    #define mkp(a, b) std::make_pair(a, b)
    const int inf = 1073741823;
    static const int N = 20010, M = 200010;
    int dis[N], pre[N], vis[N], flo[N], que[N * 10];
    int tot = 1, fir[N], nex[M], got[M], tak[M], cst[M];

    inline void AddEdge(int u, int v, int t, int w = 0)
    {
    	// printf("%d %d %d %d\n", u, v, t, w);
        nex[++tot] = fir[u], fir[u] = tot, got[tot] = v, tak[tot] = t, cst[tot] = w;
        nex[++tot] = fir[v], fir[v] = tot, got[tot] = u, tak[tot] = 0, cst[tot] = -w;
    }

    inline int bfs(int s, int t)
    {
        for (int i = 1; i <= t; ++i) dis[i] = 0;
        int l = 1, r = 1; dis[que[1] = s] = 1;
        while (l <= r)
        {
            int u = que[l++];
            for (int i = fir[u]; i; i = nex[i])
            {
                if (dis[got[i]] || !tak[i]) continue;
                dis[got[i]] = dis[u] + 1, que[++r] = got[i];
            }
        }
        return dis[t];
    }

    inline int dfs(int u, int t, int flow)
    {
        int rest = flow;
        if (u == t) return flow;
        for (int i = fir[u]; i && rest; i = nex[i])
        {
            if (dis[u] + 1 != dis[got[i]] || !tak[i]) continue;
            int tmp = dfs(got[i], t, std::min(tak[i], rest));
            if (!tmp) dis[got[i]] = 0;
            tak[i] -= tmp, tak[i ^ 1] += tmp, rest -= tmp;
        }
        return flow - rest;
    }

    inline int Maxflow(int s, int t)
    {
        int flow = 0;
        while (bfs(s, t)) flow += dfs(s, t, inf);
        return flow;
    }

    inline int spfa(int s, int t)
    {
        for (int i = 1; i <= t; i++) dis[i] = inf;
        int l = 1, r = 1; dis[s] = 0, flo[s] = inf, que[1] = s;
        while (l <= r)
        {
            int u = que[l++]; vis[u] = false;
            for (int i = fir[u]; i; i = nex[i])
            {
                int v = got[i], w = cst[i];
                if (!tak[i] || dis[u] + w >= dis[v]) continue;
                dis[v] = dis[u] + w, pre[v] = i;
                flo[v] = std::min(flo[u], tak[i]);
                if (!vis[v]) vis[v] = true, que[++r] = v;
            }
        }
        return dis[t] != inf;
    }

    inline pii MincostMaxflow(int s, int t)
    {
        int cost = 0, flow = 0;
        while (spfa(s, t))
        {
            cost += flo[t] * dis[t], flow += flo[t];
            for (int u = t; u != s; u = got[pre[u] ^ 1])
                tak[pre[u]] -= flo[t], tak[pre[u] ^ 1] += flo[t];
        }
        return mkp(flow, cost);
    }
}
using namespace Graph;

int col[N];
int main()
{
    int n = read(), m = read(), g = read();
    int s = n + m + 1, t = n + m + 2, ans = 0;
    for (int i = 1; i <= n; ++i) col[i] = read();
    for (int i = 1; i <= n; ++i) col[i] ? AddEdge(i, t, read()) : AddEdge(s, i, read());
    for (int i = 1; i <= m; ++i)
    {
        int c = read(), w = read(), k = read();
        for (int j = 1; j <= k; ++j)
            c ? AddEdge(read(), i + n, inf) : AddEdge(i + n, read(), inf);
        c ? AddEdge(i + n, t, read() * g + w) : AddEdge(s, i + n, read() * g + w);
        ans += w;
    }
    printf("%d\n", ans - Maxflow(s, t));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
template<unsigned N,unsigned M> struct maxflow
{
    int const inf=1e9;
    int maxflow,s,t,tot=1,target[M],pre[M],last[N],
    cap[M],deep[N],cur[N];
    queue<int>q;
    void add(int x,int y,int z)
    {
        target[++tot]=y;
        pre[tot]=last[x];
        last[x]=tot;
        cap[tot]=z;
        target[++tot]=x;
        pre[tot]=last[y];
        last[y]=tot;
    }
    bool bfs()
    {
        for(int i=s;i<=t;i++)deep[i]=0,cur[i]=last[i];
        q.push(s);
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            for(int i=last[now];i;i=pre[i])
            {
                int tar=target[i];
                if(tar!=s&&cap[i]&&(!deep[tar]))
                    deep[tar]=deep[now]+1,q.push(tar);
            }
        }
        return deep[t];
    }
    int dfs(int x,int lim)
    {
        if((!lim)||x==t)return lim;
        int flow=0;
        for(int i=cur[x];i;cur[x]=i,i=pre[i])
        {
            int tar=target[i];
            if(cap[i]&&deep[tar]==deep[x]+1)
            {
                int d=dfs(tar,min(lim,cap[i]));
                cap[i]-=d;
                cap[i^1]+=d;
                lim-=d;
                flow+=d;
                if(!lim)break;
            }
        }
        return flow;
    }
    void dinic(){while(bfs())maxflow+=dfs(s,inf);}
};
maxflow<15005,100005>graph;
int a[10005],v[10005];
int main()
{
    int n,m,g,x,op,w,k,t,sum=0;
    scanf("%d%d%d",&n,&m,&g);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    graph.t=n+m+1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&v[i]);
        if(!a[i])graph.add(0,i,v[i]);
        else graph.add(i,n+m+1,v[i]);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&w,&k);
        sum+=w;
        for(int j=1;j<=k;j++)
        {
            scanf("%d",&t);
            if(x==0)graph.add(n+i,t,1e9);
            else graph.add(t,n+i,1e9);
        }
        scanf("%d",&op);
        if(x==0)graph.add(0,n+i,w+op*g);
        else graph.add(n+i,n+m+1,w+op*g);
    }
    graph.dinic();
    printf("%d",sum-graph.maxflow);
    return 0;
}


/*====Corycle====*/
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=20005;
int read(){
	int s=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
	return s*f;
}
int n,m,g,Ans,tot,cnt=1;
int a[N],h[N],dis[N];
struct edge{int to,next,flow;}d[N*100];
void Add(int x,int y,int f){
	d[++cnt]=(edge){y,h[x],f};h[x]=cnt;
	d[++cnt]=(edge){x,h[y],0};h[y]=cnt;
}
bool BFS(int s,int t){
	for(int i=1;i<=tot;i++)dis[i]=-1;
	queue<int>q;q.push(s);dis[s]=0;
	while(q.size()){
		int x=q.front();q.pop();
		if(x==t)return true;
		for(int i=h[x];i;i=d[i].next){
			int y=d[i].to;
			if(d[i].flow&&dis[y]==-1){
				dis[y]=dis[x]+1;q.push(y);
			}
		}
	}
	return false;
}
int DFS(int x,int maxn,int t){
	if(x==t||!maxn)return maxn;
	int ans=0;
	for(int i=h[x];i;i=d[i].next){
		int y=d[i].to;
		if(d[i].flow&&dis[y]==dis[x]+1){
			int dlt=DFS(y,min(d[i].flow,maxn),t);
			if(!dlt)dis[y]=-1;
			d[i].flow-=dlt;
			d[i^1].flow+=dlt;
			maxn-=dlt;ans+=dlt;
			if(!maxn)return ans;
		}
	}
	return ans;
}
int Dinic(int s,int t){
	int ans=0;
	while(BFS(s,t))ans+=DFS(s,inf,t);
	return ans;
}
int main(){
//	freopen("_.in","r",stdin);
//	freopen("_.out","w",stdout);
	n=read();m=read();g=read();
	int S=n+1,T=n+2;tot=n+2;
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++){
		int x=read();
		if(a[i]==0){Add(S,i,0);Add(i,T,x);}
		if(a[i]==1){Add(S,i,x);Add(i,T,0);}
	}
	while(m--){
		tot++;
		int op=read(),w=read(),num=read();
		for(int i=1;i<=num;i++){
			int x=read();
			if(op==0)Add(x,tot,inf);
			if(op==1)Add(tot,x,inf);
		}
		int t=read();
		if(op==0)Add(tot,T,w+g*t);
		if(op==1)Add(S,tot,w+g*t);
		Ans+=w;
	}
	printf("%d",Ans-Dinic(S,T));
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=1e4+50,M=1e6+50,inf=0x3f3f3f3f;
int n,m,g,S,T,cnt,ans;bool a[N];
int ver[M],edge[M],nxt[M],head[M],tot=1,d[M],q[M],l,r,cur[M];
void add(int x,int y,int z){
	ver[++tot]=y;edge[tot]=z;nxt[tot]=head[x];head[x]=tot;
	ver[++tot]=x;edge[tot]=0;nxt[tot]=head[y];head[y]=tot;
}
bool bfs(){
	memset(d,0x3f,sizeof(int)*(cnt+1));
	memcpy(cur,head,sizeof(int)*(cnt+1));
	q[l=r=1]=S;d[S]=0;
	while(l<=r){
		int x=q[l++];
		for(int i=head[x],y;i;i=nxt[i])
			if(edge[i]&&d[y=ver[i]]>d[x]+1)
				d[y]=d[x]+1,q[++r]=y;
	}
	return d[T]!=inf;
}
int dfs(int x,int flow){
	if(x==T)return flow;int now=0;
	for(int &i=cur[x],y;i;i=nxt[i]){
		if(!edge[i]||d[y=ver[i]]!=d[x]+1)continue;
		int dat=dfs(y,min(flow,edge[i]));
		edge[i]-=dat;edge[i^1]+=dat;
		now+=dat;flow-=dat;
		if(!flow)return now;
	}
	return now;
}
int main(){
	scanf("%d%d%d",&n,&m,&g);S=n+m+1;T=cnt=n+m+2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1,v;i<=n;i++)
		scanf("%d",&v),a[i]?add(S,i,v):add(i,T,v);
	for(int i=n+1,op,w,k,oo;i<=n+m;i++){
		scanf("%d%d%d",&op,&w,&k);ans+=w;
		for(int j=1,x;j<=k;j++)
			scanf("%d",&x),op?add(i,x,inf):add(x,i,inf);
		scanf("%d",&oo);
		op?add(S,i,w+oo*g):add(i,T,w+oo*g);
	}
	while(bfs())ans-=dfs(S,inf);
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define M 60005
#define inf 0x3f3f3f3f

int n, m, s, t, sum, g;
int dep[M], iter[M], a[M], v[M];

struct Edge
{ int to, cap, rev; };

vector<Edge> G[M];

inline void add_edge(int u, int v, int cap)
{
//	cout << u << " --> " << v << " : " << cap << endl;
	G[u].push_back((Edge) {v, cap, G[v].size()});
	G[v].push_back((Edge) {u, 0, G[u].size()-1});
}

inline bool bfs()
{
	memset(dep, -1, sizeof(dep));
	dep[0] = 0; queue<int> q; q.push(s);
	while (!q.empty())
	{
		int x = q.front(), sz = G[x].size(); q.pop();
		for (int i = 0; i < sz; i++)
		{
			Edge e = G[x][i];
			if (dep[e.to] == -1 && e.cap)
				q.push(e.to), dep[e.to] = dep[x] + 1;
		}
	}
	return dep[t] != -1;
}

int dfs(int x, int t, int flow)
{
	if (x == t) return flow;
	int sz = G[x].size();
	for (int i = iter[x]; i < sz; i++)
	{
		Edge &e = G[x][i];
		if (e.cap && dep[e.to] == dep[x] + 1)
		{
			int d = dfs(e.to, t, min(flow, e.cap));
			if (d)
			{
				e.cap -= d; G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

int dinic()
{
	int res = 0, flow = 0;
	while (bfs())
	{
		memset(iter, 0, sizeof(iter));
		while ( (flow = dfs(s, t, inf)) != 0 )
			res += flow;
	}
	return res;
}

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

int main()
{
	n = read(), m = read(), g = read();
	s = 0, t = n * 2 + m + 1;
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i <= n; i++)
		v[i] = read();
	for (int i = 1; i <= n; i++)
	{
		if (a[i]) add_edge(s, i, v[i]);
		else add_edge(i, t, v[i]);
	}

	for (int i = 1; i <= m; i++)
	{
		int opt = read(), w = read(), num = read(); sum += w;
		if (opt == 0)
		{
			for (int j = 1; j <= num; j++)
				add_edge(read(), n + i, inf);
			int tmp = read();
			if (tmp == 1) add_edge(n + i, t, g + w);
			else add_edge(n + i, t, w);
		}
		if (opt == 1)
		{
			for (int j = 1; j <= num; j++)
				add_edge(n + i, read(), inf);
			int tmp = read();
			if (tmp == 1) add_edge(s, n + i, g + w);
			else add_edge(s, n + i, w);
		}
	}

	int tmp = dinic();
//	cout << tmp << endl;
	cout << sum - tmp;

	return 0;
}


#include <bits/stdc++.h>
#define LOCAL
using namespace std;

namespace STDquantum {

const int N = 2e6 + 10, inf = 0x3f3f3f3f;
int n, m, g, s, t, sum, tot = -1, a[N], cur[N], dis[N], head[N];
struct E {
  int next, to, w;
} e[N];
inline void add(int x, int y, int z) {
  e[++tot] = (E){head[x], y, z}, head[x] = tot;
  e[++tot] = (E){head[y], x, 0}, head[y] = tot;
}
inline bool bfs() {
  memset(dis, -1, sizeof(dis));
  deque<int> q;
  q.push_back(s);
  dis[s] = 0, cur[s] = head[s];
  while (!q.empty()) {
    int u = q.front();
    q.pop_front();
    for (int i = head[u], v; ~i; i = e[i].next) {
      if (dis[v = e[i].to] == -1 && e[i].w) {
        dis[v] = dis[u] + 1, cur[v] = head[v];
        if (v == t) return 1;
        q.push_back(v);
      }
    }
  }
  return 0;
}
inline int dfs(int u, int lim) {
  if (u == t) return lim;
  int flow = 0, delta;
  for (int i = cur[u], v; ~i && flow < lim; i = e[i].next) {
    cur[u] = i;
    if (dis[v = e[i].to] == dis[u] + 1 && e[i].w) {
      delta = dfs(v, min(e[i].w, lim - flow));
      if (!delta) dis[v] = -1;
      e[i].w -= delta, e[i ^ 1].w += delta;
      flow += delta;
    }
  }
  return flow;
}
inline int Dinic() {
  int tmp = 0;
  while (bfs()) tmp += dfs(s, inf);
  return tmp;
}
inline void main() {
  scanf("%d%d%d", &n, &m, &g);
  s = n + m + 1, t = n + m + 2;
  memset(head, -1, sizeof(head));
  for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); }
  for (int i = 1, x; i <= n; ++i) {
    scanf("%d", &x);
    if (a[i]) {
      add(i, t, x);
    } else {
      add(s, i, x);
    }
  }
  for (int i = 1, op, w, k, o, r[12]; i <= m; ++i) {
    scanf("%d%d%d", &op, &w, &k);
    sum += w;
    for (int j = 1; j <= k; ++j) { scanf("%d", r + j); }
    scanf("%d", &o);
    if (op) {
      add(n + i, t, w + o * g);
      for (int j = 1; j <= k; ++j) { add(r[j], n + i, inf); }
    } else {
      add(s, n + i, w + o * g);
      for (int j = 1; j <= k; ++j) { add(n + i, r[j], inf); }
    }
  }
  printf("%d", sum - Dinic());
}

}  // namespace STDquantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("test.in", "r", stdin);
  freopen("test.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("CF311E_Biologist.in", "r", stdin);
  freopen("CF311E_Biologist.out", "w", stdout);
#endif
#endif
  STDquantum::main();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;const int INF=(int)0x3f3f3f3f3f3f3f3f;
template<typename T>inline void read(T &x)
{
	x=0;char c=getchar(),f=0;
	for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=1;
	for(;c>='0'&&c<='9';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	if(f) x=-x;
}
template<typename T>inline void write(T x) {{if(x<0) putchar('-'),x=-x;}{if(x>9) write(x/10);}putchar(x%10+48);}
struct edge{int to,w,nxt;}e[500005];
int n,q,g,et,a[100005],head[100005],s,t,d[100005],cr[100005],sm=0;
inline void ADDE(int x,int y,int w) {e[++et]=(edge){y,w,head[x]},head[x]=et;}
inline void adde(int x,int y,int w) {ADDE(x,y,w),ADDE(y,x,0);}
inline char bfs(int s,int t)
{
	queue<int>q;memset(d,-1,sizeof(d)),d[s]=0,q.push(s);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for(int i=head[x];i;i=e[i].nxt) if(e[i].w&&!~d[e[i].to]) d[e[i].to]=d[x]+1,q.push(e[i].to);
	}
	return d[t]!=-1;
}
#define R(x) ((((x)&1)?1:-1)+(x))
inline int dfs(int x,int t,int lim=INF)
{
	int g=lim;if(x==t) return lim;
	for(int i=cr[x];i;cr[x]=i=e[i].nxt) if(d[e[i].to]==d[x]+1&&e[i].w)
	{
		int f=dfs(e[i].to,t,min(e[i].w,lim));
		e[i].w-=f,e[R(i)].w+=f,g-=f;if(g==0) break;
	}
	return lim-g;
}
inline int dinic(int s,int t) {int r=0;while(bfs(s,t)) memcpy(cr,head,sizeof(cr)),r+=dfs(s,t);return r;}
int main()
{
	read(n),read(q),read(g),s=n+q+1,t=s+1;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=1,v;i<=n;i++) {read(v);if(a[i]) adde(i,t,v);else adde(s,i,v);}
	for(int z,w,k,x,f,i=1;i<=q;i++)
	{
		read(z),read(w),read(k),sm+=w;
		if(z) while(k--) read(x),adde(x,i+n,INF);else while(k--) read(x),adde(i+n,x,INF);
		read(f);if(z) adde(i+n,t,w+f*g);else adde(s,i+n,w+f*g);
	}
	return write(sm-dinic(s,t)),putchar('\n'),0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;const int INF=(int)0x3f3f3f3f3f3f3f3f;
template<typename T>inline void read(T &x)
{
	x=0;char c=getchar(),f=0;
	for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=1;
	for(;c>='0'&&c<='9';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	if(f) x=-x;
}
template<typename T>inline void write(T x) {{if(x<0) putchar('-'),x=-x;}{if(x>9) write(x/10);}putchar(x%10+48);}
struct edge{int to,w,nxt;}e[500005];
int n,q,g,et,a[100005],head[100005],s,t,d[100005],cr[100005],sm=0;
inline void ADDE(int x,int y,int w) {e[++et]=(edge){y,w,head[x]},head[x]=et;}
inline void adde(int x,int y,int w) {ADDE(x,y,w),ADDE(y,x,0);}
inline char bfs(int s,int t)
{
	queue<int>q;memset(d,0,sizeof(d)),d[s]=1,q.push(s);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for(int i=head[x];i;i=e[i].nxt) if(e[i].w>0&&!d[e[i].to]) d[e[i].to]=d[x]+1,q.push(e[i].to);
	}
	return !!d[t];
}
#define R(x) ((((x)&1)?1:-1)+(x))
inline int dfs(int x,int t,int lim=INF)
{
	int g=lim;if(x==t) return lim;
	for(int i=cr[x];i;cr[x]=i=e[i].nxt) if(d[e[i].to]==d[x]+1&&e[i].w>0)
	{
		int f=dfs(e[i].to,t,min(e[i].w,g));
		e[i].w-=f,e[R(i)].w+=f,g-=f;if(g==0) break;
	}
	return lim-g;
}
inline int dinic(int s,int t) {int r=0;while(bfs(s,t)) memcpy(cr,head,sizeof(cr)),r+=dfs(s,t);return r;}
signed main()
{
	read(n),read(q),read(g),s=n+q+1,t=s+1;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=1,v;i<=n;i++) {read(v);if(a[i]) adde(i,t,v);else adde(s,i,v);}
	for(int z,w,k,x,f,i=1;i<=q;i++)
	{
		read(z),read(w),read(k),sm+=w;
		if(z) while(k--) read(x),adde(x,i+n,INF);else while(k--) read(x),adde(i+n,x,INF);
		read(f);if(z) adde(i+n,t,w+f*g);else adde(s,i+n,w+f*g);
	}
	return write(sm-dinic(s,t)),putchar('\n'),0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;const int INF=(int)0x3f3f3f3f3f3f3f3f;
template<typename T>inline void read(T &x)
{
	x=0;char c=getchar(),f=0;
	for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=1;
	for(;c>='0'&&c<='9';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	if(f) x=-x;
}
template<typename T>inline void write(T x) {{if(x<0) putchar('-'),x=-x;}{if(x>9) write(x/10);}putchar(x%10+48);}
struct edge{int to,w,nxt;}e[500005];
int n,q,g,et,a[100005],head[100005],s,t,d[100005],cr[100005],sm=0;
inline void ADDE(int x,int y,int w) {e[++et]=(edge){y,w,head[x]},head[x]=et;}
inline void adde(int x,int y,int w) {ADDE(x,y,w),ADDE(y,x,0);}
inline char bfs(int s,int t)
{
	queue<int>q;memset(d,-1,sizeof(d)),d[s]=0,q.push(s);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for(int i=head[x];i;i=e[i].nxt) if(e[i].w&&!~d[e[i].to]) d[e[i].to]=d[x]+1,q.push(e[i].to);
	}
	return d[t]!=-1;
}
#define R(x) ((((x)&1)?1:-1)+(x))
inline int dfs(int x,int t,int lim=INF)
{
	int g=lim;if(x==t) return lim;
	for(int i=cr[x];i;cr[x]=i=e[i].nxt) if(d[e[i].to]==d[x]+1&&e[i].w)
	{
		int f=dfs(e[i].to,t,min(e[i].w,lim));
		e[i].w-=f,e[R(i)].w+=f,g-=f;if(g==0) break;
	}
	return lim-g;
}
inline int dinic(int s,int t) {int r=0;while(bfs(s,t)) memcpy(cr,head,sizeof(cr)),r+=dfs(s,t);return r;}
signed main()
{
	read(n),read(q),read(g),s=n+q+1,t=s+1;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=1,v;i<=n;i++) {read(v);if(a[i]) adde(i,t,v);else adde(s,i,v);}
	for(int z,w,k,x,f,i=1;i<=q;i++)
	{
		read(z),read(w),read(k),sm+=w;
		if(z) while(k--) read(x),adde(x,i+n,INF);else while(k--) read(x),adde(i+n,x,INF);
		read(f);if(z) adde(i+n,t,w+f*g);else adde(s,i+n,w+f*g);
	}
	return write(sm-dinic(s,t)),putchar('\n'),0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e4 + 7;
const int MAXM = 4e4 + 7;
const int INF = 1e9 + 7;
struct Edge{
    int to, nxt, f;
} e[MAXM << 1];
int S, T, tot;
int ecnt, head[MAXN], cur[MAXN], dis[MAXN];
queue<int> q;
void init(int _S, int _T, int _tot) {
    ecnt = 1; S = _S; T = _T; tot = _tot;
    memset(head, 0, (tot + 1) * ( sizeof (int)));
}
void addEdge(int u, int v, int f) {
    e[++ecnt] = (Edge) {v, head[u], f}; head[u] = ecnt;
    e[++ecnt] = (Edge) {u, head[v], f}; head[v] = ecnt;
}
bool bfs() {
    memset(dis, 0, (tot + 1) * sizeof(int));
    q.push(S); dis[S] = 1;
    while(! q.empty()) {
        int u = q.front(), v; q.pop();
        for(int i = cur[u] = head[u]; i; i = e[i].nxt) {
            if(e[i].f && !dis[v = e[i].to]) {
                q.push(v);
                dis[v] = dis[u] + 1;
            }
        }
    }
    return dis[T];
}
int dfs(int u, int maxf) {
    if(u == T) return maxf;
    int sumf = maxf;
    for(int &i = cur[u]; i; i = e[i].nxt) {
        if(e[i].f && dis[e[i].to] > dis[u]) {
            int tmpf = dfs(e[i].to, min(sumf, e[i].f));
            e[i].f-= tmpf; e[i ^ 1].f += tmpf;
            sumf -= tmpf;
            if(!sumf) return maxf;
        }
    }
    return maxf - sumf;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n, m, g;
    cin >> n >> m >> g;
    init(n + 1, n + 2, n + m + 2);
    vector<int> col(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> col[i];
    }

    for(int i = 1, V; i <= n; i++) {
        cin >> V;
        if ( col[i] ) addEdge(S, i, V);
        else addEdge(i, T, V);
    }
    int sum = 0;
    for(int i = 1; i <= m; i++) {
        int u = n + 2 + i;
        int tgi, wi, ki;
        cin >> tgi >> wi >> ki;
        sum += wi;
        if ( tgi ) {
            for(int j = 1, v; j <= ki; j++) {
                cin >> v;
                addEdge(u, v, INF);
            }
            int t01; cin >> t01;
            if(t01) addEdge(S, u, wi + g); else addEdge(S, u, wi);
        }else {
            for(int j = 1, v; j <= ki; j++) {
                cin >> v;
                addEdge(v, u, INF);
            }
            int t01; cin >> t01;
            if(t01) addEdge(u, T, wi + g) ;else addEdge(u, T, wi);
        }
    }
    while(bfs()) sum -= dfs(S, INF);
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e4 + 7;
const int MAXM = 4e4 + 7;
const int INF = 1e9 + 7;
struct Edge{
    int to, nxt, f;
} e[MAXM << 1];
int S, T, tot;
int ecnt, head[MAXN], cur[MAXN], dis[MAXN];
queue<int> q;
void init(int _S, int _T, int _tot) {
    ecnt = 1; S = _S; T = _T; tot = _tot;
    memset(head, 0, (tot + 1) * ( sizeof (int)));
}
void addEdge(int u, int v, int f) {
    e[++ecnt] = (Edge) {v, head[u], f}; head[u] = ecnt;
    e[++ecnt] = (Edge) {u, head[v], 0}; head[v] = ecnt;
}
bool bfs() {
    memset(dis, 0, (tot + 1) * sizeof(int));
    q.push(S); dis[S] = 1;
    while(! q.empty()) {
        int u = q.front(), v; q.pop();
        for(int i = cur[u] = head[u]; i; i = e[i].nxt) {
            if(e[i].f && !dis[v = e[i].to]) {
                q.push(v);
                dis[v] = dis[u] + 1;
            }
        }
    }
    return dis[T];
}
int dfs(int u, int maxf) {
    if(u == T) return maxf;
    int sumf = maxf;
    for(int &i = cur[u]; i; i = e[i].nxt) {
        if(e[i].f && dis[e[i].to] > dis[u]) {
            int tmpf = dfs(e[i].to, min(sumf, e[i].f));
            e[i].f-= tmpf; e[i ^ 1].f += tmpf;
            sumf -= tmpf;
            if(!sumf) return maxf;
        }
    }
    return maxf - sumf;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n, m, g;
    cin >> n >> m >> g;
    init(n + 1, n + 2, n + m + 2);
    vector<int> col(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> col[i];
    }

    for(int i = 1, V; i <= n; i++) {
        cin >> V;
        if ( col[i] ) addEdge(S, i, V);
        else addEdge(i, T, V);
    }
    int sum = 0;
    for(int i = 1; i <= m; i++) {
        int u = n + 2 + i;
        int tgi, wi, ki;
        cin >> tgi >> wi >> ki;
        sum += wi;
        if ( tgi ) {
            for(int j = 1, v; j <= ki; j++) {
                cin >> v;
                addEdge(u, v, INF);
            }
            int t01; cin >> t01;
            if(t01) addEdge(S, u, wi + g); else addEdge(S, u, wi);
        }else {
            for(int j = 1, v; j <= ki; j++) {
                cin >> v;
                addEdge(v, u, INF);
            }
            int t01; cin >> t01;
            if(t01) addEdge(u, T, wi + g) ;else addEdge(u, T, wi);
        }
    }
    while(bfs()) sum -= dfs(S, INF);
    cout << sum << endl;
    return 0;
}


#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#include <cstring>
#define MAXN 10005
#define MAXM 2005
#define INF 0x3f3f3f3f
#define MIN(a, b) (((a) < (b))?(a):(b))
using namespace std;

struct RichMan {
    int sex, pay, isFriend;
    vector<int> dogs;
};

const int start = 2 * MAXN - 2, fin = 2 * MAXN - 1;
map< pair<int, int>, int > fx, cap;
vector<int> G[2 * MAXN];
vector<int> bkChanges;

int n, m, g, male[MAXN], sol;
int costChange[MAXN];
RichMan rm[MAXM];

int bk[2 * MAXN];

inline void revertChanges() {
    while(!bkChanges.empty()) {
        bk[bkChanges.back()] = 0;
        bkChanges.pop_back();
    }
}

bool BFS() {
    queue<int> q;

    revertChanges();

    q.push(start);
    bk[start] = -1;
    bkChanges.push_back(start);

    while(!q.empty()) {
        int x = q.front();
        q.pop();

        if(x == fin)
            continue;

        for(auto y: G[x]) {
            if(!bk[y] && fx[make_pair(x, y)] < cap[make_pair(x, y)]) {
                bkChanges.push_back(y);
                bk[y] = x;
                q.push(y);
            }
        }
    }

    return bk[fin];
}

int main() {

    scanf("%d %d %d", &n, &m, &g);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &male[i]);

    for(int i = 1; i <= n; ++i)
        scanf("%d", &costChange[i]);

    for(int i = 1; i <= m; ++i) {
        int nDogs, x;
        scanf("%d %d %d", &rm[i].sex, &rm[i].pay, &nDogs);

        for(int j = 0; j < nDogs; ++j) {
            scanf("%d", &x);
            rm[i].dogs.push_back(x);
        }

        scanf("%d", &rm[i].isFriend);

        sol += rm[i].pay;
    }

    for(int i = 1; i <= n; ++i) {
        if(male[i]) {
            G[i].push_back(fin);
            G[fin].push_back(i);
            cap[make_pair(i, fin)] = costChange[i];
        }
        else {
            G[start].push_back(i);
            G[i].push_back(start);
            cap[make_pair(start, i)] = costChange[i];
        }
    }

    for(int i = 1; i <= m; ++i) {
        if(rm[i].sex) {
            G[n + i].push_back(fin);
            G[fin].push_back(n + i);
            cap[make_pair(n + i, fin)] = rm[i].pay + ((rm[i].isFriend)?g:0);

            for(auto dog: rm[i].dogs) {
                G[dog].push_back(n + i);
                G[n + i].push_back(dog);
                cap[make_pair(dog, n + i)] = INF;
            }
        }
        else {
            G[start].push_back(n + i);
            G[n + i].push_back(start);
            cap[make_pair(start, n + i)] = rm[i].pay + ((rm[i].isFriend)?g:0);

            for(auto dog: rm[i].dogs) {
                G[n + i].push_back(dog);
                G[dog].push_back(n + i);
                cap[make_pair(n + i, dog)] = INF;
            }
        }
    }

    while(BFS()) {
        for(auto x: G[fin]) {
            if(bk[x]) {
                int f = cap[make_pair(x, fin)] - fx[make_pair(x, fin)];
                if(!f) continue;

                int y = x;
                while(y != start) {
                    f = MIN(f, cap[make_pair(bk[y], y)] - fx[make_pair(bk[y], y)]);
                    y = bk[y];
                }

                if(!f) continue;

                sol -= f;
                fx[make_pair(x, fin)] += f;

                y = x;
                while(y != start) {
                    fx[make_pair(bk[y], y)] += f;
                    y = bk[y];
                }
            }
        }
    }

    printf("%d\n", sol);

    return 0;
}


#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#include <cstring>
#define MAXN 10005
#define MAXM 2005
#define INF 0x3f3f3f3f
#define MIN(a, b) (((a) < (b))?(a):(b))
using namespace std;

struct RichMan {
    int sex, pay, isFriend;
    vector<int> dogs;
};

const int start = 2 * MAXN - 2, fin = 2 * MAXN - 1;
map< pair<int, int>, int > fx, cap;
vector<int> G[2 * MAXN];
vector<int> bkChanges;

int n, m, g, male[MAXN], sol;
int costChange[MAXN];
RichMan rm[MAXM];

int bk[2 * MAXN];

inline void revertChanges() {
    while(!bkChanges.empty()) {
        bk[bkChanges.back()] = 0;
        bkChanges.pop_back();
    }
}

bool BFS() {
    queue<int> q;

    revertChanges();

    q.push(start);
    bk[start] = -1;
    bkChanges.push_back(start);

    while(!q.empty()) {
        int x = q.front();
        q.pop();

        if(x == fin)
            continue;

        for(auto y: G[x]) {
            if(!bk[y] && fx[make_pair(x, y)] < cap[make_pair(x, y)]) {
                bkChanges.push_back(y);
                bk[y] = x;
                q.push(y);
            }
        }
    }

    return bk[fin];
}

int main() {

    scanf("%d %d %d", &n, &m, &g);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &male[i]);

    for(int i = 1; i <= n; ++i)
        scanf("%d", &costChange[i]);

    for(int i = 1; i <= m; ++i) {
        int nDogs, x;
        scanf("%d %d %d", &rm[i].sex, &rm[i].pay, &nDogs);

        for(int j = 0; j < nDogs; ++j) {
            scanf("%d", &x);
            rm[i].dogs.push_back(x);
        }

        scanf("%d", &rm[i].isFriend);

        sol += rm[i].pay;
    }

    for(int i = 1; i <= n; ++i) {
        if(male[i]) {
            G[i].push_back(fin);
            G[fin].push_back(i);
            cap[make_pair(i, fin)] = costChange[i];
        }
        else {
            G[start].push_back(i);
            G[i].push_back(start);
            cap[make_pair(start, i)] = costChange[i];
        }
    }

    for(int i = 1; i <= m; ++i) {
        if(rm[i].sex) {
            G[n + i].push_back(fin);
            G[fin].push_back(n + i);
            cap[make_pair(n + i, fin)] = rm[i].pay + ((rm[i].isFriend)?g:0);

            for(auto dog: rm[i].dogs) {
                G[dog].push_back(n + i);
                G[n + i].push_back(dog);
                cap[make_pair(dog, n + i)] = INF;
            }
        }
        else {
            G[start].push_back(n + i);
            G[n + i].push_back(start);
            cap[make_pair(start, n + i)] = rm[i].pay + ((rm[i].isFriend)?g:0);

            for(auto dog: rm[i].dogs) {
                G[n + i].push_back(dog);
                G[dog].push_back(n + i);
                cap[make_pair(n + i, dog)] = INF;
            }
        }
    }

    while(BFS()) {
        for(auto x: G[fin]) {
            if(bk[x]) {
                int f = cap[make_pair(x, fin)] - fx[make_pair(x, fin)];
                if(!f) continue;

                int y = x;
                while(y != start) {
                    f = MIN(f, cap[make_pair(bk[y], y)] - fx[make_pair(bk[y], y)]);
                    y = bk[y];
                }

                if(!f) continue;

                sol -= f;
                fx[make_pair(x, fin)] += f;
                fx[make_pair(fin, x)] -= f;

                y = x;
                while(y != start) {
                    fx[make_pair(bk[y], y)] += f;
                    fx[make_pair(y, bk[y])] -= f;
                    y = bk[y];
                }
            }
        }
    }

    printf("%d\n", sol);

    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

const int N=2e5+5;
const int inf=1e9;

int n,m,head[N],ans,lev[N];
int cnt,val[N],a[N],c[N],s,t;

struct nood
{
	int nex,to;
};
nood e[N<<1];

inline void jia(int u,int v,int w)
{
	e[++cnt].nex=head[u];
	head[u]=cnt;
	e[cnt].to=v;
	val[cnt]=w;
}

inline bool bfs()
{
	queue<int>q;
	memset(lev,-1,sizeof(lev));
	lev[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for ( int i=head[u];i;i=e[i].nex )
		{
			int v=e[i].to;
			if(lev[v]==-1&&val[i])
			{
				lev[v]=lev[u]+1;
				q.push(v);
			}
		}
	}
	return (lev[t]!=-1);
}

inline int dinic(int x,int fl)
{
	if(x==t) return fl;
	int now=fl;
	for ( int i=head[x];i;i=e[i].nex )
	{
		int v=e[i].to;
		if(now<=0) break;
		if(val[i]&&lev[v]==lev[x]+1)
		{
			int mf=dinic(v,min(now,val[i]));
			now-=mf;
			val[i]-=mf;
			val[i^1]+=mf;
		}
	}
	return fl-now;
}

signed main()
{
	n=read();
	m=read();
	int g=read();
	s=0,t=n+m+1;
	for ( int i=1;i<=n;i++ ) a[i]=read();
	for ( int i=1;i<=n;i++ )
	{
		c[i]=read();
		if(a[i]) jia(i,t,c[i]),jia(t,i,0);
		else jia(s,i,c[i]),jia(i,s,0);
	}
	for ( int i=1;i<=m;i++ )
	{
		int op,w,ok,gs;
		op=read(),w=read();
		gs=read();
		ans+=w;
		if(op)
		{
			for ( int j=1;j<=gs;j++ )
			{
				int z=read();
				jia(z,i+n,inf);
				jia(i+n,z,0);
			}
			ok=read();
			if(ok) jia(i+n,t,g+w),jia(t,i+n,0);
			else jia(i+n,t,w),jia(t,i+n,0);
		}
		else
		{
			for ( int j=1;j<=gs;j++ )
			{
				int z=read();
				jia(i+n,z,inf);
				jia(z,i+n,0);
			}
			ok=read();
			if(ok) jia(s,i+n,g+w),jia(i+n,s,0);
			else jia(s,i+n,w),jia(i+n,s,0);
		}
	}
	int sum=0;
	while(bfs()) sum+=dinic(s,inf);
	printf("%lld\n",ans-sum);
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

const int N=2e5+5;
const int inf=1e9;

int n,m,head[N],ans,lev[N];
int cnt=1,val[N],a[N],c[N],s,t;

struct nood
{
	int nex,to;
};
nood e[N<<1];

inline void jia(int u,int v,int w)
{
	e[++cnt].nex=head[u];
	head[u]=cnt;
	e[cnt].to=v;
	val[cnt]=w;
}

inline bool bfs()
{
	queue<int>q;
	memset(lev,-1,sizeof(lev));
	lev[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for ( int i=head[u];i;i=e[i].nex )
		{
			int v=e[i].to;
			if(lev[v]==-1&&val[i])
			{
				lev[v]=lev[u]+1;
				q.push(v);
			}
		}
	}
	return (lev[t]!=-1);
}

inline int dinic(int x,int fl)
{
	if(x==t) return fl;
	int now=fl;
	for ( int i=head[x];i;i=e[i].nex )
	{
		int v=e[i].to;
		if(now<=0) break;
		if(val[i]&&lev[v]==lev[x]+1)
		{
			int mf=dinic(v,min(now,val[i]));
			now-=mf;
			val[i]-=mf;
			val[i^1]+=mf;
		}
	}
	return fl-now;
}

signed main()
{
	n=read();
	m=read();
	int g=read();
	s=0,t=n+m+1;
	for ( int i=1;i<=n;i++ ) a[i]=read();
	for ( int i=1;i<=n;i++ )
	{
		c[i]=read();
		if(a[i]) jia(i,t,c[i]),jia(t,i,0);
		else jia(s,i,c[i]),jia(i,s,0);
	}
	for ( int i=1;i<=m;i++ )
	{
		int op,w,ok,gs;
		op=read(),w=read();
		gs=read();
		ans+=w;
		if(op)
		{
			for ( int j=1;j<=gs;j++ )
			{
				int z=read();
				jia(z,i+n,inf);
				jia(i+n,z,0);
			}
			ok=read();
			if(ok) jia(i+n,t,g+w),jia(t,i+n,0);
			else jia(i+n,t,w),jia(t,i+n,0);
		}
		else
		{
			for ( int j=1;j<=gs;j++ )
			{
				int z=read();
				jia(i+n,z,inf);
				jia(z,i+n,0);
			}
			ok=read();
			if(ok) jia(s,i+n,g+w),jia(i+n,s,0);
			else jia(s,i+n,w),jia(i+n,s,0);
		}
	}
	int sum=0;
	while(bfs()) sum+=dinic(s,inf);
	printf("%lld\n",ans-sum);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e4 +5;
const int maxm = 1e5 +5;
int n, m, s, t, tot = 1, ans, lev[maxn], head[maxn];
struct node {int to, z, nxt;} e[maxm << 1];
void add(int x, int y, int z) {
	e[++ tot] = {y, z, head[x]}; head[x] = tot;
	e[++ tot] = {x, 0, head[y]}; head[y] = tot;
}
bool bfs() {
	bool sign = 0;
	memset(lev, 0, sizeof(lev));
	queue <int> q;
	lev[s] = 1;
	q.push(s);
	while(q.size()) {
		int x = q.front(); q.pop();
		for(int i = head[x]; i; i = e[i].nxt) {
			int y = e[i].to, z = e[i].z;
			if(z == 0 || lev[y]) continue;
			lev[y] = lev[x] + 1, q.push(y);
			if(y == t) return 1;
		}
	}
	return 0;
}
int dinic(int x, int flow) {
	if(x == t) return flow;
	int res = flow, inc;
	for(int i = head[x]; i && res; i = e[i].nxt) {
		int y = e[i].to, z = e[i].z;
		if(z && lev[y] == lev[x] + 1) {
			inc = dinic(y, min(res, z));
			if(inc == 0) lev[y] = 0;
			e[i].z -= inc, e[i ^ 1].z += inc;
			res -= inc;
		}
	}
	return flow - res;
}
int g, a[maxm], v[maxn];
signed main() {
	s = 0, t = maxn - 5;
	scanf("%lld%lld%lld", &n, &m, &g);
	for(int i = 1;i <= n;i ++) scanf("%lld", &a[i]);
	for(int i = 1, x;i <= n;i ++) {
		scanf("%lld", &x);
		if(a[i]) add(s, i, x);
		else add(i, t, x);
	}

	int sum = 0;

	for(int i = 1, op, k, neg, x, y, z;i <= m;i ++) {
		scanf("%lld%lld%lld", &op, &z, &k);
		sum += z;
		for(int j = 1;j <= k;j ++) {
			scanf("%lld", &x);
			if(op)
				add(n + i, x, 1e17);
			else
				add(x, n + i, 1e17);
		}
		scanf("%lld", &neg);
		if(op)
			add(s, n + i, z + g * neg);
		else
			add(n + i, t, z + g * neg);
	}
	int res = 0;
	while(bfs()) {
		res = dinic(s, 1e17);
		while(res) {
			ans += res;
			res = dinic(s, 1e17);
		}
	}
	cout << sum - ans << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 +5;
const int maxm = 2e5 +5;
int n, m, s, t, tot = 1, ans, lev[maxn], head[maxn];
struct node {int to, z, nxt;} e[maxm << 1];
void add(int x, int y, int z) {
	e[++ tot] = {y, z, head[x]}; head[x] = tot;
	e[++ tot] = {x, 0, head[y]}; head[y] = tot;
}
bool bfs() {
	bool sign = 0;
	memset(lev, 0, sizeof(lev));
	queue <int> q;
	lev[s] = 1;
	q.push(s);
	while(q.size()) {
		int x = q.front(); q.pop();
		for(int i = head[x]; i; i = e[i].nxt) {
			int y = e[i].to, z = e[i].z;
			if(z == 0 || lev[y]) continue;
			lev[y] = lev[x] + 1, q.push(y);
			if(y == t) return 1;
		}
	}
	return 0;
}
int dinic(int x, int flow) {
	if(x == t) return flow;
	int res = flow, inc;
	for(int i = head[x]; i && res; i = e[i].nxt) {
		int y = e[i].to, z = e[i].z;
		if(z && lev[y] == lev[x] + 1) {
			inc = dinic(y, min(res, z));
			if(inc == 0) lev[y] = 0;
			e[i].z -= inc, e[i ^ 1].z += inc;
			res -= inc;
		}
	}
	return flow - res;
}
int g, a[maxm], v[maxn];
signed main() {
	s = 0, t = maxn - 5;
	scanf("%lld%lld%lld", &n, &m, &g);
	for(int i = 1;i <= n;i ++) scanf("%lld", &a[i]);
	for(int i = 1, x;i <= n;i ++) {
		scanf("%lld", &x);
		if(a[i]) add(s, i, x);
		else add(i, t, x);
	}

	int sum = 0;

	for(int i = 1, op, k, neg, x, y, z;i <= m;i ++) {
		scanf("%lld%lld%lld", &op, &z, &k);
		sum += z;
		for(int j = 1;j <= k;j ++) {
			scanf("%lld", &x);
			if(op)
				add(n + i, x, 1e17);
			else
				add(x, n + i, 1e17);
		}
		scanf("%lld", &neg);
		if(op)
			add(s, n + i, z + g * neg);
		else
			add(n + i, t, z + g * neg);
	}
	int res = 0;
	while(bfs()) {
		res = dinic(s, 1e17);
		while(res) {
			ans += res;
			res = dinic(s, 1e17);
		}
	}
	cout << sum - ans << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e4 +5;
const int maxm = 1e5 +5;
int n, m, s, t, tot = 1, ans, lev[maxn], head[maxn];
struct node {int to, z, nxt;} e[maxm << 1];
void add(int x, int y, int z) {
	e[++ tot] = {y, z, head[x]}; head[x] = tot;
	e[++ tot] = {x, 0, head[y]}; head[y] = tot;
}
bool bfs() {
	bool sign = 0;
	memset(lev, 0, sizeof(lev));
	queue <int> q;
	lev[s] = 1;
	q.push(s);
	while(q.size()) {
		int x = q.front(); q.pop();
		for(int i = head[x]; i; i = e[i].nxt) {
			int y = e[i].to, z = e[i].z;
			if(z == 0 || lev[y]) continue;
			lev[y] = lev[x] + 1, q.push(y);
			if(y == t) return 1;
		}
	}
	return 0;
}
int dinic(int x, int flow) {
	if(x == t) return flow;
	int res = flow, inc;
	for(int i = head[x]; i && res; i = e[i].nxt) {
		int y = e[i].to, z = e[i].z;
		if(z && lev[y] == lev[x] + 1) {
			inc = dinic(y, min(res, z));
			if(inc == 0) lev[y] = 0;
			e[i].z -= inc, e[i ^ 1].z += inc;
			res -= inc;
		}
	}
	return flow - res;
}
int g, a[maxm], v[maxn];
signed main() {
	s = 0, t = maxn - 5;
	scanf("%lld%lld%lld", &n, &m, &g);
	for(int i = 1;i <= n;i ++) scanf("%lld", &a[i]);
	for(int i = 1, x;i <= n;i ++) {
		scanf("%lld", &x);
		if(a[i]) add(s, i, x);
		else add(i, t, x);
	}

	int sum = 0;

	for(int i = 1, op, k, neg, x, y, z;i <= m;i ++) {
		scanf("%lld%lld%lld", &op, &z, &k);
		sum += z;
		for(int j = 1;j <= k;j ++) {
			scanf("%lld", &x);
			if(op)
				add(n + i, x, 1e9);
			else
				add(x, n + i, 1e9);
		}
		scanf("%d", &neg);
		if(op)
			add(s, n + i, z + g * neg);
		else
			add(n + i, t, z + g * neg);
	}

	int res = 0;
	while(bfs()) {
		res = dinic(s, 1e9);
		while(res) {
			ans += res;
			res = dinic(s, 1e9);
		}
	}

	cout << sum - ans << "\n";

	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define ll long long
using namespace std;
template <typename T>
void read(T &x) {
	x = 0; char c = getchar(); int f = 1;
	for (; !isdigit(c); c = getchar())
		if (c == '-') f = -1;
	for (; isdigit(c); c = getchar())
		x = x * 10 + (c ^ '0');
	x *= f;
}

const int N = 200500;
int h[N], ne[N<<1], to[N<<1], w[N<<1],  tot = 1;
inline void adde(int x, int y, int z) {
	ne[++tot] = h[x], to[h[x] = tot] = y, w[tot] = z;
}

inline void add(int x, int y, int z) {
	adde(x, y, z), adde(y, x, 0);
}

int dep[N], cur[N], cnt, s, t, m, n, g;

queue<int> q;
bool bfs(void) {
	memset(dep, 0, cnt *  4 + 200);
	q.push(s); dep[s] = 1;
	while (q.size()) {
		int x = q.front(); q.pop();
		for (int i = h[x]; i; i = ne[i]) {
			int y = to[i];
			if (dep[y] || !w[i]) continue;
			dep[y] = dep[x] + 1, q.push(y);
		}
	}
	if (!dep[t]) return 0;
	for (int i = 1;i <= cnt; i++) cur[i] = h[i];
	return 1;
}

int dfs(int x, int lim) {
	if (x == t || !lim) return lim;
	int res = 0, f;
	for (int &i = cur[x]; i; i = ne[i]) {
		int y = to[i]; if (!w[i] || dep[y] != dep[x] + 1) continue;
		f = dfs(y, min(lim, w[i]));
		res += f, lim -= f;
		w[i] -= f, w[i^1] += f;
		if (!lim) return res;
	}
	return res;
}

int a[N], v[N];
const int inf = 1e8;
int main() {
	read(n), read(m), read(g); cnt = n, s = ++cnt, t = ++cnt;
	for (int i = 1;i <= n; i++) read(a[i]);
	for (int i = 1, x;i <= n; i++) {
		read(x);
		if (a[i]) add(s, i, x);
		else add(i, t, x);
	}
	ll ans = 0;
	for (int i = 1, v, w, k;i <= m; i++) {
		read(v), read(w), read(k);
		ans += w; int num = ++cnt;
		for (int j = 1, x;j <= k; j++) {
			read(x);
			v ? add(num, x, inf) : add(x, num, inf);
		}
		read(k); w += k * g;
		v ? add(s, num, w) : add(num, t, w);
	}
	while (bfs()) ans -= dfs(s, inf);
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <queue>
#define SZ 200009
#define inf 0x3f3f3f3f
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) > (y) ? (x) : (y)
using namespace std;

int dis[SZ], cur[SZ], ans;
int n, m, G, idx, S, T, a[SZ], hd[SZ], cnt;
struct Edge{int to, nxt, f; } edge[SZ << 1];
struct DT {
	int dis, u;
	friend bool operator<(DT x, DT y)
		{return x.dis > y.dis; }
} ;
priority_queue <DT> q;

int read();
void add(int u, int v, int f) {
	edge[++cnt] = (Edge){v, hd[u], f}; hd[u] = cnt;
	edge[++cnt] = (Edge){u, hd[v], 0}; hd[v] = cnt;
}
bool bfs() {
	for (int i = 1; i <= idx; ++i) dis[i] = inf, cur[i] = hd[i];
	dis[S] = 0; q.push((DT){0, S});
	while (q.size()) {
		DT now = q.top(); q.pop(); int u = now.u;
		if (dis[u] < now.dis) continue;
		for (int i = hd[u], v; i; i = edge[i].nxt)
			if (edge[i].f && dis[v = edge[i].to] > dis[u] + 1)
				{dis[v] = dis[u] + 1; q.push((DT){dis[v], v}); }
	} return dis[T] < inf;
}
int dfs(int u, int flow, int ret = 0) {
	if (!flow || u == T) return flow;
	for (int i = cur[u], v; i; i = edge[i].nxt) {
		cur[u] = i; v = edge[i].to;
		int nf = Min(flow, edge[i].f), fl;
		if (dis[v] == dis[u] + 1 && (fl = dfs(v, nf)))
			edge[i].f -= fl, flow -= fl, ret += fl,
			edge[1 + (i - 1 ^ 1)].f + fl;
		if (!flow) return ret;
	} return ret;
}

int main() {
	freopen("a.in", "r", stdin);
	n = read(), m = read(), G = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	idx = n; S = ++idx; T = ++idx;
	for (int i = 1, vi; i <= n; ++i) {
		vi = read();
		if (a[i]) add(i, T, vi);
		else add(S, i, vi);
	}
	for (int i = 1; i <= m; ++i) {
		int kd = read(), wi = read(), ki = read();
		++idx; ans += wi;
		for (int j = 1, ai; j <= ki; ++j) {
			ai = read();
			if (kd) add(ai, idx, inf);
			else add(idx, ai, inf);
		}
		int ad = read();
		if (kd) add(idx, T, wi + G * ad);
		else add(S, idx, wi + G * ad);
	}
	while (bfs())
		ans -= dfs(S, inf);
	printf("%d\n", ans);
	return 0;
}

int read() {
	int x = 0, f = 1; char c = getchar();
	while (c > '9' || c < '0') f = (c == '-') ? -1 : f, c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}


#include <cstdio>
#include <queue>
#define SZ 200009
#define inf 0x3f3f3f3f
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) > (y) ? (x) : (y)
using namespace std;

int dis[SZ], cur[SZ], ans;
int n, m, G, idx, S, T, a[SZ], hd[SZ], cnt;
struct Edge{int to, nxt, f; } edge[SZ << 1];
struct DT {
	int dis, u;
	friend bool operator<(DT x, DT y)
		{return x.dis > y.dis; }
} ;
priority_queue <DT> q;

int read();
void add(int u, int v, int f) {
	edge[++cnt] = (Edge){v, hd[u], f}; hd[u] = cnt;
	edge[++cnt] = (Edge){u, hd[v], 0}; hd[v] = cnt;
}
bool bfs() {
	for (int i = 1; i <= idx; ++i) dis[i] = inf, cur[i] = hd[i];
	dis[S] = 0; q.push((DT){0, S});
	while (q.size()) {
		DT now = q.top(); q.pop(); int u = now.u;
		if (dis[u] < now.dis) continue;
		for (int i = hd[u], v; i; i = edge[i].nxt)
			if (edge[i].f && dis[v = edge[i].to] > dis[u] + 1)
				{dis[v] = dis[u] + 1; q.push((DT){dis[v], v}); }
	} return dis[T] < inf;
}
int dfs(int u, int flow, int ret = 0) {
	if (!flow || u == T) return flow;
	for (int i = cur[u], v; i; i = edge[i].nxt) {
		cur[u] = i; v = edge[i].to;
		int nf = Min(flow, edge[i].f), fl;
		if (dis[v] == dis[u] + 1 && (fl = dfs(v, nf)))
			edge[i].f -= fl, flow -= fl, ret += fl,
			edge[1 + (i - 1 ^ 1)].f += fl;
		if (!flow) return ret;
	} return ret;
}

int main() {
	n = read(), m = read(), G = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	idx = n; S = ++idx; T = ++idx;
	for (int i = 1, vi; i <= n; ++i) {
		vi = read();
		if (a[i]) add(i, T, vi);
		else add(S, i, vi);
	}
	for (int i = 1; i <= m; ++i) {
		int kd = read(), wi = read(), ki = read();
		++idx; ans += wi;
		for (int j = 1, ai; j <= ki; ++j) {
			ai = read();
			if (kd) add(ai, idx, inf);
			else add(idx, ai, inf);
		}
		int ad = read();
		if (kd) add(idx, T, wi + G * ad);
		else add(S, idx, wi + G * ad);
	}
	while (bfs())
		ans -= dfs(S, inf);
	printf("%d\n", ans);
	return 0;
}

int read() {
	int x = 0, f = 1; char c = getchar();
	while (c > '9' || c < '0') f = (c == '-') ? -1 : f, c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}


#include <cstdio>
#include <queue>
#define SZ 2000009
#define inf 0x3f3f3f3f
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) > (y) ? (x) : (y)
using namespace std;

int dis[SZ], cur[SZ], ans;
int n, m, G, idx, S, T, a[SZ], hd[SZ], cnt;
struct Edge{int to, nxt, f; } edge[SZ << 1];
struct DT {
	int dis, u;
	friend bool operator<(DT x, DT y)
		{return x.dis > y.dis; }
} ;
priority_queue <DT> q;

int read();
void add(int u, int v, int f) {
	edge[++cnt] = (Edge){v, hd[u], f}; hd[u] = cnt;
	edge[++cnt] = (Edge){u, hd[v], 0}; hd[v] = cnt;
}
bool bfs() {
	for (int i = 1; i <= idx; ++i) dis[i] = inf, cur[i] = hd[i];
	dis[S] = 0; q.push((DT){0, S});
	while (q.size()) {
		DT now = q.top(); q.pop(); int u = now.u;
		if (dis[u] < now.dis) continue;
		for (int i = hd[u], v; i; i = edge[i].nxt)
			if (edge[i].f && dis[v = edge[i].to] > dis[u] + 1)
				{dis[v] = dis[u] + 1; q.push((DT){dis[v], v}); }
	} return dis[T] < inf;
}
int dfs(int u, int flow, int ret = 0) {
	if (!flow || u == T) return flow;
	for (int i = cur[u], v; i; i = edge[i].nxt) {
		cur[u] = i; v = edge[i].to;
		int nf = Min(flow, edge[i].f), fl;
		if (dis[v] == dis[u] + 1 && (fl = dfs(v, nf)))
			edge[i].f -= fl, flow -= fl, ret += fl,
			edge[1 + (i - 1 ^ 1)].f + fl;
		if (!flow) return ret;
	} return ret;
}

int main() {
	n = read(), m = read(), G = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	idx = n; S = ++idx; T = ++idx;
	for (int i = 1, vi; i <= n; ++i) {
		vi = read();
		if (a[i]) add(i, T, vi);
		else add(S, i, vi);
	}
	for (int i = 1; i <= m; ++i) {
		int kd = read(), wi = read(), ki = read();
		++idx; ans += wi;
		for (int j = 1, ai; j <= ki; ++j) {
			ai = read();
			if (kd) add(ai, idx, inf);
			else add(idx, ai, inf);
		}
		int ad = read();
		if (kd) add(idx, T, wi + G * ad);
		else add(S, idx, wi + G * ad);
	}
	while (bfs())
		ans -= dfs(S, inf);
	printf("%d\n", ans);
	return 0;
}

int read() {
	int x = 0, f = 1; char c = getchar();
	while (c > '9' || c < '0') f = (c == '-') ? -1 : f, c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}


#include <cstdio>
#include <queue>
#define SZ 200009
#define inf 0x3f3f3f3f
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) > (y) ? (x) : (y)
using namespace std;

int dis[SZ], cur[SZ], ans;
int n, m, G, idx, S, T, a[SZ], hd[SZ], cnt;
struct Edge{int to, nxt, f; } edge[SZ << 1];
struct DT {
	int dis, u;
	friend bool operator<(DT x, DT y)
		{return x.dis > y.dis; }
} ;
priority_queue <DT> q;

int read();
void add(int u, int v, int f) {
	edge[++cnt] = (Edge){v, hd[u], f}; hd[u] = cnt;
	edge[++cnt] = (Edge){u, hd[v], 0}; hd[v] = cnt;
}
bool bfs() {
	for (int i = 1; i <= idx; ++i) dis[i] = inf, cur[i] = hd[i];
	dis[S] = 0; q.push((DT){0, S});
	while (q.size()) {
		DT now = q.top(); q.pop(); int u = now.u;
		if (dis[u] < now.dis) continue;
		for (int i = hd[u], v; i; i = edge[i].nxt)
			if (edge[i].f && dis[v = edge[i].to] > dis[u] + 1)
				{dis[v] = dis[u] + 1; q.push((DT){dis[v], v}); }
	} return dis[T] < inf;
}
int dfs(int u, int flow, int ret = 0) {
	if (!flow || u == T) return flow;
	for (int i = cur[u], v; i; i = edge[i].nxt) {
		cur[u] = i; v = edge[i].to;
		int nf = Min(flow, edge[i].f), fl;
		if (dis[v] == dis[u] + 1 && (fl = dfs(v, nf)))
			edge[i].f -= fl, flow -= fl, ret += fl,
			edge[1 + (i - 1 ^ 1)].f + fl;
		if (!flow) return ret;
	} return ret;
}

int main() {
	n = read(), m = read(), G = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	idx = n; S = ++idx; T = ++idx;
	for (int i = 1, vi; i <= n; ++i) {
		vi = read();
		if (a[i]) add(i, T, vi);
		else add(S, i, vi);
	}
	for (int i = 1; i <= m; ++i) {
		int kd = read(), wi = read(), ki = read();
		++idx; ans += wi;
		for (int j = 1, ai; j <= ki; ++j) {
			ai = read();
			if (kd) add(ai, idx, inf);
			else add(idx, ai, inf);
		}
		int ad = read();
		if (kd) add(idx, T, wi + G * ad);
		else add(S, idx, wi + G * ad);
	}
	while (bfs())
		ans -= dfs(S, inf);
	printf("%d\n", ans);
	return 0;
}

int read() {
	int x = 0, f = 1; char c = getchar();
	while (c > '9' || c < '0') f = (c == '-') ? -1 : f, c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
inline int get(){
	char c; while(!isdigit(c=getchar()));
	int v=c-48; while(isdigit(c=getchar()))v=v*10+c-48;
	return v;
}
const int inf=0x3f3f3f3f;
int n,m,g,s,t,sex[10005],sum;
int fst[20005],nxt[200005],to[200005],w[200005],tt=1;
void add_edge(int x,int y,int z){
	nxt[++tt]=fst[x]; fst[x]=tt; to[tt]=y; w[tt]=z;
	nxt[++tt]=fst[y]; fst[y]=tt; to[tt]=x; w[tt]=0;
}
int q[20005],d[20005],cur[20005];
bool bfs(){
	memset(d,0,(t+1)<<2);
	int l=0,r=0; q[0]=s;
	d[s]=1;
	while(l<=r){
		int x=q[l++];
		for(int i=fst[x];i;i=nxt[i])
		if(w[i]&&!d[to[i]]){
			d[to[i]]=d[x]+1;
			q[++r]=to[i];
		}
	}
	return d[t]>0;
}
int dfs(int x,int a){
	if(x==t||a==0)return a;
	int flow=0,f;
	for(int i=cur[x];i;i=nxt[i])
	if(d[to[i]]==d[x]+1&&(f=dfs(to[i],min(a,w[i])))>0){
		flow+=f; w[i]-=f;
		w[i^1]+=f; a-=f;
		if(a==0)break;
	}
	return flow;
}
int main(){
	n=get(); m=get(); g=get();
	for(int i=1;i<=n;++i)sex[i]=get();
	s=n+m+1; t=n+m+2;
	for(int i=1,v;i<=n;++i){
		v=get();
		if(sex[i])add_edge(i,t,v);
		else add_edge(s,i,v);
	}
	for(int i=1,sc,w,k;i<=m;++i){
		sc=get();
		w=get();
		k=get();
		sum+=w;
		for(int j=1,u;j<=k;++j){
			u=get();
			if(sc)add_edge(u,i+n,inf);
			else add_edge(i+n,u,inf);
		}
		w+=g*get();
		if(sc)add_edge(i+n,t,w);
		else add_edge(s,i+n,w);
	}
	int ans=0;
	while(bfs()){
		memcpy(cur,fst,(t+1)<<2);
		ans+=dfs(s,inf);
	}
	cout<<sum-ans<<endl;
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
inline int get(){
	char c; while(!isdigit(c=getchar()));
	int v=c-48; while(isdigit(c=getchar()))v=v*10+c-48;
	return v;
}
const int inf=0x3f3f3f3f;
int n,m,g,s,t,sex[10005],sum;
int fst[20005],nxt[200005],to[200005],w[200005],tt=1;
void add_edge(int x,int y,int z){
	nxt[++tt]=fst[x]; fst[x]=tt; to[tt]=y; w[tt]=z;
	nxt[++tt]=fst[y]; fst[y]=tt; to[tt]=x; w[tt]=0;
}
int q[20005],d[20005],cur[20005];
bool bfs(){
	memset(d,0,(t+1)<<2);
	int l=0,r=0; q[0]=s;
	d[s]=1;
	while(l<=r){
		int x=q[l++];
		for(int i=fst[x];i;i=nxt[i])
		if(w[i]&&!d[to[i]]){
			d[to[i]]=d[x]+1;
			q[++r]=to[i];
		}
	}
	return d[t]>0;
}
int dfs(int x,int a){
	if(x==t||a==0)return a;
	int flow=0,f;
	for(int&i=cur[x];i;i=nxt[i])
	if(d[to[i]]==d[x]+1&&(f=dfs(to[i],min(a,w[i])))>0){
		flow+=f; w[i]-=f;
		w[i^1]+=f; a-=f;
		if(a==0)break;
	}
	return flow;
}
int main(){
	n=get(); m=get(); g=get();
	for(int i=1;i<=n;++i)sex[i]=get();
	s=n+m+1; t=n+m+2;
	for(int i=1,v;i<=n;++i){
		v=get();
		if(sex[i])add_edge(i,t,v);
		else add_edge(s,i,v);
	}
	for(int i=1,sc,w,k;i<=m;++i){
		sc=get();
		w=get();
		k=get();
		sum+=w;
		for(int j=1,u;j<=k;++j){
			u=get();
			if(sc)add_edge(u,i+n,inf);
			else add_edge(i+n,u,inf);
		}
		w+=g*get();
		if(sc)add_edge(i+n,t,w);
		else add_edge(s,i+n,w);
	}
	int ans=0;
	while(bfs()){
		memcpy(cur,fst,(t+1)<<2);
		ans+=dfs(s,inf);
	}
	cout<<sum-ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
typedef long long ll;
const int N = 20010 , INF = 0x3f3f3f3f;

struct edge{
	int pos , nx , cap;
}e[N * 40];

int num , s , t , n , m , g;
int sum , head[N] , cur[N] , d[N];
bool mark[N] , sex[N];
vector<int> graph[N];

void add(int u , int v , int c){
	e[++num].pos = v; e[num].cap = c; e[num].nx = head[u];
	head[u] = num;
	e[++num].pos = u; e[num].cap = 0; e[num].nx = head[v];
	head[v] = num;
}

bool BFS(){
	queue<int> q;
	memcpy(cur , head , sizeof(cur));
	memset(d , 0 , sizeof(d));
	q.push(s); d[s] = 1;
	while(!q.empty()){
		int now = q.front(); q.pop();
		for(int i = head[now] ; i ; i = e[i].nx){
			if(e[i].cap && !d[e[i].pos]){
				d[e[i].pos] = d[now] + 1;
				if(e[i].pos == t) return true;
				q.push(e[i].pos);
			}
		}
	}
	return false;
}

int finds(int now , int minx){
	if(now == t)
		return minx;
	int ret;
	for(int &i = cur[now] ; i ; i = e[i].nx){
		if(e[i].cap && d[e[i].pos] == d[now] + 1 && (ret = finds(e[i].pos , min(minx , e[i].cap)))){
			e[i].cap -= ret;
			e[i ^ 1].cap += ret;
			return ret;
		}
	}
	return 0;
}

int mincut(){
	int ans = 0 , temp;
	while(BFS()){
		while(temp = finds(s , INF)){
			ans += temp;
		}
	}
	return ans;
}

int main(){
	int x , y , k , w , g;
	scanf("%d%d%d",&n,&m,&g);
	s = n + m + 1 , t = n + m + 2 , num = 1;
	for(int i = 1 ; i <= n ; i++) scanf("%d",&sex[i]);
	for(int i = 1 ; i <= n ; i++){
		scanf("%d",&x);
		if(sex[i])
			add(i , t , x);
		else
			add(s , i , x);
	}
	for(int i = 1 ; i <= m ; i++){
		scanf("%d%d%d", &mark[i] , &w , &k);
		for(int j = 1 ; j <= k ; j++){
			scanf("%d",&x);
			if(sex[x] ^ mark[i]){
				if(mark[i])
					add(x , i + n , INF);
				else
					add(i + n , x , INF);
			}
			graph[x].push_back(i);
		}
		sum += w;
		scanf("%d",&x);
		if(x) w += g;

		printf("%d\n",w);

		if(mark[i]) add(i + n , t , w);
		else add(s , i + n , w);
	}

	for(int i = 1 ; i <= n ; i++)
		for(int j = 0 ; j < graph[i].size() ; j++)
			for(int l = j + 1 ; l < graph[i].size() ; l++)
				if(mark[graph[i][l]] ^ mark[graph[i][j]])
					if(mark[graph[i][l]])
						add(graph[i][j] + n , graph[i][l] + n , INF);
					else
						add(graph[i][l] + n , graph[i][j] + n , INF);

	printf("%d\n", sum - mincut());
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 20010
using namespace std;
const int inf = 0x3f3f3f3f;
struct flows {
    struct node {
        int to, next, flow;
    } e[N * 40];
    int tot, st[N], dis[N], cur[N];
    void init() {
        tot = -1, memset(e, -1, sizeof e),
        memset(st, -1, sizeof st);
    }
    void add(int x, int y, int z) {
        e[++tot].to = y;
        e[tot].flow = z;
        e[tot].next = st[x];
        st[x] = tot;
    }
    void add_edge(int x, int y, int z) {
//      printf("%d %d %d\n", x, y, z);
        add(x, y, z), add(y, x, 0);
    }
    queue <int> que;
    int bfs(int S, int T) {
        memcpy(cur, st, sizeof st);
        memset(dis, 0, sizeof dis);
        while (!que.empty()) que.pop();
        que.push(S);
        dis[S] = 1;
        while(!que.empty()) {
            int now = que.front();
            que.pop();
            for (int i = st[now]; ~i; i = e[i].next)
                if (e[i].flow && !dis[e[i].to]) {
                    dis[e[i].to] = dis[now] + 1;
                    if (e[i].to == T) return 1;
                    que.push(e[i].to);
                }
        }
        return 0;
    }
    int finds(int now, int T, int flow) {
        if (now == T)
            return flow;
        int f;
        for (int i = cur[now]; ~i; i = e[i].next) {
            cur[now] = i;
            if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
                (f = finds(e[i].to, T, min(flow, e[i].flow)))) {
                e[i].flow -= f;
                e[i^1].flow += f;
                return f;
            }
        }
        return 0;
    }
    int dinic(int S, int T) {
        int flow = 0, x;
        while(bfs(S, T)) {
            while(x = finds(S, T, inf)) {
                flow += x;
            }
        }
        return flow;
    }
}flow;
int n, m, g, v, w, t, id, ans, x, sex[N], want[N];
vector <int> ins[N];
main() {
    scanf("%d%d%d", &n, &m, &g);
    int S = 0, T = n + m + 1;
    flow.init();
    for (int i = 1; i <= n; ++i)
        scanf("%d", &sex[i]);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &v);
        if (sex[i])
            flow.add_edge(i, T, v);
        else
            flow.add_edge(S, i, v);
    }
    for (int i = 1; i <= m; ++i) {
        scanf("%d%d%d", &want[i], &w, &t);
        for (int j = 1; j <= t; ++j) {
            scanf("%d", &id);
            if (sex[id] ^ want[i])
                if (want[i])
                    flow.add_edge(id, n + i, inf);
                else
                    flow.add_edge(n + i, id, inf);
            ins[id].push_back(i);
        }
        ans += w;
        scanf("%d", &x);
        if (x) w += g;
        if (want[i])
            flow.add_edge(n + i, T, w);
        else
            flow.add_edge(S, n + i, w);
    }
    for (int i = 1; i <= n; ++i)
        for (int j = 0; j < ins[i].size(); ++j)
            for (int k = j + 1; k < ins[i].size(); ++k)
                if (want[ins[i][j]] ^ want[ins[i][k]])
                    if (want[ins[i][j]])
                        flow.add_edge(n + ins[i][k], n + ins[i][j], inf);
                    else
                        flow.add_edge(n + ins[i][j], n + ins[i][k], inf);
    printf("%d", ans - flow.dinic(S, T));
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
typedef long long ll;
const int N = 20010 , INF = 0x3f3f3f3f;

struct edge{
	int pos , nx , cap;
}e[N * 40];

int num , s , t , n , m , g;
int sum , head[N] , cur[N] , d[N];
bool mark[N] , sex[N];
vector<int> graph[N];

void add(int u , int v , int c){
	e[++num].pos = v; e[num].cap = c; e[num].nx = head[u];
	head[u] = num;
	e[++num].pos = u; e[num].cap = 0; e[num].nx = head[v];
	head[v] = num;
}

bool BFS(){
	queue<int> q;
	memcpy(cur , head , sizeof(cur));
	memset(d , 0 , sizeof(d));
	q.push(s); d[s] = 1;
	while(!q.empty()){
		int now = q.front(); q.pop();
		for(int i = head[now] ; i ; i = e[i].nx){
			if(e[i].cap && !d[e[i].pos]){
				d[e[i].pos] = d[now] + 1;
				if(e[i].pos == t) return true;
				q.push(e[i].pos);
			}
		}
	}
	return false;
}

int finds(int now , int minx){
	if(now == t)
		return minx;
	int ret;
	for(int &i = cur[now] ; i ; i = e[i].nx){
		if(e[i].cap && d[e[i].pos] == d[now] + 1 && (ret = finds(e[i].pos , min(minx , e[i].cap)))){
			e[i].cap -= ret;
			e[i ^ 1].cap += ret;
			return ret;
		}
	}
	return 0;
}

int mincut(){
	int ans = 0 , temp;
	while(BFS()){
		while(temp = finds(s , INF)){
			ans += temp;
		}
	}
	return ans;
}

int main(){
	int x , y , k , w , g;
	scanf("%d%d%d",&n,&m,&g);
	s = n + m + 1 , t = n + m + 2 , num = 1;
	for(int i = 1 ; i <= n ; i++) scanf("%d",&sex[i]);
	for(int i = 1 ; i <= n ; i++){
		scanf("%d",&x);
		if(sex[i])
			add(i , t , x);
		else
			add(s , i , x);
	}
	for(int i = 1 ; i <= m ; i++){
		scanf("%d%d%d", &mark[i] , &w , &k);
		for(int j = 1 ; j <= k ; j++){
			scanf("%d",&x);
			if(sex[x] ^ mark[i]){
				if(mark[i])
					add(x , i + n , INF);
				else
					add(i + n , x , INF);
			}
			graph[x].push_back(i);
		}
		sum += w;
		scanf("%d",&x);
		if(x) w += g;

		if(mark[i]) add(i + n , t , w);
		else add(s , i + n , w);
	}

	for(int i = 1 ; i <= n ; i++)
		for(int j = 0 ; j < graph[i].size() ; j++)
			for(int l = j + 1 ; l < graph[i].size() ; l++)
				if(mark[graph[i][l]] ^ mark[graph[i][j]])
					if(graph[i][l])
						add(graph[i][j] + n , graph[i][l] + n , INF);
					else
						add(graph[i][l] + n , graph[i][j] + n , INF);

	printf("%d\n", sum - mincut());
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
typedef long long ll;
const int N = 20010 , INF = 0x3f3f3f3f;

struct edge{
	int pos , nx , cap;
}e[N * 40];

int num , s , t , n , m , g;
int sum , head[N] , cur[N] , d[N];
bool mark[N] , sex[N];
vector<int> graph[N];

void add(int u , int v , int c){
	e[++num].pos = v; e[num].cap = c; e[num].nx = head[u];
	head[u] = num;
	e[++num].pos = u; e[num].cap = 0; e[num].nx = head[v];
	head[v] = num;
}

bool BFS(){
	queue<int> q;
	memcpy(cur , head , sizeof(cur));
	memset(d , 0 , sizeof(d));
	q.push(s); d[s] = 1;
	while(!q.empty()){
		int now = q.front(); q.pop();
		for(int i = head[now] ; i ; i = e[i].nx){
			if(e[i].cap && !d[e[i].pos]){
				d[e[i].pos] = d[now] + 1;
				if(e[i].pos == t) return true;
				q.push(e[i].pos);
			}
		}
	}
	return false;
}

int finds(int now , int minx){
	if(now == t)
		return minx;
	int ret;
	for(int &i = cur[now] ; i ; i = e[i].nx){
		if(e[i].cap && d[e[i].pos] == d[now] + 1 && (ret = finds(e[i].pos , min(minx , e[i].cap)))){
			e[i].cap -= ret;
			e[i ^ 1].cap += ret;
			return ret;
		}
	}
	return 0;
}

int mincut(){
	int ans = 0 , temp;
	while(BFS()){
		while(temp = finds(s , INF)){
			ans += temp;
		}
	}
	return ans;
}

int main(){
	int x , y , k , w , g;
	scanf("%d%d%d",&n,&m,&g);
	s = n + m + 1 , t = n + m + 2 , num = 1;
	for(int i = 1 ; i <= n ; i++) scanf("%d",&sex[i]);
	for(int i = 1 ; i <= n ; i++){
		scanf("%d",&x);
		if(sex[i])
			add(i , t , x);
		else
			add(s , i , x);
	}
	for(int i = 1 ; i <= m ; i++){
		scanf("%d%d%d", &mark[i] , &w , &k);
		for(int j = 1 ; j <= k ; j++){
			scanf("%d",&x);
			if(sex[x] ^ mark[i]){
				if(mark[i])
					add(x , i + n , INF);
				else
					add(i + n , x , INF);
			}
			graph[x].push_back(i);
		}
		sum += w;
		scanf("%d",&x);
		if(x) w += g;

		if(mark[i]) add(i + n , t , w);
		else add(s , i + n , w);
	}

	for(int i = 1 ; i <= n ; i++)
		for(int j = 0 ; j < graph[i].size() ; j++)
			for(int l = j + 1 ; l < graph[i].size() ; l++)
				if(mark[graph[i][l]] ^ mark[graph[i][j]])
					if(mark[graph[i][l]])
						add(graph[i][j] + n , graph[i][l] + n , INF);
					else
						add(graph[i][l] + n , graph[i][j] + n , INF);

	printf("%d\n", sum - mincut());
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
typedef long long ll;
const int N = 20010 , INF = 0x3f3f3f3f;

struct edge{
	int pos , nx , cap;
}e[N * 40];

int num , s , t , n , m , g;
int sum , head[N] , cur[N] , d[N];
bool mark[N] , sex[N];
vector<int> graph[N];

void add(int u , int v , int c){
	e[++num].pos = v; e[num].cap = c; e[num].nx = head[u];
	head[u] = num;
	e[++num].pos = u; e[num].cap = 0; e[num].nx = head[v];
	head[v] = num;
}

bool BFS(){
	queue<int> q;
	memcpy(cur , head , sizeof(cur));
	memset(d , 0 , sizeof(d));
	q.push(s); d[s] = 1;
	while(!q.empty()){
		int now = q.front(); q.pop();
		for(int i = head[now] ; i ; i = e[i].nx){
			if(e[i].cap && !d[e[i].pos]){
				d[e[i].pos] = d[now] + 1;
				if(e[i].pos == t) return true;
				q.push(e[i].pos);
			}
		}
	}
	return false;
}

int finds(int now , int minx){
	if(now == t)
		return minx;
	int ret;
	for(int &i = cur[now] ; i ; i = e[i].nx){
		if(e[i].cap && d[e[i].pos] == d[now] + 1 && (ret = finds(e[i].pos , min(minx , e[i].cap)))){
			e[i].cap -= ret;
			e[i ^ 1].cap += ret;
			return ret;
		}
	}
	return 0;
}

int mincut(){
	int ans = 0 , temp;
	while(BFS()){
		while(temp = finds(s , INF)){
			ans += temp;
		}
	}
	return ans;
}

int main(){
	int x , y , k , w , g;
	scanf("%d%d%d",&n,&m,&g);
	s = n + m + 1 , t = n + m + 2 , num = 1;
	for(int i = 1 ; i <= n ; i++) scanf("%d",&sex[i]);
	for(int i = 1 ; i <= n ; i++){
		scanf("%d",&x);
		if(sex[i])
			add(i , t , x);
		else
			add(s , i , x);
	}
	for(int i = 1 ; i <= m ; i++){
		scanf("%d%d%d", &mark[i] , &w , &k);
		sum += w;

		for(int j = 1 ; j <= k ; j++){
			scanf("%d",&x);
			if(sex[x] ^ mark[i]){
				if(mark[i])
					add(x , i + n , INF);
				else
					add(i + n , x , INF);
			}
			graph[x].push_back(i);
		}

		scanf("%d",&x);
		if(x) w += g;

		if(mark[i]) add(i + n , t , w);
		else add(s , i + n , w);
	}

	for(int i = 1 ; i <= n ; i++)
		for(int j = 0 ; j < graph[i].size() ; j++)
			for(int l = j + 1 ; l < graph[i].size() ; l++)
				if(mark[graph[i][l]] ^ mark[graph[i][j]])
					if(graph[i][l])
						add(graph[i][j] + n , graph[i][l] + n , INF);
					else
						add(graph[i][l] + n , graph[i][j] + n , INF);

	printf("%d\n", sum - mincut());
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
typedef long long ll;
const int N = 20005 , INF = 0x3f3f3f3f;

struct edge{
	int pos , nx , cap;
}e[N * 40];

int num , s , t , n , m , g;
int sum , head[N] , cur[N] , d[N];
bool mark[N] , sex[N];
vector<int> graph[10005];

void add(int u , int v , int c){
	e[++num].pos = v; e[num].cap = c; e[num].nx = head[u];
	head[u] = num;
	e[++num].pos = u; e[num].cap = 0; e[num].nx = head[v];
	head[v] = num;
}

bool BFS(){
	queue<int> q;
	memcpy(cur , head , sizeof(cur));
	memset(d , 0 , sizeof(d));
	q.push(s); d[s] = 1;
	while(!q.empty()){
		int now = q.front(); q.pop();
		for(int i = head[now] ; i ; i = e[i].nx){
			if(e[i].cap && !d[e[i].pos]){
				d[e[i].pos] = d[now] + 1;
				if(e[i].pos == t) return true;
				q.push(e[i].pos);
			}
		}
	}
	return false;
}

int finds(int now , int minx){
	if(now == t)
		return minx;
	int ret;
	for(int &i = cur[now] ; i ; i = e[i].nx){
		if(e[i].cap && d[e[i].pos] == d[now] + 1 && (ret = finds(e[i].pos , min(minx , e[i].cap)))){
			e[i].cap -= ret;
			e[i ^ 1].cap += ret;
			return ret;
		}
	}
	return 0;
}

int mincut(){
	int ans = 0 , temp;
	while(BFS()){
		while(temp = finds(s , INF)){
			ans += temp;
		}
	}
	return ans;
}

int main(){
	int x , y , k , w , g;
	scanf("%d%d%d",&n,&m,&g);
	s = n + m + 1 , t = n + m + 2 , num = 1;
	for(int i = 1 ; i <= n ; i++) scanf("%d",&sex[i]);
	for(int i = 1 ; i <= n ; i++){
		scanf("%d",&x);
		if(sex[i])
			add(i , t , x);
		else
			add(s , i , x);
	}
	for(int i = 1 ; i <= m ; i++){
		scanf("%d%d%d", &mark[i] , &w , &k);
		sum += w;

		for(int j = 1 ; j <= k ; j++){
			scanf("%d",&x);
			if(sex[x] ^ mark[i]){
				if(mark[i])
					add(x , i + n , INF);
				else
					add(i + n , x , INF);
			}
			graph[x].push_back(i);
		}

		scanf("%d",&x);
		if(x) w += g;

		if(mark[i]) add(i + n , t , w);
		else add(s , i + n , w);
	}

	for(int i = 1 ; i <= n ; i++)
		for(int j = 0 ; j < graph[i].size() ; j++)
			for(int l = j + 1 ; l < graph[i].size() ; l++)
				if(mark[graph[i][l]] ^ mark[graph[i][j]])
					if(graph[i][l])
						add(graph[i][j] + n , graph[i][l] + n , INF);
					else
						add(graph[i][l] + n , graph[i][j] + n , INF);

	printf("%d\n", sum - mincut());
	return 0;
}


/************************************************
*Author        :  xzj213
*Created Time  :  2020.10.07.10:21
*Mail          :  xzj213@qq.com
*Problem       :  CF311E
************************************************/
#include <bits/stdc++.h>
#define int long long
#define REP(i,a,b) for(register int i=(a);i<=(b);i++)
#define DREP(i,a,b) for(register int i=(a);i>=(b);i--)
#define mem(a,x) memset((a),(x),sizeof(a))
#define pii pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define x first
#define y second
#define str(a) strlen(a)
using namespace std;
const int maxn=5e4+5,maxm=2e6+5,inf=0x3f3f3f3f3f3f3f;
int beg[maxn],tot=1,level[maxn],n,m,s,t,maxflow,g,a[maxn],tmp[maxn],sum;
struct edge{
	int nex,to,w;
}e[maxm*2];
void chkmax(int &a,int b) {if (a<b)a=b;}
void chkmin(int &a,int b) {if (a>b)a=b;}
void add(int x,int y,int z) {
	e[++tot]=(edge){beg[x],y,z};
	beg[x]=tot;
	e[++tot]=(edge){beg[y],x,0};
	beg[y]=tot;
}
int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch>57 || ch<48) {if(ch==45)f=-1;ch=getchar();}
    while (ch<=57 && ch>=48) {x=x*10+ch-48;ch=getchar();}
    return x*f;
}
bool bfs() {
	mem(level,0);
	queue<int>q;
	q.push(s);
	level[s]=1;
	while(!q.empty()) {
		int now=q.front();
		q.pop();
		for (int i=beg[now];i;i=e[i].nex) {
			int nex=e[i].to;
			if (e[i].w && !level[nex]) {
				q.push(nex);
				level[nex]=level[now]+1;
				if (nex==t) return true;
			}
		}
	}
	return false;
}
int dinic(int now,int flow) {
	if (now==t) return flow;
	int rest=flow,inc;
	for (int i=beg[now];i && rest;i=e[i].nex) {
		int nex=e[i].to;
		if (e[i].w && level[nex]==level[now]+1) {
			inc=dinic(nex,min(rest,e[i].w));
			if (!inc) level[nex]=0;
			e[i].w-=inc;
			e[i^1].w+=inc;
			rest-=inc;
		}
	}
	return flow-rest;
}
signed main() {
    n=read();m=read();g=read();s=0,t=n+m+1;
	REP (i,1,n) a[i]=read();
	REP (i,1,n) {
		int x=read();
		if (a[i]) add(i,t,x);
		else add(s,i,x);
	}
	REP (i,1,m) {
		int f=read(),w=read(),k=read();
		REP (j,1,k) tmp[j]=read();
		int f2=read();sum+=w;
		if (f) {
			REP (j,1,k) add(tmp[j],n+i,inf);
			add(n+i,t,w+f2*g);
		}
		else {
			REP (j,1,k) add(n+i,tmp[j],inf);
			add(s,n+i,w+f2*g);
		}
	}
	int flow=0;
	while(bfs()) {
		flow=dinic(s,inf);
		maxflow+=flow;
	}
	cout<<sum-maxflow<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=(1e4+9+2000+2),M=(N+1e4)*2,INF=1e9;
int n,m,g,S,T,tot=-1,sum;
int h[N],dis[N],cur[N],ver[M],nxt[M],f[M];
int Q[N],hh,tt,a[N];

inline void add(int x,int y,int z)
{
	ver[++tot]=y,nxt[tot]=h[x],h[x]=tot,f[tot]=z;
	ver[++tot]=x,nxt[tot]=h[y],h[y]=tot,f[tot]=0;
}

inline bool bfs()
{
	memset(dis,-1,sizeof dis);
	hh=tt=0;
	Q[0]=S,dis[S]=0,cur[S]=h[S];
	while(hh<=tt)
	{
		int u=Q[hh++];
		for(int i=h[u];~i;i=nxt[i])
		{
			int v=ver[i];
			if(dis[v]==-1&&f[i])
			{
				dis[v]=dis[u]+1;
				cur[v]=h[v];
				if(v==T) return true;
				Q[++tt]=v;
			}
		}
	}
	return false;
}

int dfs(int u,int lim)
{
	if(u==T) return lim;
	int flow=0;
	for(int i=cur[u];~i&&flow<lim;i=nxt[i])
	{
		int v=ver[i];
		if(dis[v]==dis[u]+1&&f[i])
		{
			int t=dfs(v,min(f[i],lim-flow));
			if(!t) dis[v]=-1;
			f[i]-=t,f[i^1]+=t,flow+=t;
		}
	}
	return flow;
}

int dinic()
{
	int res=0;
	while(bfs()) res+=dfs(S,INF);
	return res;
}
int main()
{
	memset(h,-1,sizeof h);
	scanf("%d%d%d",&n,&m,&g);
	S=0,T=n+m+1;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(a[i]==0) add(S,i,x);
		else add(i,T,x);
	}
	for(int i=1;i<=m;i++)
	{
		int op,w,k,p;
		int flag;
		scanf("%d%d%d",&op,&w,&k);
		sum+=w;
		while(k--)
		{
			scanf("%d",&p);
			if(op==0) add(i+n,p,INF);
			else add(p,i+n,INF);
		}
		scanf("%d",&flag);
		if(flag) w+=g;
		if(op==0) add(S,i+n,w);
		else add(i+n,T,w);
	}
	printf("%d",sum-dinic());
	return 0;
}