Problem: SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In other words, she can change female dogs into male dogs and vice versa.

She is going to demonstrate this technique. Now SmallR has n dogs, the costs of each dog's change may be different. The dogs are numbered from 1 to n. The cost of change for dog i is vi RMB. By the way, this technique needs a kind of medicine which can be valid for only one day. So the experiment should be taken in one day and each dog can be changed at most once.

This experiment has aroused extensive attention from all sectors of society. There are m rich folks which are suspicious of this experiment. They all want to bet with SmallR forcibly. If SmallR succeeds, the i-th rich folk will pay SmallR wi RMB. But it's strange that they have a special method to determine whether SmallR succeeds. For i-th rich folk, in advance, he will appoint certain ki dogs and certain one gender. He will think SmallR succeeds if and only if on some day the ki appointed dogs are all of the appointed gender. Otherwise, he will think SmallR fails.

If SmallR can't satisfy some folk that isn't her friend, she need not pay him, but if someone she can't satisfy is her good friend, she must pay g RMB to him as apologies for her fail.

Then, SmallR hope to acquire money as much as possible by this experiment. Please figure out the maximum money SmallR can acquire. By the way, it is possible that she can't obtain any money, even will lose money. Then, please give out the minimum money she should lose.

Input

The first line contains three integers n, m, g (1 ≤ n ≤ 104, 0 ≤ m ≤ 2000, 0 ≤ g ≤ 104). The second line contains n integers, each is 0 or 1, the sex of each dog, 0 represent the female and 1 represent the male. The third line contains n integers v1, v2, ..., vn (0 ≤ vi ≤ 104).

Each of the next m lines describes a rich folk. On the i-th line the first number is the appointed sex of i-th folk (0 or 1), the next two integers are wi and ki (0 ≤ wi ≤ 104, 1 ≤ ki ≤ 10), next ki distinct integers are the indexes of appointed dogs (each index is between 1 and n). The last number of this line represents whether i-th folk is SmallR's good friend (0 — no or 1 — yes).

Output

Print a single integer, the maximum money SmallR can gain. Note that the integer is negative if SmallR will lose money. 

Examples

Input

5 5 9
0 1 1 1 0
1 8 6 2 3
0 7 3 3 2 1 1
1 8 1 5 1
1 0 3 2 1 4 1
0 8 3 4 2 1 0
1 7 2 4 1 1


Output

2


Input

5 5 8
1 0 1 1 1
6 5 4 2 8
0 6 3 2 3 4 0
0 8 3 3 2 4 0
0 0 3 3 4 1 1
0 10 3 4 3 1 1
0 4 3 3 4 1 1


Output

16

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
pair<int, int> &operator+=(pair<int, int> &a, const pair<int, int> &b) {
  a.first += b.first;
  a.second += b.second;
  return a;
}
pair<int, int> &operator-=(pair<int, int> &a, const pair<int, int> &b) {
  a.first -= b.first;
  a.second -= b.second;
  return a;
}
template <class T, class U>
bool cmp_second(const pair<T, U> &a, const pair<T, U> &b) {
  return a.second < b.second;
}
template <class T>
T gcd(T a, T b) {
  a = abs(a);
  b = abs(b);
  while (b) {
    T t = b;
    b = a % b;
    a = t;
  }
  return a;
}
template <class T>
pair<T, T> ext_gcd(T a, T b) {
  T a0 = 1, a1 = 0, b0 = 0, b1 = 1;
  if (a < 0) {
    a = -a;
    a0 = -1;
  }
  if (b < 0) {
    b = -b;
    b1 = -1;
  }
  while (b) {
    T t, q = a / b;
    t = b;
    b = a - b * q;
    a = t;
    t = b0;
    b0 = a0 - b0 * q;
    a0 = t;
    t = b1;
    b1 = a1 - b1 * q;
    a1 = t;
  }
  return make_pair(a0, a1);
}
inline int sg(int x) { return x ? (x > 0 ? 1 : -1) : 0; }
const int inf = 1000000010;
class Arc {
 public:
  int to, revind;
  int flow, cap;
  Arc(int _to, int _revind, int _cap)
      : to(_to), revind(_revind), cap(_cap), flow(0) {}
};
class DinicFlow {
 public:
  int vn, src, sink;
  vector<Arc> adj[15000];
  int dist[15000];
  int ql, qr, que[15000];
  int iter[15000];
  DinicFlow(int _vn, int _src, int _sink) : vn(_vn), src(_src), sink(_sink) {}
  void append(int v, int u, int c) {
    int vid = adj[v].size(), uid = adj[u].size();
    adj[v].push_back(Arc(u, uid, c));
    adj[u].push_back(Arc(v, vid, 0));
  }
  void bfs_layer() {
    ql = qr = 0;
    for (int i = 0; i < vn; i++) dist[i] = inf;
    que[qr++] = src;
    dist[src] = 0;
    while (ql < qr) {
      int v = que[ql++];
      for (int i = 0; i < adj[v].size(); i++) {
        int u = adj[v][i].to;
        int res = adj[v][i].cap - adj[v][i].flow;
        if (dist[u] < inf) continue;
        if (!res) continue;
        que[qr++] = u;
        dist[u] = dist[v] + 1;
      }
    }
  }
  int dfs_push(int v, int a) {
    if (v == sink) return a;
    int pushed = 0;
    for (int &i = iter[v]; i < adj[v].size(); i++) {
      int u = adj[v][i].to;
      int res = adj[v][i].cap - adj[v][i].flow;
      if (dist[u] != dist[v] + 1) continue;
      if (!res) continue;
      int ri = adj[v][i].revind;
      int pf = dfs_push(u, min(a - pushed, res));
      adj[v][i].flow += pf;
      adj[u][ri].flow -= pf;
      pushed += pf;
      if (pushed == a) break;
    }
    return pushed;
  }
  int push_flow() {
    bfs_layer();
    for (int i = 0; i < vn; i++) iter[i] = 0;
    return dfs_push(src, inf);
  }
  int maxflow() {
    int f = 0;
    while (1) {
      int pf = push_flow();
      if (!pf) break;
      f += pf;
    }
    return f;
  }
  vector<pair<int, int> > mincut() {
    bfs_layer();
    vector<pair<int, int> > cut;
    for (int v = 0; v < vn; v++) {
      for (int i = 0; i < adj[v].size(); i++) {
        int u = adj[v][i].to;
        if (dist[v] < inf && dist[u] == inf) cut.push_back(make_pair(v, u));
      }
    }
    return cut;
  }
  vector<int> srccut() {
    bfs_layer();
    vector<int> vs;
    for (int v = 0; v < vn; v++)
      if (dist[v] < inf) vs.push_back(v);
    return vs;
  }
  vector<int> sinkcut() {
    bfs_layer();
    vector<int> vs;
    for (int v = 0; v < vn; v++)
      if (dist[v] == inf) vs.push_back(v);
    return vs;
  }
  bool mincut_is_isolated_sink() { return sinkcut().size() == 1; }
};
int nsub, nplan, pen;
int osex[15000], tran[15000];
int base;
inline int vsub(int x) { return x + 1; }
inline int vplan(int x) { return nsub + x + 1; }
int main(void) {
  scanf("%d %d %d", &(nsub), &(nplan), &(pen));
  int vn = nsub + nplan + 2;
  int src = 0;
  int sink = vn - 1;
  base = 0;
  DinicFlow df(vn, src, sink);
  for (int i = 0; i < nsub; i++) scanf("%d", &(osex[i]));
  for (int i = 0; i < nsub; i++) {
    scanf("%d", &(tran[i]));
    if (osex[i])
      df.append(vsub(i), sink, tran[i]);
    else
      df.append(src, vsub(i), tran[i]);
  }
  for (int i = 0; i < nplan; i++) {
    int goal, reward, num, isfriend;
    scanf("%d %d %d", &(goal), &(reward), &(num));
    for (int t = 0; t < num; t++) {
      int u;
      scanf("%d", &(u));
      u--;
      if (goal)
        df.append(vsub(u), vplan(i), inf);
      else
        df.append(vplan(i), vsub(u), inf);
    }
    scanf("%d", &(isfriend));
    int rc = reward + (isfriend ? pen : 0);
    base += reward;
    if (goal)
      df.append(vplan(i), sink, rc);
    else
      df.append(src, vplan(i), rc);
  }
  int mincost = df.maxflow();
  int sol = base - mincost;
  printf("%d\n", sol);
  return 0;
}